如何为应用列表表格实现基于appAcronym的搜索筛选功能?
Add a Search Bar to Filter App Table by
appAcronym Got it, let's get that search bar set up so you can quickly filter your app table by app acronym. Here's a straightforward solution tailored to your Angular setup:
1. Update Your Component Class
First, add a search term property and a filtered version of your apps list to handle the filtering logic. This keeps your original data intact while serving up only matching results:
import { Component } from '@angular/core'; import { App } from './path-to-your-app-interface'; // Adjust the path as needed @Component({ selector: 'app-your-component', templateUrl: './your-component.component.html', styleUrls: ['./your-component.component.css'] }) export class YourComponent { appsList: App[] = []; // Your original 250-row app dataset searchTerm: string = ''; // Return filtered apps based on the search input get filteredApps(): App[] { if (!this.appsList || !this.searchTerm) { return this.appsList; // Return full list if no search term } // Case-insensitive match for appAcronym return this.appsList.filter(app => app.appAcronym.toLowerCase().includes(this.searchTerm.toLowerCase()) ); } }
2. Modify Your Template to Add the Search Bar
Add the search input above your existing table, and update the table to use the filteredApps list instead of the raw appsList:
<!-- Search Bar (placed above the table) --> <div class="mb-3"> <input type="text" class="form-control" placeholder="Search by App Acronym..." [(ngModel)]="searchTerm" > </div> <!-- Updated Table --> <table class="table table-striped table-hover table-sm" *ngIf="filteredApps"> <thead> <tr> <th>App Acronym</th> <th>App Name</th> <!-- Keep your existing header columns here --> </tr> </thead> <tbody> <tr *ngFor="let app of filteredApps"> <td>{{ app.appAcronym }}</td> <td>{{ app.appName }}</td> <!-- Keep your existing table cells here --> </tr> </tbody> </table>
Quick Checks to Ensure It Works:
- Forms Module: Don’t forget to import
FormsModulein your parent module if you haven’t already—this is required for[(ngModel)]to work:import { FormsModule } from '@angular/forms'; @NgModule({ imports: [ // ... other imports FormsModule ] }) export class YourModule { } - Performance: With only 250 rows, this simple filter will run smoothly. If you ever scale to thousands of rows, you could add a debounce (using RxJS
debounceTime) to reduce unnecessary filter runs, but this isn’t needed right now.
That’s all! Users can now type in the search bar to instantly narrow down the table to rows matching their appAcronym query.
内容的提问来源于stack exchange,提问作者Yousuf Jamil
相关产品推荐
相关产品推荐

