ngFor filter by text with custom trackby function

I'm currently developing an angular 4 application and I am dealing with a large array of objects (around 200 rows). To improve user experience, I added a search input field that dynamically filters the displayed content based on what the user types. Here's how it looks in my code:

<input type="text" [(ngModel)]="searchtext" placeholder="Search">
<div *ngFor="let m of (devices | filterEquipments : searchtext)">{{ m.name }}</div>

However, I noticed that the filtering process is quite slow when I start typing. After some research, I found out that using "trackBy" in the ngFor directive can help optimize performance. The challenge now is figuring out how to implement it correctly and whether it will actually make the filtering faster.

Does anyone have suggestions on how to implement a more efficient filtering solution?

Answer №1

Consider incorporating a delay for better performance

<input type="text" [value]="item.task_name"(keyup)="term$.next($event.target.value)">

import ......

import {Subject} from 'rxjs/Subject';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/switchMap';

@Component{( ... )} export class YourComponent {

  term$ = new Subject<string>();

  constructor() {
   this.term$
     .debounceTime(1000)
     .distinctUntilChanged()
     .switchMap(term => /*perform necessary actions*/);
  }
}

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

Save a collection of interfaces/types in TypeScript

Exploring a new approach has presented me with a minor challenge: This is what I have=> export interface SynchGroupSubject { type: SynchGroupEvent; target: any; } export enum SynchGroupEvent { ADD_MAP, REMOVE_MAP } Within a service, the fol ...

The 'state' property is not found on the 'FetchPeriod' type

Currently, I am embarking on a journey to grasp ReactJS by following a tutorial provided at this Tutorial. Being a novice in the programming language, I find myself at a loss as to what steps to take next. One roadblock I encountered was when attempting ...

Ways to shift the focus away from the current date in the Angular Material Datepicker

Is there a way to prevent the focus on today's date when utilizing Angular Material datepicker? I have attempted to include certain properties to mat-datepicker without success. Here is what I have tried: restoreFocus="false" [startAt]="null" &l ...

Updating the Bootstrap entry dynamically within the @NgModule module

My web application has a specific requirement where it can only be accessed through example.com/index.html without any parameters. The backstory behind this restriction is quite lengthy, so let's not delve into that. Within the index.html file, I have ...

The Nativescript Angular reusable component fails to trigger the (tap) function call on IOS build

I am facing an issue with my NS-Angular app that has already been developed for Android and works perfectly. The problem lies with a reusable button component designed in Angular, like so: <app-button [btnTxt]="'Sign Up'" [btnBgColor]="' ...

The Angular production build is unable to load - Module loading is being blocked due to an unauthorized MIME type ("text/html") being used

After building my application with ng build --configuration=production and manually uploading it (via FTP) to a server (I tried two different ones), I encountered a peculiar issue. When I try to directly access a subpage like myurl.com/subpage, the page ap ...

In Angular-Cli, the variables @Input and @Output are consistently null until they are assigned

Individuals' internal values are printed without any problems, but those obtained using @Input or @Output are not being displayed. child.component.ts @Component({ selector: 'app-form-input', templateUrl: './form-input.component.ht ...

The functionality of Angular router is disrupted when guards yield an Observable as their return value

My goal is to redirect users who are not logged in when they try to access protected routes, but I am facing an issue with redirection. The function that checks if the user is logged in returns an Observable. Below is the code snippet from my PrivateGuard ...

Transforming an array of HTTP Observables into an Observable that can be piped asynchronously

I am attempting to execute a series of XHR GET requests based on the results of an initial request. I have an array of observables representing the secondary requests I wish to make, and I am able to utilize Array.map for iterating over them and subscribin ...

Is the ID Column in the Minimal Material Table Demo not appearing as expected?

Hey there, I'm in the process of developing a simple demonstration of a material table - Take note that this is a stackblitz link and for some reason, the id column isn't showing up. Here's a snippet from my app.component.ts: import { C ...

Tips on organizing the object value to be included in its list value using Angular 8

I have some data that I need to reorganize. [ { ..., id: 1, value: [ {geoLat: 123123, geoLong: 123432}, {geoLat: 23240, geoLong: 234324}, {geoLat: 23240, geoLong: 234324}, ] } { ..., id: 2, value: [ ...

Troubleshooting the issue of the Delete Service not functioning properly within Angular

ListStore.ts file export class ListstoreComponent implements OnInit { rawlist; name = ''; id = ''; storeid = ""; store: Store; constructor(private api: APIService, private router: Router, private route: ActivatedRoute, pri ...

combine several JSON files into one JSON object with their respective file paths as keys

I have multiple JSON files that I need to merge into a single JSON file. To organize them effectively, I want to enclose each file under a unique key based on its path so that accessing specific files becomes easier in the combined JSON file. const jsonF ...

How to effectively dispatch actions using @ngrx/store: Exploring the benefits and best practices

Currently, I am immersed in a project that involves Angular 2 and @ngrx/store. I have a question regarding best practices and how to effectively execute certain actions. In my development process, I have created a "web finder" tool for file management. Th ...

AngularJS organization by

On this page, I have my list of products displayed: Here is the code used to display the products: <img class="col-xs-2" ng-src="{{producto.photo}}" class="img-responsive " alt="{{producto.nombre}}"> <div class="col-xs-1"> <div class= ...

How to effectively utilize TypeScript in a team environment using both Atom and VSCode?

Our team utilizes TypeScript with both Atom and VSCode as our editors, but we are facing challenges with the tsconfig.json file. VSCode is not recognizing the typings, causing the namespace for 'ng' (for Angular 1.x) to be unknown in VSCode. Wh ...

Is there a way to imitate the typeof operation on an object in TypeScript?

Today's challenge is a strange one - I've encountered a bug where the code behaves differently if typeof(transaction) returns object instead of the object name. To work around this issue, I introduced a new parameter called transactionType to my ...

Oops! There seems to be a syntax error near "NOT" in TypeORM

I am currently developing an app using NestJs with a Postgres database and TypeOrm as the ORM. I have created my migration file, configured the package.json file, but when I try to run yarn typeorm migration:run, I encounter the following error: query fail ...

Unable to exclude folder while creating production build is not functioning as intended

I've got a directory full of simulated data in the "src/api/mock" folder, complete with ts and JSON files. I'm attempting to have Webpack skip over them during the production build process. I attempted to implement the following rule, but unfortu ...

Is it possible to synchronize the Lit cached DOM with the live DOM?

Utilizing the Lit framework for constructing my front-end UI components has been a game-changer. However, I have encountered an issue while incorporating our internal company design system's web components. One of these components has the ability to r ...