Filtering select options dynamically in Angular

Utilizing an ng-repeat to iterate through objects for display in Angular. Each object includes a select element with a property of the object.

The goal is to filter options based on the selected value for each object's other properties. The challenge arises with Breeze entities as using the standard method provided by Angular causes a stack overflow exception due to their cyclic nature.

While Ward's example offers a static function for filtering, the aim is to make it more dynamic but facing difficulties -

The view presents a list of available fighters, filtered by weight class for each fight within the ng-repeat's fights. Includes a weight class selector and two fighter selectors per fight -

Attempt 1 -

<select
    ng-model="fight.firstFighter"
    ng-options="f.fullName for f in fighters | filter: fighterFilter">
</select>

<select
    ng-model="fight.weightClass"
    ng-options="w.fullName for w in weightClasses">
</select>

$scope.fighterFilter = function (fighter) {
    var fight = ???;
    return fight.weightClass ?
        -1 != fighter.weightClass === fight.weightClass :
        true;
};

Attempting to pass no parameter as shown leads to only sending the fighter’s value during iteration, thus unable to extract the fight.weightClass value.

Seeking input on how to access the context of both fight and the iterating fighter? Or better approaches to this type of filtering?

Fighters Structure

  1. Fighter

    • Id
    • Name
    • WeightClassId
    • WeightClass (navigation property)
  2. Fight

    • FirstFighter
    • SecondFighter
    • WeightClassId
    • WeightClass (navigation property)
  3. WeightClass

    • Id
    • Weight
    • Description
    • FullName

Edit

Able to successfully filter results for a single fight. Challenge lies in dynamically handling this on a per-fight basis within the same view under the ng-repeat directive. Unable to access 'fight' and 'fighter' contexts to compare whether the 'weight class' values of both entities match.

Answer №1

Using an expression as an Object in the form of {fieldName:value}, Filter is capable of filtering data.

To achieve your desired outcome, you can utilize

| filter: {WeightClassId:fight.WeightClass.Id}
.

<li ng-repeat="fight in fights">
    <h4>{{ fight.number }}</h4>

    Weight Class:
    <select ng-model="fight.WeightClass" ng-options="w.Name for w in weightClasses"></select>{{ fight.WeightClass.Name }}

    <br/>First Fighter
    <select ng-model="fight.FirstFighterId" ng-options="f.Name for f in fighters | filter: {WeightClassId:fight.WeightClass.Id}"></select><span>{{ fight.FirstFighter.Name }}</span>

    <br/>Second Fighter
    <select ng-model="fight.SecondFighterId" ng-options="f.Name for f in fighters| filter: {WeightClassId:fight.WeightClass.Id}"></select><span>{{ fight.SecondFighter.Name }}</span>
</li>

Check out the Demo here

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

What is the process for transforming a JSON object format to another in node.js?

I found myself with a json object structured as follows: { "sample1": { "4": 2245, "5": 2175, "6": 3495, "7": 1845, ... "11.5": 1674, "12.5" ...

How to Disable Checkbox in Ionic Framework's Ion-List

Feel free to check out my Codepen project. Within this demonstration, I am working with two list arrays - one called items and the other items1. My goals are: To display a list where certain items from the items array are already checked based on the c ...

How can I make one object's position target another grouped object in three.js, while still enabling rotation to track a camera in augmented reality?

Currently, I am utilizing a cutting-edge augmented reality library that specializes in advanced image tracking capabilities. Despite extensively studying this project, I have reached a point where I require assistance. Essentially, the library generates an ...

Retrieve information from a template and pass it to a Vue component instance

Being a newcomer to vue, I have a fundamental question. In my template, I have a value coming from a parsed object prop like this: <h1>{{myval.theme}}</h1> The above code displays the value in the browser. However, I want to store this value i ...

Improve the translation animation on an element containing numerous child nodes

Looking for ways to enhance the smoothness of the transition in the "infinity list" animation. While it's just a demo at the moment, the real app will have various elements emerging from each "pin". The main performance bottleneck seems to stem from t ...

Center both vertically and horizontally in Bootstrap UI Modal

I'm attempting to create a Bootstrap UI Modal that is centered both vertically and horizontally. While I found this solution that successfully centers the modal vertically, it loses its horizontal centering when applied to my own template with 800px ...

Is there a way in jquery or any other method that specifically detects changes only in direct child nodes?

I came across a jQuery method called DOMSubtreeModified that can detect the insertion of child nodes within a specific DOM. However, I noticed that this method detects the insertion of all child nodes, not just the immediate ones. Is there a jQuery met ...

Dynamic web page updates from server using an Ajax request

I have a JavaScript client application and an Express.js server. I am looking to update a page on my server with information sent through an AJAX call from my client application. I need the page to be updated in real-time. Here is the code snippet in my ...

Implementing Google Calendar access token in JavaScript: A practical guide

I have a question about Google Calendar and I'm hoping you can assist me. I currently have an access_token from Google Calendar that has been stored in the localStorage. const googleAccessToken = e.vc.access_token; localStorage.s ...

What is the best way to display SVGs from an API response using next/image or the <img> tag in Next.js?

I have a collection of SVGs being returned from an API response, which I am fetching inside the useEffect. The response structure is as follows: {svg1: 'https://remoteserver.com/assests/svg1.svg', ...and so on} (These SVGs are not static assets ...

The text link on an iOS device is trapped by a persistent blue border

I've encountered an issue on my iOS device. When I open the menu with a popup, there is a blue border around the text element, even though it's not a hyperlink underline. https://i.sstatic.net/WA3rz.jpg I attempted to resolve this using the fol ...

Automatic cancellation of AJAX request

Having a strange issue that I need help with. I am using a standard ajax call to upload avatars from the user's PC to the server. However, sometimes I notice in Firebug that the request is being "Aborted" and marked in red after loading for some time ...

Choose a HTML element <a> containing a hyperlink reference and a nested child element <li>

Does anyone know of a way in javascript/css to select the (n) <li> in my code, while also specifying that the <li> must have a parent with the current <a> tag containing a specific href link? <a href="www.link.com">CATEGORY NAME& ...

The application is unable to locate the installed packages

I have successfully installed formik and yup by running npm install formik and npm install yup. Both packages are listed in the dependencies object of package.json. In my file, I import them as follows: import * as Yup from 'yup'; import { For ...

Reposition a child directive to a different location

I am currently developing a set of Angular directives that function as a unique type of list. One directive acts as the parent, while the others represent individual items within the list. The challenge I am facing involves extracting one specific child it ...

"Enhancing user experience with JQuery and MVC4 through efficient multiple file uploads

Currently, I'm facing a challenge in uploading multiple files alongside regular form data. While I had successfully implemented this functionality in PHP before, I am now attempting to achieve the same in ASP.NET MVC4 using C#. Below is the HTML form ...

Switching to version 2 of date-fns: Not a Number

I am currently in the process of updating the date-fns module from version 1 to version 2. Prior to the update, the following helper method functioned as expected: const { format, parseISO, differenceInSeconds } = require("date-fns"); const new ...

Implementing GroupBy functionality in MongoDB queries using Mongoose and/or underscore.js

Our database contains a collection called messages that is structured as shown below... /* 0 */ { "text" : "A message to John", "created" : ISODate("2015-01-29T23:50:10.488Z"), "to" : { "id" : ObjectId("54bf1dc6c65b030c00faec0d"), "name" : " ...

Passing data from getServerSideProps to an external component in Next.js using typescript

In my Index.js page, I am using serverSideProps to fetch consumptions data from a mock JSON file and pass it to a component that utilizes DataGrid to display and allow users to modify the values. export const getServerSideProps: GetServerSideProps = async ...

How to interact with an Angular controller through the developer console in Chrome or Firefox

I have taken a screenshot showing the specific click action I am trying to execute from the Chrome console, along with different values being passed. The button highlighted in gray at the top right can be found by inspecting the element. I am specifically ...