What is the best way to monitor the elements within ngRepeat in AngularJS?

Hello everyone,

I am facing a minor issue that I seem unable to resolve. I have a search input field that allows users to search through a table in all columns. However, I am struggling to track the items in ngRepeat effectively.

Below is the search input field:

<div>
  <input type="search" ng-model="searchText" />
</div>

And here is the table using ngRepeat:

...
<tr ng-repeat="item in filtered = (persons | filter:searchText track by item.name)">
   <td>{{ item.id }}</td>
   <td>{{ item.name }}</td>
</tr>
...

If anyone has a solution to my problem, I would greatly appreciate it! :)

Answer №1

It seems that you don't need to use track by, as simply applying the filter directive should suffice

<tr ng-repeat="item in persons | filter:{ name: searchText} ">

angular.module('app', [])
  .controller('ctrl', function($scope) {
    $scope.persons = [{
      name: 'a',
      id: 1
    }, {
      name: 'aa',
      id: 2
    }, {
      name: 'aaaa',
      id: 3
    }, {
      name: 'aaaaa',
      id: 4
    }, {
      name: 'aaaaaa',
      id: 5
    }, {
      name: 'aaaaaaa',
      id: 6
    }];
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <div>
    <input type="search" ng-model="searchText" />
  </div>
  <table>
    <tr ng-repeat="item in persons | filter:{name:searchText}">
      <td>{{ item.id }}</td>
      <td>{{ item.name }}</td>
    </tr>
  </table>
</div>

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

Preventing ngclass animation in Angular 1.2 from running when re-compiled

Example: http://codepen.io/anon/pen/ixEdu I am currently working on an angular animation using ngClass. In the scenario where the element containing the animation is re-compiled, the animation triggers again. Is there a way to prevent this unwanted behavi ...

Guide on setting up global typing for AngularJS in your project

I have been working on a project that initially used the deprecated typings method for incorporating Typescript definitions. I now want to transition to using the @types method instead. Currently, we have a typings.json file located in the root of the pro ...

Adding a dynamic key-value pair object to an array using the JS push method

In my programming challenge, I am dealing with an array of objects that have dynamic keys and values. The structure is a bit complicated, but here's a simplified version: let products_row = [ { id: 1, category_id: 11, options: { &a ...

Select from a list to save

My goal is to create a feature where users can select a hotel name, number of days, guests, and peak time, the system will calculate them together and give a sum. Furthermore, I wish to store all user selections in the database, including the calculated to ...

JavaScript comparing variables to nested object properties

items resembling this are heading my direction: var info = { a0: { name: 'lengthy title 0', var1_min: '10', var1_max: '99', select: ['alpha', 'gamma'], display: 'value0' }, b12: { ...

How to visually deactivate a flat button ( <input type="button"> ) programmatically with JavaScript

I am facing an issue with my buttons. I have one regular button and another flat button created using input elements. After every click, I want to disable the buttons for 5 seconds. The disable function is working properly for the normal button, but for th ...

Achieving a collapsing navbar on click in Bootstrap 5

Is there a way to collapse this navigation bar after clicking on a link, without using a JavaScript event listener or the data-bs-toggle and data-bs-target methods mentioned in this article? I tried both methods but they are not working with my code. Here ...

Troubleshooting three.js exceeding 2-minute load times and crashing issues

I am currently in the process of developing a 3D game and have encountered an issue with the localhost GET request taking longer than expected. After around 15-45 seconds, the canvas turns white and I receive a warning in the console indicating that the We ...

The privateroute is throwing an error because it cannot access the property state since

I stumbled upon a tutorial online detailing how to construct a customized private route, but alas, I am encountering an error. Upon execution, I am faced with the dreaded message: "Cannot read property 'state' of undefined" on line 12. As someon ...

spontaneous angular rotations in a constantly shifting CSS environment

Is it possible to apply a random CSS rotation to an image just once, rather than having it continuously spin when hovering over a leaflet map? http://jsfiddle.net/J03rgPf/mzwwfav4/3/ I want the random CSS rotation to be set only one time. How can I achie ...

The confirm() function shows up before the background on a blank layout

I'm facing an issue where whenever my confirm() function pops up, the alert box displays correctly but then the background turns blank. I've tried various solutions like moving the entire if statement to the bottom of the page or at the end of th ...

Loop through the elements retrieved by the `getElementsByName` method in

My goal is to access each node by elementName using the following JavaScript code: function myFunction() { var h1 = document.getElementsByName("demoNode"); for (var i = 0; i < h1.length; i++) { if (h1[i].name == "demoNode") { var att ...

Oops! There seems to be a problem with inserting an image using

I am trying to insert multiple images with a loop when clicked. The images are named "0.jpg","1.jpg","2.jpg" etc. To achieve this, I am using an array with all the elements: {name: '1', result: 1, prefecture: "city", photo1: "0.jpg"}, and my l ...

The Problem with AJAX Error Handling Customization

Upon loading my webpage using Code Igniter and PHP, the JSON encoded query is returned successfully with data. I have managed to handle the scenario where no records are found by encoding a response in JSON format. However, I am unsure of how to transmit t ...

Leveraging route configuration's scope in HTML

As a beginner in AngularJs, I am currently exploring the creation of a single page application. However, I am encountering difficulties in converting my initial code into more professional and efficient code. During this conversion process, I have separate ...

Tips for identifying a scroll down gesture on a touch device with jQuery

I have a method for infinite scroll that is triggered like this: $(window).on('scroll resize load', function() { // execute the infinite scroll method }); It is triggered by these events: scroll resize load However, it does not seem to ...

What is the best way to assign three different dates in my protractor test?

I am facing an issue with setting random dates in 3 date fields in a row using Protractor. The problem is that Protractor sets the dates too quickly and sometimes assigns invalid dates like: first data: 01/08/1990 (correct) second data: 01/09/0009 (inva ...

Encountering Error 1 in Cordova File Transfer with Laravel Framework

I'm currently developing an Ionic angular 1 application where I need to upload an image to my laravel api server. However, I keep encountering error code 1 (File not found?) along with a laravel PHP error message as the response body. I've tried ...

Animate images using mouse vertical movement

Creating websites is just a hobby for me, not something professional. Recently, I came across a beautifully designed website that had some unique features I hadn't seen before, like placing three images in one div (which I researched and learned how t ...

How can I implement disabling buttons for specific IDs in React?

I'm currently developing an interactive quiz application with React that features multiple choice questions. I've implemented state management to track and increment the user's score when they select the correct answer option, but there&apos ...