angular - Angular中的排序排列
问题描述
我从数据库中获取一个数组(按降序排列),并将它们绘制在表格和图表的前面,如图所示:
但是在图表的一部分中,我想按升序排序,然后执行以下操作:
export class PruebasComponent implements OnInit {
data: any[];
dataSource: PeriodicElement[] = [];
lineChartData: ChartDataSets[];
.......
ngOnInit() {
this.loading = true;
this.datos.getDesktopLimit().subscribe(
res => {
this.loading = false;
this.data = [res];
this.dataSource = this.data[0]; //aqui almaceno el arreglo y lo paso al componente html
this.barChartData = true;
this.getFilter(this.dataSource); // ejecuto la funcion getFilter()
}
)
}
用sort
我按升序对其进行排序以将其传递给图表
getFilter(data) {
console.log(data);
data.sort((a, b) => a.id - b.id); //ordenar de forma ascendente para la grafica
for (let entry of data) {
this.date.push(moment(entry.created).format('DD-MM-YYYY HH:mm'))
this.time.push(entry.total_load_time * 0.001)
}
this.lineChartData = [{ data: this.time, label: 'Time Render' }];
this.lineChartLabels = this.date;
this.loading = false
}
它有效地命令我,但表格也按升序排序
解决方案
以下行什么都不做;
data.sort((a, b) => a.id - b.id);
您必须从中分配值:
sortedDataForGraph : any[];
...
sortedDataForGraph = data.sort((a, b) => a.id - b.id);
推荐阅读
- javascript - chrome 使用 nginx 后不加载外部脚本,让我们加密
- python-3.x - 预测结果从数据帧保存到 csv
- ios - 为 SwiftUI 检测 MapView 中的长按
- php - React JS 和 PHP 联系表单失败
- javascript - 为什么我必须在 Express 中调用 app.default.listen() 而不是 app.listen()?
- reactjs - 在 react-admin 列表中设置用作过滤器的 RadioButtonInput 的样式?
- winapi - 运行应用程序提示:“应用程序无法启动,因为找不到 XYZ.DLL。” 但它实际上运行良好?
- yaml - CloudFormation 脚本 - 复制 AMI
- mysql - MySQL:计算购物车行项目+具有单个值的选项总和
- r - 有没有办法在闪亮仪表板中调整数据表宽度和侧边栏宽度?