vxe-table 如何自定義排序圖標(biāo)按鈕,自定義排序
vxe-table 如何自定義排序圖標(biāo)按鈕,自定義排序
查看官網(wǎng):https://vxetable.cn
gitbub:https://github.com/x-extends/vxe-table
gitee:https://gitee.com/x-extends/vxe-table




使用表頭排序按鈕插槽 sort 來自定義模板
<template>
<div>
<vxe-grid ref="gridRef" v-bind="gridOptions">
<template #nameSort="{ column }">
<vxe-button mode="text" title="點擊排序" :status="column.order ? 'primary' : ''" :icon="column.order === 'desc' ? 'vxe-icon-sort-alpha-desc' : 'vxe-icon-sort-alpha-asc'" @click="sortEvent(column.field, column.order)"></vxe-button>
</template>
<template #numSort="{ column }">
<vxe-button mode="text" title="點擊排序" :status="column.order ? 'primary' : ''" :icon="column.order === 'desc' ? 'vxe-icon-sort-numeric-desc' : 'vxe-icon-sort-numeric-asc'" @click="sortEvent(column.field, column.order)"></vxe-button>
</template>
</vxe-grid>
</div>
</template>
<script setup>
import { ref, reactive } from 'vue'
const gridRef = ref()
const gridOptions = reactive({
border: true,
columns: [
{ type: 'seq', width: 70 },
{ field: 'name', title: 'Name', width: 200, sortable: true, slots: { sort: 'nameSort' } },
{ field: 'sex', title: 'Sex' },
{ field: 'num', title: 'Number', sortable: true, slots: { sort: 'numSort' } },
{ field: 'age', title: 'Age' },
{ field: 'address', title: 'Address' }
],
data: [
{ id: 10001, name: 'Test1', role: 'Develop', sex: '0', age: 28, num: 100, address: 'test abc' },
{ id: 10002, name: 'Test3', role: 'Test', sex: '1', age: 22, num: 234, address: 'Guangzhou' },
{ id: 10003, name: 'Test5', role: 'PM', sex: '0', age: 32, num: 12, address: 'Shanghai' },
{ id: 10003, name: 'Test4', role: 'Test', sex: '1', age: 8, num: 10, address: 'Shanghai' }
]
})
const sortEvent = (field, order) => {
const $grid = gridRef.value
if ($grid) {
// 觸發(fā)事件用 setSortByEvent
$grid.setSort({
field,
order: order === 'desc' ? 'asc' : (order === 'asc' ? null : 'desc')
}, true)
}
}
</script>

浙公網(wǎng)安備 33010602011771號