Filtering a subarray in Angular using ng-repeat

While working with Angular, I am attempting to filter data using ng-repeat based on the FactorName property in the given schema.
Unfortunately, trying to use <... ng-model="query.Factors.FactorName" ...> as a filter does not produce the desired results. It seems like filtering this way is uncommon based on my research.

My question is: Can Angular support filtering based on a property of elements within a subarray?

[
   {
      "Name":"1",
      "Factors":[
         {
            "FactorName":"FactorOne",
            "Type":"SomeType"
         },
         {
            "FactorName":"FactorTwo",
            "Type":"SomeType"
         }
      ]
   },
   {
      "Name":"2",
      "Factors":[
         {
            "FactorName":"FactorThree",
            "Type":"SomeType"
         },
         {
            "FactorName":"FactorFour",
            "Type":"SomeType"
         }
      ]
   }
]

Answer №1

If you need to iterate over an array while using ng-repeat and a custom filter, there is another way to do it.

Check out the working demo here

HTML Code

<ul ng-repeat="x in factorData">
  <li>
    {{ x.Factors | factorFilter }}
  </li>
</ul>

JavaScript Code

app.filter('factorFilter', function() {
    return function(items) {
        var results = [];
        if (items) {
            for (var i = 0; i < items.length; i++) {
                results = items[i]['FactorName'];
            }
            return results;
        } else {
          return 'Doh!';
        }
   }
})    

Answer №2

I managed to create a filter for all the elements in the Factors array, but I couldn't quite figure out how to only select one specific field from the array. Maybe it will work better for you:

<input ng-model="filtervalue.Factors"/><br/><br/>
Filtered:
<p><span ng-repeat="d in data | filter:filtervalue:false">{{d}}</span></p>

Here's how you can initialize it in JavaScript:

$scope.filtervalue = {};

You can also check out a sample on jsfiddle here:

http://jsfiddle.net/jordiburgos/QFUu6/

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

How can Angular be used for live search to provide precise search results even when the server does not return data in the expected sequence?

Currently, I am in the process of constructing a website for one of my clients. The search page on the site is designed to update search results as you type in the search bar - with each keystroke triggering a new search request. However, there's an i ...

jqueryajax function returns a boolean value upon completion

Recently, I developed a container method to handle an ajax request: function postRating(formData) { $.ajax({ type: "POST", url: '/api/ratings', data: formData }) .done(function () { return true ...

Unable to display MongoDB collection in React application

I've been working on showcasing my projects using React and Meteor. I have two collections, Resolutions and Projects. The issue I'm facing is that while I can successfully display the Resolution collection on the frontend, I'm struggling to ...

Guide on using jQuery to dynamically update a section of an ejs template following an AJAX request in ExpressJS

I'm currently struggling to figure out how to dynamically update a portion of the DOM using EJS after a jQuery ajax call. The issue arises with a live search feature that successfully sends a request to the server, searches the database, and returns a ...

Troubles with creating promises in Node.js

Currently, I am facing a challenge in Nodejs where I need to execute asynchronous tasks. Specifically, I need to navigate through all levels of a JSON object sequentially but synchronously. I am experimenting with the following code snippet, which is a si ...

Building a Modal in React and Triggering it for Display, followed by Making an AJAX Request upon Submission

Check out my CodePen snippet (minus delete ajax request): http://codepen.io/martincarlin87/pen/KzPWOw I've been diving into the world of React recently, and I'm working on converting a page in my web application to utilize React instead of just ...

What is the best way to add all the items from an array to a div element?

I am currently facing an issue where only the last object in my array is being added to my div using the code below. How can I modify it to add all objects from the array to my div? ajaxHelper.processRequest((response: Array<Vehicle.Vehicle>) ...

Incorporate a JavaScript variable within the src attribute of a script tag

Embedding Google's script allows for displaying a trends Map on our website. <script type="text/javascript" src="//www.google.com.pk/trends/embed.js?hl=en-US&q=iphone&cmpt=q&content=1&cid=TIMESERIES_GRAPH_0&export=5&w=500&a ...

Retrieving JSON information using Angular from JSONPlaceholder

Obtaining the JSON data from http://jsonplaceholder.typicode.com/ using Angular seems quite simple, yet I am struggling to find the right method. Can anyone provide guidance on how to achieve this? I have already attempted multiple approaches with no suc ...

Tips for avoiding a button reverting to its original state upon page refresh

I have a button with the ID #first that, when clicked, is replaced by another button with the ID #second. However, if I refresh the page after clicking on the second button, it goes back to displaying the first button. Is there a way to make sure that th ...

Design a recurring report using an endless JavaScript cycle

I am in the process of creating a brand new scheduled report and have encountered an issue. How can I incorporate a script that includes a loop to run a specific function every 10 seconds? Here's what I have so far: var count = 1; while(count > 0 ...

What is the process for creating a symbolic link from a project's node_modules directory?

Recently, I've been learning how to build a Password Manager using React, Node.js, and MYSQL by following a tutorial. However, I encountered an issue where the file /EncryptionHandler was located outside of the src/ directory of my project. Even thoug ...

Retrieve JavaScript functions as JSON or text in AngularJS and execute them

Currently, I am working on developing an AngularJS application and have come across a particular challenge that needs to be addressed: After making a web service call, I receive JavaScript code that looks like this: { "script":"function foo(a, b) { c = ...

Troubleshooting issues with ng-options not correctly updating ng-model in AngularJS when using ajax requests

I have a question regarding my logic that I can't seem to figure out. In this Fiddle example, everything works fine without using AJAX or a timeout. However, when I try to implement the same logic with a timeout/ajax, the expected behavior does not o ...

An error occurred while trying to access the property 'send' which is undefined

I am currently facing an issue while attempting to send data to the router from an external script. The error message that I receive is TypeError: Cannot read property 'send' of undefined Below is the code snippet: app.js var Cake = require(&a ...

What is the method for determining the level based on the provided experience points?

I've created a formula that can calculate experience based on specific levels and another formula that calculates the level based on given experience. However, there seems to be an issue with the second function as it is not returning the expected val ...

What is the best way to define the active <li> tab using JQuery?

Initially, my HTML code includes a set of <li> elements that I want to dynamically add to the existing <ul>. <ul class="tabs" id="modal_addquestion_ul"> <li class="tab-link current" data-tab="multipleChoice">Multiple Choice< ...

Protractor problem: difficulty scrolling into an infinite scroller

For a protractor test, I am attempting to find a record in my infinite scroller component using the code below: searchPage.searchEntitlement('search criteria'); var myHiddenElementInScroller = element(by.repeater('result in ctrl.result ...

ajax is triggering the error handler

I am currently developing a web application and I am trying to send data from a PHP controller to JavaScript. After conducting some research, it seems that the most efficient way to accomplish this is by utilizing Ajax and Json. However, I am encountering ...

Timeout function is failing to trigger the digest cycle

When calling a chrome function that runs asynchronously, the digest cycle needs to be run in order to update scope variable values in the view. While attempting to achieve this with $timeout and $evalAsync, I encountered issues. Directly calling $scope.$ap ...