You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为应用列表表格实现基于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())
    );
  }
}

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 FormsModule in 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:33:42