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

Setting the row ID value after performing form editing in free jqGrid

In the table, there is a primary key with 3 columns (Grupp, Kuu, Toode), and the server returns an Id created from those columns. When the primary key column is changed in form editing, the server sends back a new row id. However, Free jqgrid does not se ...

One limitation is that you cannot use JQuery to make multiple rows editable simultaneously

I have a unique challenge with implementing an edit button on each row of a dynamic Bootstrap table. I am attempting to toggle the button's icons and, depending on its current state, enable the corresponding row for editing. <td><button typ ...

Failure to update the DOM after clicking a button and making an AJAX request

After experimenting with different iterations of this code, the outcomes have been mediocre and inconsistent. The concept is rather simple: A user inputs a search query into a text box, clicks a button, and the query is assigned to a var search. This varia ...

Running only failed tests in Protractor can be achieved by implementing a custom script that

I've encountered a problem in my JavaScript file where some of the test cases fail intermittently, and I want to rerun only those that have failed. Is there any feature or solution available that can help with this issue? It's quite frustrating a ...

The JS Fiddle code fails to function properly once it has been downloaded to the local computer

I came across a helpful example fiddle webpage: jsfiddle.net/yijiang/6FLsM/2 After following examples from others, I attempted to download (right click and Save As) using the latest Chrome browser with the following links: jsfiddle.net/yijiang/6FLsM/2/s ...

Check if the element is not present in the array, then add it to the array

I am attempting to generate an array in JavaScript with unique elements extracted from a JSON feed. My thought process is possibly too influenced by PHP, where a simple script like the one below would suffice: $uniqueArray = array(); foreach($array as $ke ...

What could be causing the issue of the view not showing up in AngularJS?

I've been trying to use AngularJS modules to display a view, but for some reason my page isn't showing up. Can anyone help me figure out what's going on? You can check out my code at the following link: <!DOCTYPE html> <html> & ...

The npm package for google-spreadsheet.js is experiencing issues and is not functioning correctly when trying to replicate the GitHub

After attempting to implement the basic example code provided at https://github.com/theoephraim/node-google-spreadsheet, I encountered an issue. For instance: const { GoogleSpreadsheet } = require('google-spreadsheet') const creds = require(&apo ...

Utilize dynamic CSS application within an Angular application

In my Angular application, I dynamically load URLs inside an iframe and want to apply custom.css to the loaded URL once it's inside the iframe. I attempted using ng-init with angular-css-injector function in the iframe, but the CSS is not being applie ...

How can I make a page scroll to the center when clicking on a carousel?

My goal is to create a webpage where clicking on the carousel will center it on the page. A great example of this functionality can be seen on the Studio New Work website (). As I am still in the process of learning JQuery, I have not yet mastered all the ...

Utilizing AngularJS for seamlessly closing Bootstrap Modal Popup

Having recently delved into AngularJS, I am seeking assistance with a particular issue; In my AngularJS (v 1.6) application, there is a Bootstrap modal window for user login. Once the user successfully logs in, I wish for Angular to automatically close the ...

Retrieve the ID using AngularJS and save it to resources

As a newcomer to AngularJS, I have a query regarding saving Tasks using a resource. When the Task is saved, it correctly sends the information to the database and the server responds with the ID of the created Task after receiving the POST request. The co ...

Toggle the display of data in an angular table with a button click

Having trouble implementing a sorting feature in Angular where I can filter friends by age. The goal is to be able to click on one of three buttons with ages and have only friends with that age displayed. For example: Initially, all friends are shown. Cl ...

An alert box displays N times when the submit button is clicked N times

Consider the following function: function validateForm() { var elements = ["firstname", "lastname", "password", "favfood", "favsport"]; document.getElementById('register').setAttribute('noValidate', true); document.getElement ...

Extracting values from PHP using AJAX

Whenever a user clicks on the download button, a zip file is successfully created on the server with the necessary files. However, instead of getting an alert with the location of the zip variable ($zip) from PHP as expected, it shows [object Object]. The ...

The synergy of Sails, Angular, and Jasmine combined creates a

Utilizing SailsJS for the backend and Angular for the frontend has been a successful combination thus far. I have already implemented Mocha tests for the backend logic in SailsJS and now I am looking to add some tests for the frontend Angular code. Howeve ...

Is there a specific regular expression that can be used for validating Credit Card Expiry dates in Javascript/Ang

I am currently working on a textfield where users can enter their credit card expiry date in the format mm/yy. To ensure the validity of this input, I have implemented JavaScript validation using regular expressions. Here is an example of what I have tried ...

What is the reason for the addEventListener function not being able to access global variables?

I have set up an event listener function to utilize popcorn.js for displaying subtitles. Additionally, I have created functions that are unrelated to popcorn.js outside of the event listener and declared a global variable array. However, when attempting ...

How to Capture Clicks on Any DOM Element in React

Currently working on a web project using React and Node. My goal is to monitor all clicks across the entire document and verify if the previous click occurred within a 2-minute timeframe. ...

Can a `react` app with `mysql` be uploaded to `github`?

I recently created a basic online store with the help of react, node, and mysql. I am considering uploading it to github, but I'm uncertain if I can do so while my database is currently stored on localhost. Any advice? ...