vxetable列表远程排序的使用

<template>
  <div>
    <vxe-table
      border
      height="400"
      :loading="loading"
      :data="tableData"
      :sort-config="sortConfig"
      @sort-change="sortChangeEvent">
      <vxe-column type="seq" width="70"></vxe-column>
      <vxe-column field="name" title="Name"></vxe-column>
      <vxe-column field="role" title="Role" sortable></vxe-column>
      <vxe-column field="sex" title="Sex" sortable></vxe-column>
      <vxe-column field="age" title="Age" sortable></vxe-column>
      <vxe-column field="address" title="Address" sortable></vxe-column>
    </vxe-table>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { VxeTablePropTypes, VxeTableEvents, VxeColumnPropTypes } from 'vxe-table'
import XEUtils from 'xe-utils'

interface RowVO {
  id: number
  name: string
  role: string
  sex: string
  age: number
  num: string
  num2: string
  address: string
}

const loading = ref(false)
const tableData = ref<RowVO[]>()

const sortConfig = ref<VxeTablePropTypes.SortConfig<RowVO>>({
  remote: true
})

const findList = (field?: VxeColumnPropTypes.Field, order?: VxeTablePropTypes.SortOrder) => {
  loading.value = true
  // 模拟接口
  return new Promise<RowVO[]>(resolve => {
    setTimeout(() => {
      loading.value = 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 })
        tableData.value = rest
        resolve(rest)
      } else {
        tableData.value = mockList
        resolve(mockList)
      }
    }, 300)
  })
}

const sortChangeEvent: VxeTableEvents.SortChange<RowVO> = ({ field, order }) => {
  findList(field, order)
}

findList()
</script>
要在Vue3+TypeScript项目中实现VxeTable的拖拽排序功能,可以按照以下步骤进行: 1. 安装VxeTable和相关依赖: ``` npm install xe-utils @vue/composition-api vxe-table@next ``` 2. 在Vue3项目中引入Composition API: ```js import { createApp } from 'vue' import { createRouter, createWebHistory } from 'vue-router' import { createHead } from '@vueuse/head' import { useRoute, useRouter } from 'vue-router' import { useMeta } from 'vue-meta' import { ref, reactive, toRefs, onMounted, onUnmounted } from 'vue' import { VXETable } from 'vxe-table' import App from './App.vue' import routerConfig from './router' const app = createApp(App) app.use(router) app.use(createHead()) app.use(VXETable) app.mount('#app') ``` 3. 创建一个Vue3组件,并在其中引入VxeTable和相关依赖: ```html <template> <div> <vxe-table ref="xTable" :columns="columns" :data="data" @sort-change="handleSortChange" @header-dragend="handleHeaderDragend"></vxe-table> </div> </template> <script lang="ts"> import { ref, reactive } from 'vue' import { VXETable, ColumnInfo } from 'vxe-table' export default { setup () { const xTable = ref(null) const columns = ref([ { field: 'name', title: 'Name', sortable: true }, { field: 'age', title: 'Age', sortable: true }, { field: 'gender', title: 'Gender', sortable: true } ]) const data = ref([ { name: 'John', age: 21, gender: 'Male' }, { name: 'Mary', age: 28, gender: 'Female' }, { name: 'Bob', age: 35, gender: 'Male' } ]) const handleSortChange = (params: { column: ColumnInfo, prop: string, order: string }) => { console.log('Sort Change:', params) } const handleHeaderDragend = (params: { columns: ColumnInfo[] }) => { console.log('Header Dragend:', params) } onMounted(() => { VXETable.setup({ table: { mouseConfig: { selected: { mode: 'single' } } } }) }) onUnmounted(() => { VXETable.destroy() }) return { xTable, columns, data, handleSortChange, handleHeaderDragend } } } </script> ``` 4. 然后,在handleHeaderDragend方法中,可以获取到排序后的列信息,然后根据这些信息修改data中的数据以实现拖拽排序功能。 这样,就可以在Vue3+TypeScript项目中实现VxeTable的拖拽排序功能了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值