Two select boxes trigger multiple sorting operations

Struggling to implement 2 different sorting operations on two separate columns in a datagrid, using 2 different select boxes has proven to be challenging. I attempted the code below, but as a beginner, I was unable to solve it...

In HTML:

<select ng-model="reverse1">
          <option value="false">Top Sr</option>
          <option value="true">Bottom Sr</option>
</select>

<select ng-model="reverse2">
          <option value="false">Top Rank</option>
          <option value="true">Bottom Rank</option>
</select>


In datagrid:

<tr ng-repeat="item in empList | orderBy : ['SR', 'TR']" >


In controller:

$scope.reverse1 = false;
$scope.reverse2 = false;

Answer №1

Here is a suggested solution for your sorting needs:

<select  ng-model="sr">
          <option  value="+sr">Top Sr</option>
          <option  value="-sr">Bottom Sr</option>
</select>

 <select  ng-model="tr">
          <option  value="+tr">Top Rank</option>
          <option  value="-tr">Bottom Rank</option>
</select>

To apply sorting to the table, use the following ng-repeat statement:

<tr ng-repeat="items in empList | orderBy : [tr, sr]" >

In the controller, set default values as follows:

$scope.sr = '+sr';
$scope.tr = '+tr';

Replace 'sr' and 'tr' with the property names from empList that you want to sort by.

By selecting different options in the select dropdowns, the $scope variables will update automatically and reflect in the correct sorting order:

<tr ng-repeat="items in empList | orderBy : [sr, tr]" >

An updated plunker is available for reference. Make sure to review the changes made to the property name 'tr'.

UPDATE:

If you want to always sort by the property associated with the most recently changed dropdown, follow these steps (another plunker):

1)Initialize the following variable:

$scope.srRecentlyChanged = true;

2)Add an event on select dropdown change:

<select  ng-model="sr" ng-change="srRecentlyChanged = true;">
          <option  value="+sr">Top Sr</option>
          <option  value="-sr">Bottom Sr</option>
</select>

 <select  ng-model="tr" ng-change="srRecentlyChanged = false;">
          <option  value="+tr">Top Rank</option>
          <option  value="-tr">Bottom Rank</option>
</select>

3)Modify the orderBy expression accordingly:

<tr ng-repeat="items in empList | orderBy : (srRecentlyChanged? sr : tr)">

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issues with Jquery Checkboxes Functionality

Hi everyone, yesterday I had a question and since then I have made quite a few changes to my code. Right now, I am attempting to make some JavaScript work when a specific checkbox is checked. However, nothing is happening when I check the checkbox. Can any ...

Filtering React component names and their corresponding values

I am looking to apply filters to React components based on their name and values. Below is the array that needs to be filtered: let filteredArray: any[] = [] const filteredItems: any[] = eventList.filter( (event) => event.printEvent.labels = ...

What is the best way to merge two JSON objects linked by a foreign key in Angular?

Looking to display data from two JSON objects in a table, connected by a foreign key. The first set of JSON data is as follows: $scope.items = [ {status_id: 1, type: 1}, {status_id: 2, type: 2}, {status_id: 1, type: 3}, {status_id: 1, typ ...

Verify whether a string includes any of the elements within an array

Imagine I have a string: const subject = "This process is flawless" and an array: const matchArray = ["process","procedure","job"] If subject contains any keyword from matchArray, I want to do the following: if (su ...

Using the Angular translate filter within a ternary operator

I am currently working on translating my project into a different language. To do this, I have implemented the Angular Translate library and uploaded an external JSON file containing all the translations. Here is an example of how it looks: { "hello_wor ...

Troubleshooting issue with AngularJS ng-repeat not functioning properly when using Object key value filter with ng-model

Is there a way to have an object with an ID as a key value pair? For example: friends = { 1:{name:'John', age:25, gender:'boy'}, 2:{name:'Jessie', age:30, gender:'girl'}, 3:{name:'Johanna', ag ...

Retrieve the weekday dates for a specific year, month, and relative week number using Javascript or Typescript

I am in need of a custom function called getDaysOfWeekDates that can take a year, a month (ranging from 0 to 11), and the week number of each month (usually 4-5 weeks per month) as parameters, and return a list of dates containing each day of that particul ...

Button with a Jquery icon design

Hello, I am currently working on implementing an icon button that can collapse and expand text. Although I have successfully implemented the logic for collapsing/expanding, I am facing difficulties in creating the actual icon button. The theme I am require ...

ajaxStop and ajaxStart not functioning as expected

Currently working on a project to create a Wikipedia viewer, and running into an issue with the following code snippet: $(document).ready(function() { $(document).on('click', '#search_button', function() { ...

Directly transfer image to Cloudinary without creating a local file

I have integrated jdenticon to create user avatars upon signup in a node/express app. When working locally, I follow these steps: Create identicon using jdenticon Save the file on my local machine Upload the local file to cloudinary Here is a snippet o ...

Conceal the menu in Angular Material when the mouse leaves the button

When the mouse hovers over a button, a menu is displayed on the website's toolbar. The menu is set to close when the mouse leaves a surrounding span element. Now, there is an attempt to also close the menu when the mouse leaves the triggering button i ...

Extract information from a webpage using Selenium WebDriver

Currently, I am working on mastering Selenium, but I have hit a roadblock that I need assistance with. My task is to gather all the betting information for games from the following link and store it into an array. Given my limited experience with HTML an ...

How to use multiple template urls in Angular 6

Currently, I am creating a front-end using Angular 6 and facing the challenge of having components with varying html structures based on the user who is logged in. The number of templates required can range from 2 to over 20, so my preference would be to ...

Retrieve data from the MySQL database based on the search input field and dynamically update the options in the

I'm facing a programming challenge that I perceive to be at an advanced level for me. Currently, I have a custom search field in my registration form. However, I am looking to transform it into a dropdown menu that pulls user values from MySQL databas ...

Vue: Duplicating a div element with unique input fields upon clicking

I have successfully set up a page where I can dynamically add a div/row of inputs by clicking the link. This feature works smoothly and adds the same div below the current one with all the proper inputs. Additionally, one of the inputs is designed as an a ...

Performing addition operations on numbers entered through an HTML input field using PHP

I am looking to create a feature where the numbers entered in an input form are added together. I need to store these numbers in an array and have them display in a new line when a button is clicked. Here is the HTML code for the input field and button: ...

Submitting information via jQuery

https://i.stack.imgur.com/VU5LT.jpg Currently, I am working on integrating an event planner into my HTML form. However, I am facing difficulty in transferring the data ("meetup", "startEvent", "break") from HTML to my database. Using jQuery, I was able to ...

Is extracting the title of an image from Flickr?

I have a JavaScript code that randomly pulls single images from Flickr every time the page is refreshed. Now, I want to add a feature where the title of the image is displayed when it's hovered over. However, I've been searching for a solution fo ...

The exportAs attribute is not specified as "ngForm" for any directive

Encountered an error while attempting to test the LoginComponent PhantomJS 2.1.1 (Linux 0.0.0): Executed 3 of 55 (1 FAILED) (0 secs / 0.307 secs) PhantomJS 2.1.1 (Linux 0.0.0) LoginComponent should create FAILED Failed: Uncaught (in promise): Error: Templ ...

Leveraging Angular recommendations within HTML connected to a controller in PhpStorm

I am currently using phpStorm and I'm wondering if it is possible to bind an HTML template to a controller in order to receive proper suggestions. Within my router, I have set up a template HTML and controller: $stateProvider .state('ap ...