【发布时间】:2021-12-14 05:50:02
【问题描述】:
表格 -> 动作 -> 不活动
当我们更改了行状态值时,mat-table 数据源值也随之更新。
我们的要求是在更新到数据库后更改状态值,但现在 mat-table 立即更改状态值。
我们如何停止 mat-table 数据源更新?
我已经给出了例子。 请查看堆栈闪电战
HTML
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<!--- Note that these columns can be defined in any order.
The actual rendered columns are set as a property on the row definition" -->
<!-- Position Column -->
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef>No.</th>
<td mat-cell *matCellDef="let element">{{element.position}}</td>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef>Name</th>
<td mat-cell *matCellDef="let element">{{element.name}}</td>
</ng-container>
<!-- Weight Column -->
<ng-container matColumnDef="weight">
<th mat-header-cell *matHeaderCellDef>Weight</th>
<td mat-cell *matCellDef="let element">{{element.weight}}</td>
</ng-container>
<!-- Symbol Column -->
<ng-container matColumnDef="symbol">
<th mat-header-cell *matHeaderCellDef>Symbol</th>
<td mat-cell *matCellDef="let element">{{element.symbol}}</td>
</ng-container>
<ng-container matColumnDef="status">
<th mat-header-cell *matHeaderCellDef>status</th>
<td mat-cell *matCellDef="let element">{{element.status}}</td>
</ng-container>
<ng-container matColumnDef="actions">
<th mat-header-cell *matHeaderCellDef>Actions</th>
<td mat-cell *matCellDef="let element">
<button
mat-button
[matMenuTriggerFor]="menu"
class="nav-link dropdown-toggle mobile-data"
>
Acción
</button>
<mat-menu #menu="matMenu">
<button
mat-menu-item
class="dropdown-item"
(click)="updateStatus('inActive',element)"
>
<span>InActive</span>
</button>
</mat-menu>
</td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
ts
import { Component } from '@angular/core';
/**
* @title Basic use of `<table mat-table>`
*/
@Component({
selector: 'table-basic-example',
styleUrls: ['table-basic-example.css'],
templateUrl: 'table-basic-example.html',
})
export class TableBasicExample {
displayedColumns = [
'position',
'name',
'weight',
'symbol',
'status',
'actions',
];
dataSource = ELEMENT_DATA;
updateStatus(status, item) {
console.log('before set status', this.dataSource);
item.status = status;
console.log('After set status', this.dataSource);
}
}
export interface PeriodicElement {
name: string;
position: number;
weight: number;
symbol: string;
status: string;
}
const ELEMENT_DATA: PeriodicElement[] = [
{
position: 1,
name: 'Hydrogen',
weight: 1.0079,
symbol: 'H',
status: 'Active',
},
{
position: 2,
name: 'Helium',
weight: 4.0026,
symbol: 'He',
status: 'Active',
},
{
position: 3,
name: 'Lithium',
weight: 6.941,
symbol: 'Li',
status: 'Active',
},
];
stackblitz 演示:https://stackblitz.com/edit/angular-keucik?file=app/table-basic-example.ts
【问题讨论】:
-
你好穆鲁甘。回答您后,我们需要更多信息。我们需要了解您何时何地调用 de API Backend。
-
只是我设置了 item.status = status 但 DataSource( [dataSource]="dataSource") 值也发生了变化。我的问题
-
您分享的代码没有显示出来。如需帮助,请提供正确信息
-
我已经更新了 stackblitz 代码。请检查一下
-
我将在回答中解释这一点
标签: angular angular-material mat-table angular-material-table