1.对el-table绑定sort-change事件:
<el-table ref="table" max-height="425" size="mini" v-loading="loading" :data="certificationList" @sort-change="sortChange" border stripe></el-table>
data() {
return {
propType:"",//存放column.prop的字符串值
}
}
2.js
sortChange(column) {
console.log('当前列需要排序的数据', column.prop);
console.log('当前列的排序规则', column.order);
this.certificationForm.pageNo = 1 //排序后返回第一页
if (column.prop === 'riskStatus') {
this.propType = column.prop
if (column.order === 'descending') {
this.certificationList.sort(this.descSort)
} else if (column.order === 'ascending') {
this.certificationList.sort(this.ascSort)
}
} else if (column.prop === 'totalCertificateQuantity') {
this.propType = column.prop
if (column.order === 'descending') {
this.certificationList.sort(this.descSort)
} else if (column.order === 'ascending') {
this.certificationList.sort(this.ascSort)
}
} else {
this.certificationForm.sortName = column.prop;
this.certificationForm.sort = column.order === "descending" ? "DESC" : "ASC";
this.getcertificationList();
}
},
// 正序
descSort(a, b) {
return b[this.propType] - a[this.propType]
},
// 倒序
ascSort(a, b) {
return a[this.propType] - b[this.propType]
},