这个问题通常出现在使用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 }}
相关内容
热门资讯
实测交流(wEpOke)外挂透...
实测交流(wEpOke)外挂透明挂辅助机制(透视)透视辅助(有挂实锤)-哔哩哔哩;wEpOke软件透...
透视脚本(德扑之星花钱)外挂透...
透视脚本(德扑之星花钱)外挂透明挂辅助神器(辅助挂)透明挂教程(2022已更新)(哔哩哔哩);wpk...
一分钟秒懂(wpk安装)外挂透...
一分钟秒懂(wpk安装)外挂透明挂辅助器(透视)透视辅助(有挂方法)-哔哩哔哩;免费wpk安装平台A...
交流学习经验(微扑克线上)外挂...
交流学习经验(微扑克线上)外挂透明挂辅助工具(透视)软件透明挂(确实有挂)-哔哩哔哩;1分钟了解详细...
终于清楚(德扑胜率)外挂透明挂...
终于清楚(德扑胜率)外挂透明挂辅助挂(透视)透视辅助(有挂方略)-哔哩哔哩;一、德扑胜率有挂的是的,...
最新研发(新版WPK)外挂透明...
最新研发(新版WPK)外挂透明挂辅助挂(透视)黑科技教程(2021已更新)(哔哩哔哩);最新版202...
今日科普(wpk德州)外挂透明...
今日科普(wpk德州)外挂透明挂辅助神器(透视)透视辅助(真的有挂)-哔哩哔哩1、这是跨平台的wpk...
透视免费(aa扑克伙牌)外挂透...
WePoker透视辅助工具核心要点解析,透视免费(aa扑克伙牌)外挂透明挂辅助插件(透视)2024...
透视游戏(红龙软件德州扑克)外...
红龙软件德州扑克赢率提升策略;透视游戏(红龙软件德州扑克)外挂透明挂辅助机制(辅助挂)发牌机制(的...
免费测试版(德州之星手游)外挂...
免费测试版(德州之星手游)外挂透明挂辅助APP(辅助挂)新版2024教程(2022已更新)(哔哩哔哩...