做项目过程中需要第一行**合计不参与排序**,保持固定,以下是**整数类型**排序和**浮点数**类型排序,包括**带单位的%**利率类的问题进行排序代码。
HTML代码
<div class="table" ref="table">
<Table
border
:columns="columns1"
:data="data1"
:default-sort="{ prop: 'count', order: 'descending' }"
:max-height="tableHeight"
class="table-css ivu-table-tip-left"
></Table>
</div>
js代码
**//这是表格数据data**
filterData(data) {
this.data1 = [];
if (data && data.length > 0) {
data.map((item, index) => {
this.data1.push({
spinstlevel: item.spinstlevel,
//整数类,合计设为字符串类型,不参与排序
count: index == 0 ? String(item.count) : Number(item.count),
//金额类,合计设为字符串类型,不参与排序
opsumRMB:
index == 0
? new String(item.opsumRMB)
: parseFloat(item.opsumRMB).toFixed(2),
loansumRMB: item.loansumRMB,
notLoansumRMB: item.notLoansumRMB,
//百分数,new 字符串对象进行标记处理
loansumRate: new String(item.loansumRate),
});
if (index == 0) {
this.data1[0].opsumRMB.type = "合计";
this.data1[0].loansumRate.type = "合计";
}
});
console.log(this.data1);
}
},
columns1: [
{
sortable: true,
title: "用率",
key: "loansumRate",
sortMethod: (a, b, type) => {
if (a.type == "合计" || b.type == "合计") {
return 0;
} else {
if (type == "asc") {
return Number(a.substring(0, a.length - 1)) >
Number(b.substring(0, b.length - 1))
? -1
: 1;
} else {
return Number(a.substring(0, a.length - 1)) <
Number(b.substring(0, b.length - 1))
? -1
: 1;
}
}
},
sortMethod: function (a, b, type) {
if (a.type == "合计" || b.type == "合计") {
return 0;
} else {
a = parseFloat(a);
b = parseFloat(b);
if (type == "asc") {
return parseFloat(a) > parseFloat(b) ? -1 : 1;
} else {
return parseFloat(a) < parseFloat(b) ? -1 : 1;
}
}
},
},
sortMethod: (a, b, type) => {
if (typeof a == "string" || typeof b == "string") {
return 0;
} else {
if (type == "asc") {
return Number(a) > Number(b) ? -1 : 1;
} else {
return a < b ? -1 : 1;
}
}
},
]