gpt4 book ai didi

angular - 在列值更改时禁用 Angular 表的自动排序

转载 作者:行者123 更新时间:2023-12-04 10:49:14 27 4
gpt4 key购买 nike

当用户更改列中的值时,是否可以以某种方式禁用 Angular 表中的自动排序? Bcs 更改后立即对数据进行排序,当该列上有事件排序时。如果我设置 dataSource.sort = null 排序被禁用,但数据排序返回到默认排序。
我只想在列标题单击时对数据进行排序。

最佳答案

您可以尝试以下方法。我不知道你是如何更新你的表的。我只是用一个简单的例子来向你展示如何禁用 mat-table 中的现有排序

.ts

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

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

const ELEMENT_DATA: PeriodicElement[] = [
{position: 1, name: 'Hydrogen', weight: 100, symbol: 'H'},
{position: 2, name: 'Helium', weight: 40, symbol: 'He'},
{position: 3, name: 'Lithium', weight: 60, symbol: 'Li'},
{position: 4, name: 'Beryllium', weight: 90, symbol: 'Be'},
{position: 5, name: 'Boron', weight: 100, symbol: 'B'},
{position: 6, name: 'Carbon', weight: 10, symbol: 'C'},
{position: 7, name: 'Nitrogen', weight: 140, symbol: 'N'},
{position: 8, name: 'Oxygen', weight: 150, symbol: 'O'},
{position: 9, name: 'Fluorine', weight: 180, symbol: 'F'},
{position: 10, name: 'Neon', weight: 20, symbol: 'Ne'},
];

/**
* @title Table with sorting
*/
var i =11;
@Component({
selector: 'table-sorting-example',
styleUrls: ['table-sorting-example.css'],
templateUrl: 'table-sorting-example.html',
})
export class TableSortingExample implements OnInit {
displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
dataSource = new MatTableDataSource(ELEMENT_DATA);

@ViewChild(MatSort, {static: true}) sort: MatSort;

ngOnInit() {
this.dataSource.sort = this.sort;
}
AddNewItem(){
this.dataSource.data = [...this.dataSource.data,{position: i, name: 'Neon', weight: 20, symbol: 'Ne'}];
this.sort.sort(
{id:'',start:'asc',disableClear : false}
)
i++
}
}

.html
<button (click)="AddNewItem()">Add New Item</button>
<table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8">

<!-- Position Column -->
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef mat-sort-header> No. </th>
<td mat-cell *matCellDef="let element"> {{element.position}} </td>
</ng-container>

<!-- Name Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Name </th>
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>

<!-- Weight Column -->
<ng-container matColumnDef="weight">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Weight </th>
<td mat-cell *matCellDef="let element"> {{element.weight}} </td>
</ng-container>

<!-- Symbol Column -->
<ng-container matColumnDef="symbol">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Symbol </th>
<td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
</ng-container>

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

你可以在这里找到工作链接:

https://stackblitz.com/edit/angular-di4rp1

在更新数据源后的组件中,只需添加这些行即可删除现有的排序
this.sort.sort(
{id:'',start:'asc',disableClear : false}
)

关于angular - 在列值更改时禁用 Angular 表的自动排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59566622/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com