我的最尾部的合计行是单独请求接口的数据放在了列表的最后一行的。但是我对列表排序的时候并不想带上最后一行合计行,
实现代码逻辑如下
添加属性ref="productionRecordsTable"和sort-change
<el-table
:data="tableData"
ref="productionRecordsTable"
@sort-change="sortChange"
>
<el-table-column
prop="region_name"
label="大区"
sortable="custom"
>
<template scope="scope">
{{ dash(scope.row.region_name) }}
</template>
</el-table-column>
</el-table>
js代码逻辑
// 排序事件
sortChange(column) {
// console.log('排序事件', column)
const fieldName = column.prop;
const sortingType = column.order;
// this.getWeeklyList();
let tableData = this.tableData;
let avgData = {}; // 当页合计一行数据
console.log(tableData, "没排序之前");
tableData.map((item) => {
if (item.region_name == "合计") {
avgData = item;
}
});
if (sortingType == "ascending") {
// 删除table中当页合计一行数据
tableData.forEach((item, index) => {
if (item.region_name == "合计") {
tableData.splice(index, 1);
}
return item;
});
//正序
tableData = tableData.sort((a, b) => {
// console.log(555, typeof a[fieldName])
if (typeof a[fieldName] == "string") {
return b[fieldName].localeCompare(a[fieldName]);
} else if (typeof a[fieldName] == "number") {
return b[fieldName] - a[fieldName];
}
});
tableData.push(avgData);
} else if (sortingType == "descending") {
tableData.map((item, index) => {
if (item.region_name == "合计") {
tableData.splice(index, 1);
}
return item;
});
// 倒序
tableData = tableData.sort((a, b) => {
// console.log(666, typeof a[fieldName])
if (typeof a[fieldName] == "string") {
return a[fieldName].localeCompare(b[fieldName]);
} else if (typeof a[fieldName] == "number") {
return a[fieldName] - b[fieldName];
}
});
tableData.push(avgData);
} else {
this.$refs.productionRecordsTable.clearSort(); // 清除排序
}
},