Vxe UI vue vxe-table v4.9+ 分享如何实现列直接拖拽排序,列拖拽调整顺序
安装
npm install vxe-pc-ui@3.3.0 vxe-table@3.11.0
main.js
import Vue from 'vue'
// ...
import VxeUI from 'vxe-pc-ui'
import 'vxe-pc-ui/lib/style.css'
import VxeUITable from 'vxe-table'
import 'vxe-table/lib/style.css'
Vue.use(VxeUI)
Vue.use(VxeUITable)
//...
列拖拽
参数 column-config.drag 开启列拖拽功能,这样就可以开启行拖拽调整顺序功能了。
<template>
<div>
<vxe-button status="success" @click="resultEvent">获取数据</vxe-button>
<vxe-grid ref="gridRef" v-bind="gridOptions"></vxe-grid>
</div>
</template>
<script>
export default {
data () {
const gridOptions = {
border: true,
columnConfig: {
useKey: true,
drag: true
},
columns: [
{ field: 'name', title: 'Name' },
{ field: 'role', title: 'Role' },
{ field: 'sex', title: 'Sex' },
{ field: 'age', title: 'Age' },
{ field: 'address', title: 'Address' }
],
data: [
{ id: 10001, name: 'Test1', role: 'Develop', sex: 'Man', age: 28, address: 'test abc' },
{ id: 10002, name: 'Test2', role: 'Test', sex: 'Women', age: 22, address: 'Guangzhou' },
{ id: 10003, name: 'Test3', role: 'PM', sex: 'Man', age: 32, address: 'Shanghai' },
{ id: 10004, name: 'Test4', role: 'Designer', sex: 'Women', age: 24, address: 'Shanghai' }
]
}
return {
gridOptions
}
},
methods: {
resultEvent () {
const $grid = this.$refs.gridRef
if ($grid) {
const { visibleColumn } = $grid.getTableColumn()
console.log(visibleColumn)
}
}
}
}
</script>
同时使用行与列拖拽排序
参数 column-config.drag 和 row-config.drag 开启同时行与列拖拽调整顺序功能,这样就可以开启行拖拽功能了。
<template>
<div>
<vxe-button status="success" @click="resultEvent">获取数据</vxe-button>
<vxe-grid ref="gridRef" v-bind="gridOptions"></vxe-grid>
</div>
</template>
<script>
export default {
data () {
const gridOptions = {
border: true,
columnConfig: {
useKey: true,
drag: true
},
rowConfig: {
useKey: true,
drag: true
},
columns: [
{ field: 'name', title: 'Name' },
{ field: 'role', title: 'Role' },
{ field: 'sex', title: 'Sex' },
{ field: 'age', title: 'Age' },
{ field: 'address', title: 'Address' }
],
data: [
{ id: 10001, name: 'Test1', role: 'Develop', sex: 'Man', age: 28, address: 'test abc' },
{ id: 10002, name: 'Test2', role: 'Test', sex: 'Women', age: 22, address: 'Guangzhou' },
{ id: 10003, name: 'Test3', role: 'PM', sex: 'Man', age: 32, address: 'Shanghai' },
{ id: 10004, name: 'Test4', role: 'Designer', sex: 'Women', age: 24, address: 'Shanghai' }
]
}
return {
gridOptions
}
},
methods: {
resultEvent () {
const $grid = this.$refs.gridRef
if ($grid) {
const { visibleColumn } = $grid.getTableColumn()
const { visibleData } = $grid.getTableData()
console.log(visibleColumn, visibleData)
}
}
}
}
</script>
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。