在el-table中添加@sort-change="sortChange",el-table-column中添加sortable="custom"实现自定义排序
排序代码
sortChange(column) {
let fieldName = column.prop;
this.sortingType = column.order;
let tableData = this.tableData;
let sumData = {}; //放置总计一行数据
tableData.map((item) => {
if (item.name == "总计") {
sumData = item;
}
});
if (this.sortingType == "ascending") {
//正序
tableData = tableData.sort((a, b) => a[fieldName] - b[fieldName]);
// 删除table中的总计
tableData.forEach((item, index) => {
if (item.name == "总计") {
tableData.splice(index, 1);
}
return item;
});
tableData.push(sumData);
} else if (this.sortingType == "descending") {
// 倒序
tableData = tableData.sort((a, b) => b[fieldName] - a[fieldName]);
tableData.map((item, index) => {
if (item.name == "总计") {
tableData.splice(index, 1);
}
return item;
});
tableData.push(sumData);
}
},