【问题标题】:angular data source filter角度数据源过滤器
【发布时间】:2022-01-03 05:29:09
【问题描述】:

我有一个正在渲染到表格的对象列表,您可以查看示例截图。我正在尝试根据#searchTransactionUserInput placeholder="" (keyup)="applyFilter($event)"过滤数据

您可以看到从作为表格数据源的示例屏幕截图返回的数据。当前的问题是,当我搜索关键字“ron”时,它返回的其他数据与该键不匹配,例如,如果关键字是“ro”,它应该只返回用户 Ronnie js Renil,但当前它也返回“用户两个”

我当前的过滤算法似乎有什么问题?有什么想法吗?谢谢。

过滤或搜索的优先级是按全名过滤。

#table数据源

表格数据来源:

#UI

用户界面示例:

#html代码

<div class="search" fxLayoutAlign="start" fxFill>
  <mat-form-field appearance="standard" fxFill>
    <mat-label style="font-size: 12px"
      >Filter users by name, company or title</mat-label
    >
    <input
      matInput
      #searchTransactionUserInput
      placeholder=""
      (keyup)="applyFilter($event)"
    />
    <button
      mat-button
      *ngIf="searchTransactionUserInput"
      matSuffix
      mat-icon-button
      aria-label="Clear"
      (click)="clearSearch()"
    >
      <mat-icon>close</mat-icon>
    </button>
  </mat-form-field>
</div>

#tscode

applyFilter(filterValue: any) {
  const data = filterValue.target.value;
  filterValue = data.trim(); // Remove whitespace
  filterValue = data.toLowerCase(); // Datasource defaults to lowercase matches
  this.dataSource.filter = data;

}

clearSearch(): void {
  this.searchTransactionUserInput.nativeElement.value = '';
  this.dataSource.filter = null;
  this._transactionUserPageEvent();
}

【问题讨论】:

  • 为什么你将data.trim()data.toLowerCase() 分配给filterValue 而你之后不使用这个filterValue?你在哪里指定过滤器的优先级?能否请您发布您的完整代码?
  • 这就是过滤Sir的所有代码
  • @MernaMustafa,先生,有没有更好的过滤器实现可以解决我的问题?
  • 这里有很多问题,与过滤无关,尤其是函数中变量的使用
  • @Drenai,是的,这是我正在尝试的实验的一部分,也许我只是忘了删除它

标签: angular typescript angular-material


【解决方案1】:

试试这个。这是我的工作代码:

<div *ngIf="isSearch" class="search-box-container">
<mat-form-field class="search-box-form-field">
  <mat-label>Search by Keyword</mat-label>
  <input #search matInput type="text" (keyup)="onSearchKeyUp(search)">
  <button mat-button *ngIf="search" matSuffix mat-icon-button aria-label="Clear" (click)="onClearClicked(search)">
    <mat-icon>close</mat-icon>
  </button>
</mat-form-field>
onSearchKeyUp(search: { value: any; }) {
var currentFilter = search.value;
this.dataSource.filter = currentFilter;

if (this.dataSource.paginator) {
  this.dataSource.paginator.firstPage();
}

onClearClicked(search: { value: string; }) {
this.dataSource.filter = "";
search.value = "";

if (this.dataSource.paginator) {
  this.dataSource.paginator.firstPage();
}

【讨论】:

  • 先生,为什么当我输入或搜索“ro”时,为什么会出现包含“用户二”的结果?该数据上没有“ro”imgur.com/a/H32B8cT
  • 它应该只返回 ronnie jr renil
  • 将“搜索”添加到 OnInit 并将其设为空白以开始如下。搜索 = '';
  • 为什么当我输入或搜索“ro”时,为什么会出现包含“用户二”的结果?该数据上没有“ro”
  • 当我搜索“ro”时,应该只有 1 个结果,因为只有“Ronnie js Renil”,因为“用户二”没有“ro”,imgur.com/a/H32B8cT
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-11
  • 2022-11-27
  • 1970-01-01
  • 2021-04-06
相关资源
最近更新 更多