在 vxe-table 使用排序功能非常簡單,給列加上 columns.sortable 屬性就可以啟用列的排序功能,當然這是本地排序,就是對當前已載入過當前頁的資料進行排序。對於查詢列表列表,專案中使用最多的是服務端排序,因為大部分都涉及分頁。
前端排序
這樣就可以啟用前端資料排序,僅限於當前頁
<template>
<div>
<vxe-grid v-bind="gridOptions"></vxe-grid>
</div>
</template>
<script>
export default {
data () {
const gridOptions = {
border: true,
loading: false,
height: 400,
columns: [
{ type: 'seq', width: 70 },
{ field: 'name', title: 'Name' },
{ field: 'role', title: 'Role', sortable: true },
{ field: 'sex', title: 'Sex', sortable: true },
{ field: 'age', title: 'Age', sortable: true },
{ field: 'address', title: 'Address', sortable: true }
],
data: [
{ id: 10001, name: 'Test1', role: 'Develop', sex: 'Man', age: 28, num: '3.8', num2: '3.8', address: 'test abc' },
{ id: 10002, name: 'Test2', role: 'Test', sex: 'Women', age: 22, num: '511', num2: '511', address: 'Guangzhou' },
{ id: 10003, name: 'Test3', role: 'PM', sex: 'Man', age: 32, num: '12.8', num2: '12.8', address: 'Shanghai' },
{ id: 10004, name: 'Test4', role: 'Designer', sex: 'Women', age: 23, num: '103', num2: '103', address: 'test abc' },
{ id: 10005, name: 'Test5', role: 'Develop', sex: 'Women', age: 30, num: '56', num2: '56', address: 'Shanghai' },
{ id: 10006, name: 'Test6', role: 'Designer', sex: 'Women', age: 21, num: '49', num2: '49', address: 'test abc' },
{ id: 10007, name: 'Test7', role: 'Test', sex: 'Man', age: 29, num: '400.9', num2: '400.9', address: 'test abc' },
{ id: 10008, name: 'Test8', role: 'Develop', sex: 'Man', age: 35, num: '5000', num2: '5000', address: 'test abc' }
]
}
return {
gridOptions
}
}
}
</script>
多個欄位排序
多欄位排序透過 sort-config.multiple 來配置啟用
<template>
<div>
<vxe-grid v-bind="gridOptions"></vxe-grid>
</div>
</template>
<script>
export default {
data () {
const gridOptions = {
border: true,
height: 300,
sortConfig: {
multiple: true
},
columns: [
{ type: 'seq', width: 70 },
{ field: 'name', title: 'Name', sortable: true },
{ field: 'role', title: 'Role', sortable: true },
{ field: 'num', title: 'Num', sortable: true }
],
data: [
{ name: '小紅', role: '前端', num: 7 },
{ name: '老王', role: '後端', num: 6 },
{ name: '小紅', role: '後端', num: 1 },
{ name: '小明', role: '前端', num: 2 },
{ name: '老徐', role: '測試', num: 3 },
{ name: '老王', role: '前端', num: 3 },
{ name: '老徐', role: '測試', num: 4 },
{ name: '小明', role: '前端', num: 4 },
{ name: '小明', role: '前端', num: 8 },
{ name: '小明', role: '測試', num: 6 },
{ name: '小紅', role: '後端', num: 9 },
{ name: '老徐', role: '前端', num: 5 },
{ name: '老徐', role: '測試', num: 1 },
{ name: '小紅', role: '前端', num: 4 },
{ name: '小紅', role: '前端', num: 2 },
{ name: '小明', role: '測試', num: 3 },
{ name: '老王', role: '前端', num: 6 },
{ name: '老王', role: '後端', num: 4 },
{ name: '老徐', role: '前端', num: 8 },
{ name: '小明', role: '測試', num: 7 }
]
}
return {
gridOptions
}
}
}
</script>
服務端排序
透過配置 sort-config.remote 和 sort-change 事件,實現服務端排序
<template>
<div>
<vxe-grid v-bind="gridOptions" @sort-change="sortChangeEvent"></vxe-grid>
</div>
</template>
<script>
import XEUtils from 'xe-utils'
export default {
data () {
const gridOptions = {
border: true,
loading: false,
height: 400,
sortConfig: {
remote: true
},
columns: [
{ type: 'seq', width: 70 },
{ field: 'name', title: 'Name' },
{ field: 'role', title: 'Role', sortable: true },
{ field: 'sex', title: 'Sex', sortable: true },
{ field: 'age', title: 'Age', sortable: true },
{ field: 'address', title: 'Address', sortable: true }
],
data: []
}
return {
gridOptions
}
},
methods: {
findList (field, order) {
this.gridOptions.loading = true
// 模擬介面
return new Promise(resolve => {
setTimeout(() => {
this.gridOptions.loading = false
const mockList = [
{ id: 10001, name: 'Test1', role: 'Develop', sex: 'Man', age: 28, num: '3.8', num2: '3.8', address: 'test abc' },
{ id: 10002, name: 'Test2', role: 'Test', sex: 'Women', age: 22, num: '511', num2: '511', address: 'Guangzhou' },
{ id: 10003, name: 'Test3', role: 'PM', sex: 'Man', age: 32, num: '12.8', num2: '12.8', address: 'Shanghai' },
{ id: 10004, name: 'Test4', role: 'Designer', sex: 'Women', age: 23, num: '103', num2: '103', address: 'test abc' },
{ id: 10005, name: 'Test5', role: 'Develop', sex: 'Women', age: 30, num: '56', num2: '56', address: 'Shanghai' },
{ id: 10006, name: 'Test6', role: 'Designer', sex: 'Women', age: 21, num: '49', num2: '49', address: 'test abc' },
{ id: 10007, name: 'Test7', role: 'Test', sex: 'Man', age: 29, num: '400.9', num2: '400.9', address: 'test abc' },
{ id: 10008, name: 'Test8', role: 'Develop', sex: 'Man', age: 35, num: '5000', num2: '5000', address: 'test abc' }
]
if (field && order) {
const rest = XEUtils.orderBy(mockList, { field, order })
this.gridOptions.data = rest
resolve(rest)
} else {
this.gridOptions.data = mockList
resolve(mockList)
}
}, 300)
})
},
sortChangeEvent ({ field, order }) {
this.findList(field, order)
}
},
created () {
this.findList()
}
}
</script>
https://gitee.com/xuliangzhan/vxe-table