angular - 如何使用重音符号/变音符号过滤 MatTable?

标签 angular angular-material special-characters diacritics mat-table

我想过滤表的名称,无论用户是否输入了任何重音符号。

例如:

如果用户输入“Hydrogen”,结果应显示“Hydrôgen”,如表中所示。

我使用 Angular 8.1.3 和 Angular Material 8.0.1

这是我的代码:

app.component.html:

<mat-form-field>
  <input matInput (keyup)="applyFilter($event.target.value)"  (focus)="setupFilter('name')" placeholder="Filter">
</mat-form-field>

<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
...

我的界面:

export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
}

app.component.ts:

const ELEMENT_DATA: PeriodicElement[] = [
  {position: 1, name: 'Hydrôgen', weight: 1.0079, symbol: 'H'},
  {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
  {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
  {position: 4, name: 'Béryllium', weight: 9.0122, symbol: 'Be'},
  {position: 5, name: 'Bóron', weight: 10.811, symbol: 'B'},
  {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
  {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
  {position: 8, name: 'ÔLxygen', weight: 15.9994, symbol: 'O'},
  {position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
  {position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
];

displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
dataSource = new MatTableDataSource(ELEMENT_DATA);

  applyFilter(filterValue: string) {
    this.dataSource.filter = filterValue.trim().toLowerCase();
  }

  setupFilter(column: string) {
    this.dataSource.filterPredicate = (d: any, filter: string) => {
      const textToSearch = d[column] && d[column].toLowerCase() || '';
      return textToSearch.indexOf(filter) !== -1;
    };
  }

  public removeAccents(str: string): string {
    return str.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
  }

我希望当用户输入“bo”时,表 2 行中会显示包含“Carbon”和“Bóron”的行。

最佳答案

这是当您在 mat-table 中使用过滤时 Angular Material 所使用的代码

filterPredicate: ((data: T, filter: string) => boolean) = (data: T, filter: string): boolean => {
  // Transform the data into a lowercase string of all property values.
  const dataStr = Object.keys(data).reduce((currentTerm: string, key: string) => {
    // Use an obscure Unicode character to delimit the words in the concatenated string.
    // This avoids matches where the values of two columns combined will match the user's query
    // (e.g. `Flute` and `Stop` will match `Test`). The character is intended to be something
    // that has a very low chance of being typed in by somebody in a text field. This one in
    // particular is "White up-pointing triangle with dot" from
    // https://en.wikipedia.org/wiki/List_of_Unicode_characters
    return currentTerm + (data as {[key: string]: any})[key] + '◬';
  }, '').toLowerCase();

  // Transform the filter by converting it to lowercase and removing whitespace.
  const transformedFilter = filter.trim().toLowerCase();

  return dataStr.indexOf(transformedFilter) != -1;
}

让我们继续修改这个 filterPredicate() 以满足您的需要。您已经展示了如何删除代码中的重音符号和变音符号。我们将在新的 filterPredicate 中使用它。

this.dataSource.filterPredicate = (data: PeriodicElement, filter: string): boolean => {
  const dataStr = Object.keys(data).reduce((currentTerm: string, key: string) => {
    return (currentTerm + (data as { [key: string]: any })[key] + '◬');
  }, '').normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase();

  const transformedFilter = filter.trim().normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase();

  return dataStr.indexOf(transformedFilter) != -1;
  }
}

我们从表中的值以及过滤器值中删除重音符号/变音符号,这样即使用户碰巧输入带有重音符号/变音符号的字符,过滤器也将继续正确过滤。

这是 StackBlitz 上的一个工作示例.

关于angular - 如何使用重音符号/变音符号过滤 MatTable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57282353/

相关文章:

javascript - 当我在 Angular 6 中将其设置为可编辑内容时,如何将焦点设置在该元素上?

angular - 迁移后模板错误 Angular 6 -> 9

javascript - Angular md-input-container 调整大小

regex - 如何使用 perl regex 在字符串中转义美元符号 ($)

macos - 如何在python中读取名称包含 "/"等字符的目录?

google-maps - 具有多个标记的 Angular 2 Google map

angular - 在 Angular5/6 中的 onload 方法之外访问文件阅读器内容?

按钮组的 Angular-material 恒定宽度

html - Angular Material 和输入类型 'time' ,值属性与 ngModel

php - 从 XML php 获取值后出现奇怪的字符