我用的vxe-table的库,方法逻辑都是一样的
<template>
<div>
<vxe-grid ref='xGrid' v-bind="gridOptions"></vxe-grid>
</div>
</template>
sortablejs 拖拽里面修改一下,存列表拖拽后的顺序
const columnDrop2 = () => {
const $grid = xGrid.value
sortable2 = Sortable.create($grid.$el.querySelector('.body--wrapper>.vxe-table--header .vxe-header--row'), {
handle: '.vxe-header--column',
onEnd: (sortableEvent) => {
const targetThElem = sortableEvent.item
const newIndex = sortableEvent.newIndex
const oldIndex = sortableEvent.oldIndex
const { fullColumn, tableColumn } = $grid.getTableColumn()
const wrapperElem = targetThElem.parentNode
const newColumn = fullColumn[newIndex]
if (newColumn.fixed) {
const oldThElem = wrapperElem.children[oldIndex]
if (newIndex > oldIndex) {
wrapperElem.insertBefore(targetThElem, oldThElem)
} else {
wrapperElem.insertBefore(targetThElem, oldThElem ? oldThElem.nextElementSibling : oldThElem)
}
VXETable.modal.message({ content: '固定列不允许拖动!', status: 'error' })
return
}
const oldColumnIndex = $grid.getColumnIndex(tableColumn[oldIndex])
const newColumnIndex = $grid.getColumnIndex(tableColumn[newIndex])
----------------这边就是拖拽后处理逻辑,我放在session,你要放那看你自己需求-----------------------------------------
let arr = JSON.parse(sessionStorage.getItem("rkSort"))
if(arr == null){
arr = [0,1,2,3,4,5,6,7]
}
const oldV =arr.splice(oldColumnIndex, 1)[0]
arr.splice(newColumnIndex,0,oldV)
sessionStorage.setItem("rkSort",JSON.stringify(arr))
----------------------结束----------------------------------------------------------
const currRow = fullColumn.splice(oldColumnIndex, 1)[0]
fullColumn.splice(newColumnIndex, 0, currRow)
$grid.loadColumn(fullColumn)
}
})
}
ok数据存进去了,拿出来怎么用呢
let initTime
nextTick(() => {
initTime = setTimeout(() => {
columnDrop2()
hhSwaps()
}, 500)
})
function customEvent(oldIndex,newIndex) {
const $grid = xGrid.value
const { fullColumn, tableColumn } = $grid.getTableColumn()
const arr = JSON.parse(sessionStorage.getItem("rkSort"))
const currRow = fullColumn.splice(oldIndex, 1)[0]
fullColumn.splice(newIndex, 0, currRow)
$grid.loadColumn(fullColumn)
}
function hhSwaps() {
const arrOld = [0,1,2,3,4,5,6,7]
const arrNew = JSON.parse(sessionStorage.getItem("rkSort"))
if(arrNew != null){
for(let i = 0;i < arrNew.length; i++){
for(let j=i;j < arrNew.length; j++){
if( arrNew[i] == arrOld[i]){
break
}
if(arrNew[i] == arrOld[j]){
customEvent(j,i)
const old = arrOld.splice(j, 1)[0]
arrOld.splice(i, 0, old)
}
}
}
}
}