这个问题通常出现在使用mat table进行排序时,由于不直接与数据源进行排序而是与视图数组进行排序,因此可能导致视图数组和原始数据源不同步。解决此问题的方法是在数据更改时更新BehaviorSubject的值,以便更新视图数组。以下是一个示例:
component.ts:
import { Component, OnInit, ViewChild } from '@angular/core';
import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
import { BehaviorSubject } from 'rxjs';
interface Data {
name: string;
position: string;
age: number;
}
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponent implements OnInit {
dataSource = new MatTableDataSource();
data$: BehaviorSubject = new BehaviorSubject([]);
displayedColumns: string[] = ['name', 'position', 'age'];
@ViewChild(MatSort, { static: true }) sort: MatSort;
constructor() { }
ngOnInit(): void {
this.dataSource.sort = this.sort;
this.data$.subscribe(data => {
this.dataSource.data = data;
})
}
loadData() {
const data = [
{ name: 'John', position: 'Developer', age: 25 },
{ name: 'Alice', position: 'Manager', age: 30 },
{ name: 'Bob', position: 'Designer', age: 35 },
{ name: 'Max', position: 'Developer', age: 27 },
];
this.data$.next(data);
}
}
component.html:
Name
{{ row.name }}
Position
{{ row.position }}
Age
{{ row.age }}
相关内容
热门资讯
第6分钟了解(德州ai)外挂智...
第6分钟了解(德州ai)外挂智能ai辅助代打(透视)攻略教程(2023已更新)(哔哩哔哩);相信小伙...
第2分钟了解(tritonpo...
第2分钟了解(tritonpoker)外挂辅助插件安装代打(透视)黑科技教程(2024已更新)(哔哩...
第9分钟了解(WPK技巧)黑科...
第9分钟了解(WPK技巧)黑科技透明挂辅助软件(透视)教你攻略(2024已更新)(哔哩哔哩)是一款可...
七分钟了解(aapoker德州...
七分钟了解(aapoker德州俱乐部)软件透明挂辅助科技(透视)线上教程(2022已更新)(哔哩哔哩...
1分钟了解(Wepoke开发)...
1分钟了解(Wepoke开发)黑科技透明挂辅助黑科技(透视)扑克教程(2021已更新)(哔哩哔哩);...
第六分钟了解(wpk德州ai机...
第六分钟了解(wpk德州ai机器人)外挂辅助插件安装科技(透视)规律教程(2024已更新)(哔哩哔哩...
第1分钟了解(Wepoke技术...
第1分钟了解(Wepoke技术)外挂透明挂辅助下载(透视)实用技巧(2023已更新)(哔哩哔哩);1...
第十分钟了解(新版Wepoke...
第十分钟了解(新版Wepoke)软件透明挂辅助app(透视)曝光教程(2020已更新)(哔哩哔哩);...
第3分钟了解(wepOke)外...
第3分钟了解(wepOke)外挂辅助插件安装app(透视)第三方教程(2021已更新)(哔哩哔哩),...
八分钟了解(德扑之星设置)外挂...
八分钟了解(德扑之星设置)外挂辅助插件安装黑科技(透视)AI教程(2021已更新)(哔哩哔哩);人气...