Calculating the amount of results displayed through the ng-repeat filter

I am interested in using ng-repeat with filter, and I want to know how I can track the number of elements that are being filtered out. My goal is to implement a search bar that applies the filter, while also displaying the count of hidden elements (for example, something like Showing 6 of 10 results).

Answer №1

Take a look at this link http://jsbin.com/xuyuy/1/edit

If you want to filter data in your repeater, you can create a new array called personsfiltered

ng-repeat="person in personsfilterd = (persons | filter : search )

Then, you can display the length of both the original array and the filtered array:

Showing {{personsfilterd.length}} of {{persons.length }} results

Here is the complete code snippet:

var app = angular.module('app', []);

app.controller('fCtrl', function($scope) {

  $scope.persons = [
    'Mike', 'Tom', 'Tim', 'Jim', 'Ken'
  ]

});

 <div ng-app="app">
    <div ng-controller="fCtrl">
      <input type="text" ng-model = "search"> Showing {{personsfilterd.length}} of {{persons.length }} results
      <li ng-repeat="person in personsfilterd = (persons | filter : search )">{{person}}</li>
    </div>
    </div>

Answer №2

Initially, the ngRepeat includes variables like $index, $first, $middle, and $last to determine the current position within the loop. It seems like your focus is on the $last variable. The uncertainty lies in whether it will reflect the original count or the count after filtering.

UPDATE: To access the count of the original values from your controller, set varX.length and utilize it in your template (HTML code). Assuming that $last indicates the final count plus one (due to being 0-based), you can determine the count after applying filters.

More on ngRepeat

Answer №3

After some searching, I stumbled upon the solution here. It turns out that storing the filtered data in a new variable within the ng-repeat is key. While there may be potential memory concerns, this wasn't an issue for me.

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

Changes in props do not automatically update children via Ref

I'm working on a React component that needs to update whenever the width changes: import { useRef, useEffect, useState } from "react"; function Child({ containerWidth }) { console.log("CHILD", containerWidth); return <div&g ...

Looking to enhance code readability using regular expressions

While I am not a programmer, I enjoy exploring and learning new things. Here is what I have: 1) My URL is structured like this: http://site.com/#!/show/me/stuff/1-12/ 2) I have a jQuery pagination script that displays the number of available pages. Each ...

Discover how ReactJS can dynamically display or hide div elements based on specific conditions being met within JSON data

How do I show or hide a div based on a condition in React, using data from a JSON array? I have implemented the code below, but changing the value of isPassed={resultPass.pass} to isPassed={resultPass.failed} still displays the result as pass. I came acro ...

Resolved AWS S3 access issue with a 403 Forbidden error by eliminating the "ACL" parameter during upload

While working on the frontend using React.js, I utilized the Javascript SDK for file uploads to my S3 bucket with my AWS root account. Despite following the official documentation, I consistently received a 403 Forbidden error. To resolve this issue, if yo ...

Combining round brackets and square brackets when initializing an array

In the snippet below, values are assigned with a mix of parentheses and square brackets without any errors. However, most other combinations (such as parentheses inside square brackets) do not work at all. var myItems = []; myItems[5] = ("A1", "B1", ["C1" ...

Discover the path with the power of JavaScript

Imagine I am including a JavaScript file in this manner: <script type="text/javascript" src="http://foo.com/script.js?id=120#foo"></script> Would it be possible to access the GET or hash parameters passed through this? Currently, I achieve t ...

While attempting to upload a file in my Mocha Node.js test, I encountered the message: "Received [Object null prototype] { file: { ... }}"

Everywhere I find the solution in white : app.use(bodyParser.urlencoded({extended: true})); I can use JSON.stringify(req.files) However, I am sure there is a way to fix my problem. My Mocha test : it('handles file upload', async function () { ...

What steps can be taken to guarantee that React updates occur in the correct order?

I'm currently working on developing a multi-select dropdown and facing the issue of hiding the options once a user selects one. The problem arises when I try to update the selectedCategoriesData state and then hide the dropdown using setShowCategories ...

Execute a javascript function using dynamic calling

I have defined several functions inside an array structure as shown below. checkInputType : function(sel){ alert($(sel).prop('type')); }, checkSelect : function(el){ ....... }, ..... These functions can be called like options.checkInput($(thi ...

My AJAX request seems to be redirecting to my PHP file instead of simply returning the response text. What could be causing this issue?

I am currently working on a project for my Web Engineering course and I am incorporating jQuery to execute an AJAX request to my PHP file using the POST method. The task specifies that "each time the [submit] button is pressed, the form data should be disp ...

Maintain the dropdown selection while the table is being updated

My table updates every few seconds and each row has a dropdown menu that allows users to take actions on that row. However, the problem is that when new data is received from the database, the dropdown menu closes if it was open. This can be frustrating f ...

Remove the </div> text and replace it with nothing

I'm attempting to substitute the end tag div with an empty string. Here is my code: $('#textDiv').html().replace(/'</div>'/g,'').replace(/<div>/g,'\n') This is the HTML: <div id='tex ...

NestJS WebSocketGateway fails to initialize due to a technical glitch

After following the instructions in the NestJS documentation, I set up the websockets gateway within the AppModule. The server is starting without any issues, and I'm able to serve static assets via HTTP successfully. However, I'm facing difficul ...

Error: Request for resolution of all parameters for SignupComponent failed: ([object Object], ?). Occurred at the syntaxError in compiler.js:2175

Could someone assist me in resolving this issue? signup.component.html <main role="main" class="container> <h1 class="mt-5">&nbsp;</h1> <h5 class="mt-5 ">Create Account</h5> <br/> <div class="loa ...

problem with parsing JSON

Here is a JSON string example: {"time":"2011-11-30 04:44","countryName":"Austria","sunset":"2011-11-30 16:32","rawOffset":1,"dstOffset":2,"countryCode":"AT","gmtOffset":1,"lng":10.2,"sunrise":"2011-11-30 07:42","timezoneId":"Europe/Vienna","lat":47.01} I ...

What are your thoughts on using inline PHP to assign values to JavaScript variables?

Imagine you have a PHP document and need to determine if the request was made with or without query parameters using JavaScript. Since there is no built-in function to extract values from the URL, you may need to resort to some sort of workaround. While it ...

Error message: The import from './components/headerComponent/header' failed because it does not have a default export. Make sure to export it as default to be able to import it

I've been trying to bring a header from one file into another, but it's not cooperating. import React from 'react'; import { Typography, Card, CardContent } from '@material-ui/core'; import Header from './components/head ...

Toggle the visibility of HTML elements by utilizing a JavaScript checkbox event

I have put together these JavaScript functions to hide the delivery address fields on my shopping cart address form if the goods are being sent to the billing address. The functions control the visibility of the HTML wrapped by... function getItem(id) { ...

What is the method for triggering the output of a function's value with specified parameters by clicking in an HTML

I am struggling to display a random number output below a button when it is clicked using a function. <!DOCTYPE html> <html> <body> <form> <input type="button" value="Click me" onclick="genRand()"> </form> <scri ...

How to retrieve the changing input value in ReactJS using Jquery/JS

I have a form in WordPress with two input range sliders. The form calculates the values of these sliders and displays the result as shown below: <input data-fraction-min="0" data-fraction="2" type="hidden" data-comma=" ...