Issue with ng-repeat filtering in AngularJS causing data not to display properly

My data setup appears as follows:

$scope.friends = [
    {
      name: function() {
        return 'steve';
      },
      number: function() {
        return 555;
      }
    },
    {
      name: function() {
        return 'mary';
      },
      number: function() {
        return 555;
      }
    },
    {
      name: function() {
        return 'jo';
      },
      number: function() {
        return 888;
      }
    }
  ]

I am trying to filter the data in ng-repeat based on text input. I have attempted the following:

<table id="searchTextResults">
  <tr><th>Name</th><th>Phone</th></tr>
  <tr ng-repeat="friend in friends | filter:search">
    <td>{{friend.name}}</td>
    <td>{{friend.number}}</td>
  </tr>
</table>

The input is defined as

<input type="text" ng-model="search.name"/>
. However, the filter does not work. After modifying the data structure like this,

$scope.friends = [
{
  name:'steve',
  number: 555          
},
{
  name:'marry',
  number: 555
},
{
 name:'steve',
  number: 888
} 

 ]

only then it works. But my intention is to keep my data structure as it is.

I would appreciate any help or advice on how to apply a filter with the current data structure provided above.

Here is a Plunker example for reference.

Answer №1

To meet this requirement, you can adjust the friends data. My approach involves using the angular.forEach() function to modify the data and store it in a new array called $scope.modifiedFriends.

$scope.modifiedFriends = [];
angular.forEach($scope.friends, function(friend) {
    $scope.modifiedFriends.push({
        name: friend.name(),
        number: friend.number()
    });
});

Instead of utilizing ng-repeat on the original friends array, I recommend using ng-repeat with the modifiedFriends array.

<tr ng-repeat="friend in modifiedFriends | filter:search">
    <td>{{friend.name}}</td>
    <td>{{friend.number}}</td>
</tr>

Check out the Working Plunker for a live demonstration.

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

Caution: It is important for each child within a list to have a distinct "key" prop when using react-input

I'm currently facing an issue with the following code snippet: {array.map((item) => ( <> <input key={item.id} type="checkbox" /> ...

Discovering the value of an object through its prototypes

Is it possible to create a function that can locate the value "5" within an object's prototype? What is the proper algorithm to achieve this? var rex = { "Name": "rex", "Age": 16, } te = { "to": 5, } rex.te = Object.create(te); function findValu ...

Problems with displaying PHP echo statements in Ajax requests

I am in the process of creating an ajax-php event calendar and have integrated ajax within the form to Add events: function PostData() { var xhr; if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); ...

breaking up string values in Node.js

I am currently facing an issue with splitting values from an array. Here is how the array looks: [ 'xs', 'small', 'medium' ] I am trying to rearrange them to display like this in my database: xs small medium Here is what I ...

Check out our website's event countdown timer featuring a server-sided event tracking system

Currently, I am in the process of developing a website and have the requirement to incorporate a timer on one of the pages. The goal is for the timer to count down from a specified time, such as DD::hh:mm 02:12:34, until it reaches zero. Once the countdown ...

Display picture within a modal window, regardless of whether it is the identical image

I have a file input that allows the user to upload an image. After selecting an image, a modal opens with the uploaded image displayed inside. You can view a demo of this functionality in action on this Fiddle Example: Here is the corresponding code snip ...

The Best Approach for Angular Google Maps Integration

I'm diving into Angular for the first time while working on a project that requires advanced mapping functionality like clustering, routing, road routing, paths, directions, polygons, events, drawing on maps, info windows, markers, etc. After some re ...

React state variable resetting to its initial value unexpectedly

I have encountered a puzzling issue with the React useState hook. After setting the state of a value, it gets reset to null unexpectedly. The main problem lies in the click event that should update the startDate value to the current date and time. This cli ...

Pay attention to modifications in a property within an object in the React environment

Below is a React Context Provider containing a simple Counter class with methods stored in the context. import { createContext, useContext } from "react"; class Counter { public count: number = 0; getCount = () => { return this.count ...

The function attached to $(window).on("load",fn) is failing to execute

Currently, I am utilizing jQuery to verify if the website resources are being loaded correctly. In order to test this functionality, I purposely inserted an incorrect CSS file into the index.html. When the following code is placed in the Javascript file w ...

conceal the mustard-colored dots within the overlay

Whenever I trigger the link, a modal window pops up, but it comes with an unwanted black background color and yellow dots. How can I prevent the yellow dots from showing up on the overlay? http://jsfiddle.net/y88WX/18/embedded/result/ <nav class="da-d ...

A step-by-step guide on masking (proxying) a WebSocket port

Within my server-side rendering React app, I have set up a proxy for all HTTP calls to a different port. Take a look at the code snippet below for the HTTP proxy configuration. import proxy from "express-http-proxy"; const app = express(); const bodyParse ...

Prevent the Stop Function from being executed repeatedly while scrolling

I have implemented infinite scrolling in my react/redux app. As the user nears the bottom of the page, more contents are loaded dynamically. However, a challenge arises when the user scrolls too fast and triggers the function responsible for fetching cont ...

Is it feasible to open and view a STEP file using a three.js file?

Is it possible to read STEP files in three.js for generating 3D PCB components? While the file format is different, STEP files also contain 3D component information. Are there any alternative methods for reading STEP files in three.js? Any suggestions? ...

Troubleshooting AngularUI's UI-sortable Index Update Problem

I've encountered a strange bug while using AngularUI's ui-sortable directive. Most of the time, dragging elements works smoothly and the index is updated when an element is moved to a new position. However, there are instances where the index doe ...

The jQuery each function in combination with the index function allows for

I'm struggling to make this jQuery script work properly: $(document).on('knack-scene-render.scene_126', function() { $('#view_498 > div.kn-list-content.columns.is-multiline').each(function(index) { if ($(this).eq(inde ...

The property map of undefined cannot be read

To test my understanding of the map() function, I decided to retrieve data from the Starwars API using the URL: '' and store it in a constant named 'data'. My goal was to only map the characters found in the results[] array, so I creat ...

Generate a nested object by combining multiple objects within an array using JavaScript

I am working with a nested array of objects, shown below: [ { "i18n Key": "messages.titles.info", English: "Info", Spanish: "Info", }, { ...

AngularJS: Implement ng-repeat directive to generate two separate HTML containers

I have a collection of items and I want to divide them into two separate containers. Is there a way to achieve this using ng-repeat? <div> <ul> <li>Some item</li> <li>Some item</li> <li&g ...

When you switch to a different URL within the same tab, the session storage will be automatically cleared

My current web application is experiencing an issue with session storage. Whenever I navigate to a different URL within the same tab, it seems like the session storage is being cleared. Let me walk you through the situation: I initially store some data ...