Html 有没有一种方法可以根据单元格的角度值对表格进行排序?
我当前的表如下所示:Html 有没有一种方法可以根据单元格的角度值对表格进行排序?,html,angular,typescript,angular-material,Html,Angular,Typescript,Angular Material,我当前的表如下所示: sortCells(){ let array = Cell[]; // here we're assuming there is already a cell type and a cell.active parameter, like shown in your example. let possibleValues = ["Draft","Pending","Complete"]; // easier wa
sortCells(){
let array = Cell[]; // here we're assuming there is already a cell type and a cell.active parameter, like shown in your example.
let possibleValues = ["Draft","Pending","Complete"]; // easier way to compare two values
array.sort((a,b)=>{
let aIndex = possibleValues.indexOf(a.active); // index of gets the location of the element in an array
let bIndex = possibleValues.indexOf(b.active);
if(a > b){
return -1;
} else if(b > a){
return 1;
}else{
return 0; // they are equal
}
})
}
地位
草稿
悬而未决的
完成
我对你的问题有点困惑,但我想我理解你的问题。 您需要将值转换为数组,然后使用
.sort()
函数。因此,假设您有一个单元格数组,我们可以将其称为let array=Cell[]
,然后您可以像这样访问单元格的状态:
sortCells(){
let array = Cell[]; // here we're assuming there is already a cell type and a cell.active parameter, like shown in your example.
let possibleValues = ["Draft","Pending","Complete"]; // easier way to compare two values
array.sort((a,b)=>{
let aIndex = possibleValues.indexOf(a.active); // index of gets the location of the element in an array
let bIndex = possibleValues.indexOf(b.active);
if(a > b){
return -1;
} else if(b > a){
return 1;
}else{
return 0; // they are equal
}
})
}
您可以在此处阅读有关排序的更多信息:嘿,我刚刚给出了答案。不完全确定它是否是您正在寻找的,但我希望它能让您走上正确的道路。嗨,如果这些值是MatTableDataSource的一部分呢?该规范是否仍然适用?