Here are the steps to pass the selected dropdown value to ng-repeat for ordering:

I have a dropdown box with options for filtering data in an ng-repeat by different criteria. How can I pass the selected value from the dropdown to the ng-repeat orderby?

Here is my dropdown:

<select name='filter_range' id='filter_range' onchange='filter()'>
    <option value='mrplow'> MRP Low To High</option>
    <option value='mrphigh'> MRP High To Low </option>
    <option value='qtyhigh'> Qty Low To High </option>
    <option value='qtylow'> Qty High To Low </option>
</select>

Using AngularJS:

<div ng-app="myApp" ng-controller="MyController"> 
    <div ng-repeat="grp_val in sample | orderBy:selectedValue">
        <label>{{grp_val.product}}</label><br>
    </div>
</div>

How can I dynamically set the orderBy value to the one selected in the dropdown?

Answer №1

Let me demonstrate an example that closely resembles your issue:

    const fruits = ['apple', 'banana', 'cherry'];
    console.log(fruits.sort()); // Output: ["apple", "banana", "cherry"]
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.min.js"></script>
<div id="app">
  <p>Original Array: {{fruits}}</p>
  <p>Sorted Array: {{sortedFruits}}</p>
</div>

<script>
new Vue({
  el: '#app',
  data: {
    fruits: ['apple', 'cherry', 'banana']
  },
  computed: {
    sortedFruits() {
      return this.fruits.sort();
    }
  }
});
</script>

You can find more information at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

I hope this sheds some light on your query.

Answer №2

If the data you are working with contains properties like "mrp" and "qty", this solution should help resolve your issue:

<select ng-model='orderProp' name='filter_range' id='filter_range'   onchange='filter()'>
    <option value='mrp'> MRP Low To High</option>
    <option value='-mrp'> MRP High To Low </option>
    <option value='qty'> Qty Low To High </option>
    <option value='-qty'> Qty High To Low </option>
</select>

<div ng-app="myApp" ng-controller="MyController"> 
    <div  ng-repeat = "grp_val in sample | orderBy: orderProp"
        <label>{{grp_val.product}}</label><br>
    </div>
</div>

Make sure to adjust the option values of your select box based on the properties of your "sample" object, then connect the select box value to "orderProp" which will be used in the ng-repeat directive with the orderBy filter.

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

The request to sign up at 'https://identitytoolkit.googleapis.com/v1/accounts:/signUp? from the origin 'http://localhost:8080' has been denied

My attempt to create a new user in Firebase using Axios in Vue.js is resulting in an error message regarding CORS policy. The specific error states: "Access to XMLHttpRequest at 'https://identitytoolkit.googleapis.com/v1/accounts:/signUp?key=AIzaSyDvZ ...

Can a function be appropriately utilized in a reducer?

For my reducer, I am looking to incorporate a function that is necessary for sorting. I have successfully imported this function from another file and it seems to be functioning correctly. However, I am unsure about the validity of importing and using fu ...

A step-by-step guide on recovering information from a JSON file within Angular 12

Being a novice developer, I have taken on the challenge of creating a quiz application using Angular 12 to enhance my coding skills. However, I've hit a roadblock when it comes to retrieving data with questions and answers from a JSON file. Despite su ...

Utilizing a jQuery AJAX request to invoke a web method within a

My goal is to integrate jQuery mobile into an existing ASP.NET webform application. I am currently considering using pure HTML controls to create the jQuery Mobile page. I am aware that when making an AJAX call, I can access code-behind static web methods, ...

How to incorporate an icon into a React Bootstrap Dropdown menu

I'm struggling to figure out how to include an icon in my react dropdown button, similar to the one shown in the following example. https://i.sstatic.net/cn0b0.png The react bootstrap package I am currently using does not seem to offer a straightfor ...

Tips for substituting commas and slashes within an input text box

For instance, if the input is "1,23/456", the output should be "123456". When "1,23/456" is entered into the input field and "enter" is pressed, it should automatically convert to "123456". <input id="Id" ng-model="Id" name="searchInput" type="text"&g ...

Is it possible to compare every element in an array with one another using an asynchronous process in NodeJS?

I am currently utilizing Resemble.js for image comparison within my web application. I have an array of image URLs that I need to compare with each other in order to calculate a unique score for each image. For example: [url1, url2, url3, url4] The minimu ...

Opening a Text File via an ASPX Web Page

Currently, I am immersed in a Web Pages project. The goal is to retrieve text from a Text File and display it within a div element. To achieve this, I utilized the ajax xmlhttprequest method, following a tutorial available at http://www.w3schools.com/ajax/ ...

Node.js encountered an issue: Dependency 'mime-types/node_modules/mime-db' cannot be located

Recently, I followed a tutorial on creating a CRUD App with Nodejs and MongoDB. The project was completed successfully and everything was working fine. However, when I attempted to move all the files and folders to a new directory, disaster struck. Now, w ...

Tips for generating table headers in a dynamically adjusted table with AngularJS

I am having difficulty creating a table with dynamic height and width, specifically with the table header. The <td> elements are displaying properly using this ng-repeat: <tr ng-repeat="set in currFormData.volume track by $index"> <td ng ...

Efficiently handling multiple form submissions using a single jQuery Ajax request

I'm working on a page that has 3-4 forms within divs, and I want to submit them all with just one script. Additionally, I want to refresh the content of the specific div where the form is located. However, I'm unsure of how to specify the current ...

Transferring information between different AngularJS components

I'm struggling to connect two sibling components in AngularJS without relying on a service. I've looked at examples, but I just can't seem to get them to work. Here's what I have. Where am I going wrong? Thank you! cluster.js <div ...

Experiencing a disappearing session on Express.js happens approximately every 3 minutes (I can relate to this problem as

I am encountering a similar issue to the question mentioned here: Express.js session lost after about 3 minutes Unfortunately, I have not been able to find a solution yet. This is my Session Code: app.use( session({ secret: 'secret', resave ...

Ways to modify an object similar to manipulating an array collection

Hey there, I've heard that iterating and editing objects (not arrays) is not the best practice. Is there a more efficient way to do it as easily as it can be done with an array of objects? Check out this link for reference new Vue({ el: '#app ...

Something is amiss with the PHP email validation functionality

I have been facing issues with the functionality of my form that uses radio buttons to display textboxes. I implemented a PHP script for email validation and redirection upon completion, but it doesn't seem to be functioning correctly. The error messa ...

Verifying email addresses through JavaScript and an activation process

I am in the process of implementing email confirmation/verification for my Login & Registration feature. I came across Activator on github, which claims to be a straightforward solution for managing user activation and password reset in nodejs apps (http ...

"Preventing the propagation of a click event on an anchor tag with

Is there a way to prevent the parent anchor from executing its href when a child element is clicked, despite using stopPropagation? Here is the markup provided: <div id="parent"> <h5>Parent</h5> <a id="childAnchor" href="https:// ...

Persist the data retrieved from an asynchronous function in the memory

I am faced with a challenge in Node.js where I need to store an object in memory. This particular object is created asynchronously from an API call. The issue at hand is that previously, this object was synchronous and many parts of the application were de ...

Steps to Create a Repeating Melody/Instructions for Implementing a Loop Function

Currently, I am working with discord.js and have had success implementing various commands. However, I am having trouble creating a loop command for my discord bot. This specific command should play a song repeatedly until the loop command is executed ag ...

Iterate over each option in a select dropdown menu and modify them

Below is the test code snippet I am working with: ID = { CreatedBy: { id: 'createdBy' }, ModifiedBy: { id: 'modifiedBy' } } Profile = { All: { text: 'All', val: 0 }, Sys: { text: '<a href="/cdn-cgi/l/emai ...