Steps to retrieve the number of records after applying a filter in an Angular pipe

I found some code that I've tweaked to suit my needs. The problem I'm facing is that I can't seem to get an accurate record count after entering the Auto Search text. It seems to be working fine for the Group Filters though.

Any assistance would be greatly appreciated.

http://stackblitz.com/edit/ng6-multiple-search-values-q3qgig

<form novalidate [formGroup]="form">

    <h3>Auto Search</h3>
    <input type="text" class="form-control" #searchText (input)="autoSearch.emit(searchText.value)">

    <hr/>

    <h3>Group Filter</h3>
    <div class="row">
        <div class="col-md-3 col-sm-3">
            <select class="form-control" formControlName="prefix">
                <option value="">Prefix</option>
                <option value="MR">MR</option>
                <option value="MS">MS</option>
            </select>
        </div>

        <div class="col-md-3 col-sm-3">
            <button class="btn btn-primary" (click)="search(form.value)">Search</button>
        </div>
    </div>

</form>
<table class="table table-responsive">
    <tr>
        <th>Name</th>
        <th>Prefix</th>
        <th>Email</th>
        <th>Position</th>
        <th>Gender</th>
    </tr>

    <tbody>
        <tr *ngFor="let user of filteredUsers | filter: searchByKeyword">
            <td>{{ user.name }}</td>
            <td>{{ user.prefix }}</td>
            <td>{{ user.email }}</td>
            <td>{{ user.position }}</td>
            <td>{{ user.gender }}</td>
        </tr>
    </tbody>
</table>
<div>Record Count: {{ filteredUsers.length }}</div>

Answer №1

Discovered the solution on this Stack Overflow thread

In the TypeScript component, I created a field to keep track of the current count

filteredCount = { count: 0 };

In the HTML component, I added filteredCount as a parameter to the filter pipe

<tr *ngFor="let user of filteredUsers | filter:searchByKeyword:filteredCount">

Displayed the count of records in the element using {{filteredCount.count}}

<div>AutoSearch Count: {{filteredCount.count}}</div>

Updated the filter pipe to include the filteredCount .count field after filtering is complete

transform(items, ..., filterMetadata) {
    // filtering
    let filteredItems = filter(items);

    filteredCount.count = filteredItems.length;
    return filteredItems;
  }

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

Identify differences between two arrays of varying sizes and filter out any identical elements

I am currently working with two JSON arrays: {"Array1":[ { "_id": "1234"}, { "_id": "5678"}, { "_id": "3456"} ]} as well as {"Array2":[ { "_id": "1234"}, { "_id": "5678"} ]} In my Node.js application, I am trying to find the differences be ...

Implementing debouncing or throttling for a callback function in React using hooks, allowing for continuous updates without having to wait for the user to finish typing

Using React with Hooks and Formik to create a form that includes a preview of the content, I noticed a performance issue when rendering both elements simultaneously. Even though the form works smoothly by itself, it becomes slow and unresponsive when incor ...

JavaScript is incapable of locating image files

I am encountering Resource Not Found errors for the image files I am attempting to load into var manifest. Despite following the tutorial code closely, I can't seem to identify what is causing this issue... (function () { "use strict"; WinJS.Bin ...

Issue with Jquery prevents clicking on hyperlinks

I have a table where table rows can be clicked. However, some cells cannot be clicked if they contain hyperlinks. This is because having both actions - one for clicking the table row and another for clicking the hyperlink - can cause confusion. Therefore, ...

JavaScript methods for identifying the type of a clicked object

How can I determine the type of object that was clicked in JavaScript? I have created a GUI element similar to a Tree View using React (with PrimeReact - Tree View) with data obtained from a Rest call in JSON format. The first layer of the tree represents ...

Issue: ng-select dropdown content is not visible when clicking the down arrow after dynamically searching for a result

Currently, I have implemented a ng-select dropdown for dynamically searching data. However, one issue that I've encountered is that when the search results are displayed and you click on the down arrow, the content is not visible as expected. You ca ...

When adjusting the size of my image within the canvas, I prefer to scale it evenly in all directions. However, the current settings only allow for scaling

Scenario I have a canvas that displays an image, and I would like to animate it by zooming in all directions simultaneously (top, right, bottom, left). Desired Outcome During each iteration, the image should move x pixels towards the top, bottom, left, a ...

"Implementing a unique constraint in Drizzle-orm PostgreSQL with the additional condition of deleted_at being

Is there a way to construct a table using a WHEN SQL tag? I am interested in setting up a table similar to this: "members_table", { id: serial("id").primaryKey(), email: text("email").notNull(), team_id: text(&q ...

Display items in Angular-js based on their children property

Is it possible to dynamically set the ng-show property of #category based on the value of item.show? I want #category to be visible only if at least one of the items in that category is shown. How can this be achieved using AngularJS? I'm still learni ...

Pressing one button triggers a specific function, while pressing another button activates a separate function, but only if the first function has been successfully executed

I am seeking assistance with implementing a script that involves 4 different buttons, each assigned to run distinct functions. Here is the setup: document.getElementById("buttonOne").addEventListener("click", functionOne); document.getElementById("buttonT ...

I encountered an error while working on my Next.js project. Specifically, I'm in the process of creating an authentication system that utilizes axios as the

An error occurred: Server Error ReferenceError: localStorage is not defined The error was encountered during the page generation process. Any console logs will be visible in the terminal window. import { LOGIN_SUCCESS, LOGIN_FAIL, LOGOUT } from "../ ...

Finding the nearest time in an array using Javascript or jQuery

I've been attempting to retrieve the closest time from an array or list, but so far I haven't had any success with the code I found. I made some edits to it, but it didn't work as expected. I'm open to using jQuery if it would simplify ...

Executing an AJAX request with a specific state in Node.js

Instead of rendering add.jade directly, I have chosen to enhance the user experience by making an AJAX call to the endpoint. This allows me to maintain the URL as localhost:3000/books, rather than localhost:3000/books/add which lacks navigation state for ...

Creating a consistent height for a static div to match a fluctuating dynamic div

Let's discuss a challenge I'm facing with div elements: <div class="middle"></div> On a daily basis, this particular div is populated with API data causing its height to vary. In addition, there are these two other divs in ...

New post: "Exploring the latest features in Angular

Looking for help with integrating Angular and SpringREST to fetch data from the backend? Here's my situation: I need to retrieve a JSON string from the backend using a POST request, send it to my site's hosted link, and display it on the user int ...

Unable to retrieve a property from a variable with various data types

In my implementation, I have created a versatile type that can be either of another type or an interface, allowing me to utilize it in functions. type Value = string | number interface IUser { name: string, id: string } export type IGlobalUser = Value | IU ...

I am unable to populate MongoDB references using Node.js

I need to display the user's location details on the screen. For example: name: "Andy" surname : "Carol" City : "Istanbul" Town : "Kadıkoy" When the getuser function is called, I want to show the City and Town name. This is the implementation: U ...

Setting the default radio button selection in Angular 2

Present within my interface are two radio buttons, not dynamically produced: <input type="radio" name="orderbydescending" [(ngModel)]="orderbydescending" [value]="['+recordStartDate']">Ascending<br> <input type="radio" name="order ...

Link social media buttons to specific posts

I've developed my own PHP blog from scratch. The homepage showcases all the latest posts. I integrated Facebook Like and Tweet buttons beneath the title of each post, but they are currently functioning for the entire homepage instead of each specific ...

Using array.map with Promise.all allows for concurrent execution

Currently, I am working with an array of images and attempting to apply image processing techniques using an asynchronous function. The code is functioning as expected since each image in the array contains an index field representing its position. // Res ...