角材料表排序不正确

时间:2019-11-29 23:39:10

标签: angular sorting filter angular-material

我的垫子表可以使用多个过滤器正常工作。然后添加mat-sort并尝试进行排序,没有任何改变。

table-filtering-example.html

<mat-card>
<div class="row">
    <div class="col-12">
        <mat-form-field>
            <mat-label>Category</mat-label>
            <mat-select [(ngModel)]="categoryFilter" (selectionChange)="applyFilter($event.target)">
                <mat-option>All</mat-option>
                <mat-option *ngFor="let category of categories" [value]="category">
                    {{ category }}
                </mat-option>
            </mat-select>
        </mat-form-field>
    </div>

    <div class="col-12">
        <mat-form-field>
            <input
  matInput
  [(ngModel)]="productFilter"
  (keyup)="applyFilter($event.target.value)"
  placeholder="Search"
/>
            <mat-icon matSuffix>search</mat-icon>
        </mat-form-field>
    </div>
</div>

    

    <ng-container matColumnDef="name">
        <mat-header-cell *matHeaderCellDef  mat-sort-header>Product</mat-header-cell>
        <mat-cell *matCellDef="let product">
            {{ product.name }}
        </mat-cell>
    </ng-container>

    <ng-container matColumnDef="category">
        <mat-header-cell *matHeaderCellDef mat-sort-header>Category</mat- 
    header-cell>
        <mat-cell *matCellDef="let product">
            {{ product.category}}
        </mat-cell>
    </ng-container>

    <mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumns"></mat-row>
</mat-table>
</div>

table-filtering-example.ts

import { Component, ViewChild, OnInit } from "@angular/core";
import { MatSort } from "@angular/material/sort";
import { MatTableDataSource } from "@angular/material/table";

@Component({
selector: 'table-filtering-example',
styleUrls: ['table-filtering-example.css'],
templateUrl: 'table-filtering-example.html',
})
export class TableFilteringExample {
@ViewChild(MatSort) sort: MatSort;

products = [
{ name: "Lenovo", category: "Notebook" }, 
{ name: "Asus", category: "Notebook" },
{ name: "HP", category: "Notebook" },
{ name: "Dell", category: "Notebook" },
{ name: "Logitech MX Master Mouse", category: "Notebook Accessories" },
{ name: "Razer DeathAdder Mouse", category: "Notebook Accessories" },
{ name: "Apple Magic Keyboard", category: "Notebook Accessories" },
{ name: "İphone X", category: "Mobile Phone" },
{ name: "Xaomi9", category: "Mobile Phone" },
{ name: "Galaxy Note 10", category: "Mobile Phone" }
];

categories = ["Notebook", "Notebook Accessories", "Mobile Phone"];
displayedColumns = ["name", "category"];

dataSource = new MatTableDataSource(this.products);

categoryFilter;
productFilter = "";

ngAfterViewInit() {
this.dataSource.sort = this.sort;
}

applyFilter() {
let filteredData;

if (this.categoryFilter === undefined) {
  filteredData = this.products.filter(x =>
    x.name.toLocaleLowerCase()
      .trim()
      .includes(this.productFilter)
  );
  } else {
  filteredData = this.products.filter(
    x =>
      x.name.toLocaleLowerCase()
        .trim()
        .includes(this.productFilter) && x.category === 
this.categoryFilter
  );
}

this.dataSource = new MatTableDataSource(filteredData);
}
}

这是堆叠闪电战:

https://stackblitz.com/edit/angular-material-datatable-multi-column-filter-d1dt1l?file=app/table-filtering-example.css

1 个答案:

答案 0 :(得分:1)

角度@ViewChild装饰器无法识别模板中的MatSort实例,因为您要从两个不同的包中导入MatSort

main.ts

import { MatSortModule } from '@angular/material';

app.component.ts

import { MatSort } from '@angular/material/sort';

一旦从MatSort中的同一路径导入AppComponent

import { MatSort } from '@angular/material';

排序功能应该起作用

Forked Stackblitz