Issue with filtering (ngRepeat) in a table across multiple columns is not fully functional

I am looking to apply a filter on my table that is created using ng-repeat.

        <tbody>
            <tr ng-repeat="x in contact.listeContacts | filter:contact.searchText track by $index">
                <td>
                    <b>{{x.gd$name.gd$fullName.$t}}</b>
                </td>
                <td>
                    <p>{{contact.listeContacts[$index].gd$name.gd$familyName.$t}}</p>
                </td>
                <td>
                    <p>{{contact.listeContacts[$index].gd$name.gd$givenName.$t}}</p>
                </td>
                <td>
                    <p>{{contact.listeContacts[$index].gd$email[0].address}}</p>
                </td>
                <td>
                    <p>{{contact.listeContacts[$index].gd$phoneNumber[0].$t}}</p>
                </td>
                <td>
                    <p>{{contact.listeContacts[$index].gd$organization[0].gd$orgTitle.$t}}{{contact.listeContacts[$index].gd$organization[0].gd$orgName.$t}}</p>
                </td>
            </tr>
        </tbody>

My concern is that the filter I applied appears only on the first column of the table.

Let's consider these three objects for display:

obj1 = {
'name':'obj1',
'age':10
}

obj2 = {
    'name':'obj2',
    'age':1000
}

obj3 = {
    'name'='obj3',
    'age':100000
}

If my searchText is "obj3", the table shows something similar to this obj3 / 10

instead of obj3 / 100000

I am puzzled about why it is behaving this way.

EDIT: John Joseph, Xun Chao & tanmay assisted me in resolving my initial problem, you can refer to their answers.

Currently, I'm facing an issue where my search filter does not work as intended. Please take a look at this Plunker : https://plnkr.co/edit/SfaYdgVkSfhOdI5enpOd?p=preview

Answer №1

Although the answers provided are accurate, none have delved into the reason behind this occurrence. Filtering with "obj3" results in only one item being displayed in the ng-repeat, even though your contact.listeContacts array still contains a length of 3. Thus, when searching for the text of the third item (in this case, obj3), it attempts to display contact.listeContacts[0] because the filtered array consists of just one element.

There are two possible solutions:

  1. Following the advice from previous responses, substitute x for contact.listeContacts[$index]
  2. You can create a reference to the filtered array like so:

    <tr ng-repeat="x in mycontacts = (contact.listeContacts | filter:contact.searchText) track by $index">

    Replace contact.listeContacts[$index] with mycontacts[$index], which references the filtered array rather than the original array

Answer №2

Instead of accessing the contact.listeContacts directly for the rest of the columns, try using the looped variable x. This could be causing the issue. Make the following change: replace contact.listeContacts[$index] with just x.

    <tbody>
        <tr ng-repeat="x in contact.listeContacts | filter:contact.searchText track by $index">
            <td>
                <b>{{x.gd$name.gd$fullName.$t}}</b>
            </td>
            <td>
                <p>{{x.gd$name.gd$familyName.$t}}</p>
            </td>
            <td>
                <p>{{x.gd$name.gd$givenName.$t}}</p>
            </td>
            <td>
                <p>{{x.gd$email[0].address}}</p>
            </td>
            <td>
                <p>{{x.gd$phoneNumber[0].$t}}</p>
            </td>
            <td>
                <p>{{x.gd$organization[0].gd$orgTitle.$t}}{{x.gd$organization[0].gd$orgName.$t}}</p>
            </td>
        </tr>
    </tbody>

Answer №3

Instead of using contact.listeContacts[$index], ensure that you are using the correct variable...

        <tbody>
        <tr ng-repeat="x in contact.listeContacts | filter:contact.searchText track by $index">
            <td>
                <b>{{x.gd$name.gd$fullName.$t}}</b>
            </td>
            <td>
                <p>{{x.gd$name.gd$familyName.$t}}</p>
            </td>
            <td>
                <p>{{x.gd$name.gd$givenName.$t}}</p>
            </td>
            <td>
                <p>{{x.gd$email[0].address}}</p>
            </td>
            <td>
                <p>{{x.gd$phoneNumber[0].$t}}</p>
            </td>
            <td>
                <p>{{x.gd$organization[0].gd$orgTitle.$t}}{{x.gd$organization[0].gd$orgName.$t}}</p>
            </td>
        </tr>
    </tbody>

Answer №4

The issue here is related to the $index you are using to access your items. When your list is filtered using ng-repeat, the index property iterates over your filtered list.

To resolve this, simply utilize your local variable, which appears to be named x in your situation.

For a visual example, check out this codepen link:

<td>{{contact.name}}</td>
<td>{{contacts[$index].name}}</td> <!-- may not be the same -->

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 navigation controller, responsible for updating navbar values, is only executed once

My goal is to construct a simple Angular application and familiarize myself with the basics. To start, I'm utilizing Yeoman's angular-generator for scaffolding. This generator comes with a predetermined .config featuring $routeProvider, which I ...

Creating dynamic captions in an Angular grid is an essential feature for enhancing the

Is there a way in Angular to dynamically update the grid titles based on an array of elements in the model? How can I display them as captions? For instance, imagine we are currently in week 202010. I would like to automatically generate the next five wee ...

AngularJS | Using ngAnimate to Display Form Validation Errors

My form errors are currently being displayed successfully using CSS and conditional classes, but I recently discovered ng-message and ng-animate. Would it be possible to use ng-message to contain the error messages? Also, I have been unable to find any tu ...

Accessing Values from a Map Returned by Spring in an Angular Controller

I'm completely new to using Angular and I need some help with retrieving a map value returned from Spring inside an Angular controller. Could someone please assist me with this? Below is the code snippet for reference: app.js // Service --------- ...

Show ng-message when email is invalid in Angular Material without using ng-pattern

I need to show an error message that says Please enter valid email. when an invalid email is entered, but I cannot use the ng-pattern attribute with this specific regex pattern. <md-input-container class="md-block" flex-gt-xs> <label>Ema ...

Having trouble selecting a default option in a dynamically populated select dropdown using ng-model in the dropdown

For my Angularjs application, I needed to dynamically return a select drop down with its selected option. To accomplish this, I implemented the following function: function getCellRendererMapping(data) { if (data.order == 6) { return funct ...

When using a service, AngularJS can experience issues when the code is minified

Hello, I'm still learning AngularJS and trying my best to follow Todd Motto's Opinionated AngularJS Styleguide for Teams. I've encountered an issue while using Grunt to uglify my code - whenever I enable mangling, it throws an error: Error: ...

Node.js and Express.js are being used in conjunction with Angular to create a server-side controller that fetches all data. However, there seems to be an issue as the

"findByStaff" and "findOne" are working correctly, however, "findAll" is not returning any data. I expected findAll to retrieve all courses from mongodb $scope.findByStaff = function() { $scope.courses = Courses.query(); }; $scope.fin ...

Saving a revised JSON file using AngularJS

Currently, I am in the process of developing a phonegap application using AngularJS that relies on a .json file to store an array of entries. The main goal I am aiming for is to enable users to mark specific entries as favorites and then utilize that data ...

Mapping dynamic objects inside a repeat loop in AngularJS ui-select

Here is my ui-select directive code that is working perfectly fine. Within a repeat loop, I need to dynamically set the property of the CODE object (which is ID) to something like code[fwValueProperty]. Since it is not using ng-repeat, I cannot use it in ...

Leverage Jasmine for testing a jQuery plugin

I'm currently utilizing the angular-minicolors library () within an angular controller: angular.element("myelement").minicolors({ position: 'top left', change: function() { //custom code to run upon color change } }) Wh ...

I'm finding it difficult to grasp the purpose of $inject within controllers

I'm feeling completely lost when it comes to understanding inject in Angular. I can't seem to grasp where it should be utilized and its purpose. Is it specifically tied to factory methods, as outlined here? myController.$inject = ['$scope&a ...

What is the best way to store all rows in a dynamically changing table with AngularJS?

I am facing an issue while trying to dynamically add rows for a variable that is a String array in my database. The problem is that it only saves the last value entered in the row instead of saving all of them in an array. Here is my current view code: &l ...

Develop a feature within an Angular directive to implement a button that can delete the directive completely

If I have an element called "filterList" containing angular directives in the form of <filter-item>, and I want each <filter-item> to include a delete button that removes it from the DOM when clicked, how can I achieve this? <div class="fil ...

Optimizing JavaScript performance by packaging files with Browserify

Currently, I am utilizing AngularJS for the front end of my project. Initially, I was using Grunt as my build tool but now I am interested in transitioning to npm for this purpose. I came across a useful link that discusses using npm scripts as a build too ...

Utilizing the MEAN stack to establish a connection with a simulated data collection

Currently, I am diving into the world of M.E.A.N stack development. As part of my learning process, I have successfully set up a test page where Angular.js beautifully displays and re-orders data based on search criteria. To challenge myself further, I dec ...

Testing with karma/jasmine in AngularJS can lead to issues when there are conflicts between test

Currently experiencing challenges while running my midway tests (or integration tests, which are halfway between unit tests and E2E tests). Working with an AngularJS build featuring RequireJS, I am utilizing the RequireJS plugin in Karma to run the tests. ...

Organizing AngularJS controllers in separate files

I am facing a challenge with my cross-platform enterprise app that is built using Onsen UI and AngularJS. The app has been growing rapidly in size, making it confusing and difficult to manage. Until now, I have kept all the controllers in one app.js file a ...

Switching the input of data from a string format to a date format

One of the components in my registration form requires the user to input their start date. I am currently utilizing a MEAN Framework, specifically AngularJs for the front end development. Progress so far: Initially, I attempted using the code snippet bel ...

Categorize elements in an array based on a specific keyword

Struggling with my AngularJS (1) application, I can't seem to figure out how to split an array of items into separate arrays grouped by item. In simpler terms, I have an array with different items and I want to group them by their uuid like this: [ ...