在使用mat-sort进行表格排序时,需要在要排序的列上添加mat-sort-header属性,并将所需的排序依据指定为mat-sort-header的值。 在ng-modal中使用mat-sort时,需要在ng-modal所在组件中导入MatTableModule和MatSortModule模块,并在ngOnInit函数中初始化MatSort实例,并将其赋值给matSort属性。然后,在模板文件中,将matSort属性绑定到mat-table的matSort属性上,以激活mat-sort。 以下是一个示例代码: component.ts:
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
@Component({
selector: 'app-table',
templateUrl: './table.component.html',
styleUrls: ['./table.component.css']
})
export class TableComponent implements OnInit {
@ViewChild(MatSort) sort: MatSort;
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
dataSource = new MatTableDataSource(ELEMENT_DATA);
ngOnInit() {
this.dataSource.sort = this.sort;
}
}
export interface Element {
name: string;
position: number;
weight: number;
symbol: string;
}
const ELEMENT_DATA: Element[] = [
{position: 1, name: 'Hydrogen', 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: 'Beryllium', weight: 9.0122, symbol: 'Be'},
{position: 5, name: 'Boron', weight: 10.81, symbol: 'B'},
{position: 6, name: 'Carbon', weight: 12.011, symbol: 'C'},
{position: 7, name: 'Nitrogen', weight: 14.007, symbol
上一篇:Angular11-Mat-form-fieldmat-input在标准格式下,底部应该显示的线条在重新加载之前处于中心位置。
下一篇:Angular11-NG0303:Can'tbindto'ngForOf'sinceitisn'taknownpropertyof'tr'