最近刚开始使用Vue+element UI 使用table组件进行分页并且采用了分页组件,然后进行前端排序发现了一个问题,使用table自定义的排序属性 sortable=“true” 的话组件只对当前页的数据进行了排序,而不是整个数据进行排序
这种情况下我们可以使用回调函数 sort-change 将整个数据进行排序
```javascript
<el-table
//分页模糊搜索相关,不用在乎
:data="selectInfo.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase())).slice((currpage - 1) * pagesize, currpage * pagesize)"
//我们需要使用的回调函数
@sort-change="sortChange"
@selection-change="handleSelectionChange"
style="width: 100%">
<el-table-column
label="权重"
sortable="custom" //同事将sortable属性设置成custom,这样我们才能使用sortChange进行自定义
prop="showWeights">
然后在method里
```javascript
methods: {
//查询
selectByType(type) {
this.type = type;
this.selectList();
},
// 自定义排序功能,用户排序的时候触发
sortChange(v) {
//正序
if (v.column.order == "ascending") {
//通过属性showWeights进行排序
if (v.column.property == "showWeights") {
this.selectInfo.sort(this.sortList("showWeights"));
}
}
//倒序
else if (v.column.order == "descending") {
if (v.column.property == "showWeights") {
this.selectInfo.sort(this.sortListDesc("showWeights"));
}
}
//通过数组对象的某个属性进行排序
sortList(property) {
return function(a, b) {
var value1 = a[property];
var value2 = b[property];
return value1 - value2;
};
},
//通过数组对象的某个属性进行倒序排列
sortListDesc(property) {
return function(a, b) {
var value1 = a[property];
var value2 = b[property];
return value2 - value1;
};
},
},
当我们使用分页插件的时候采用这种的方式就不用担心Table组件自带排序的问题了