sortablejs 列表拖拽后刷新保持上次拖动

我用的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
        }
        // 获取列索引 columnIndex > fullColumn
        const oldColumnIndex = $grid.getColumnIndex(tableColumn[oldIndex])
        const newColumnIndex = $grid.getColumnIndex(tableColumn[newIndex])
        
        ----------------这边就是拖拽后处理逻辑,我放在session,你要放那看你自己需求-----------------------------------------
        //把排好队序放入session
        let arr = JSON.parse(sessionStorage.getItem("rkSort"))
        if(arr == null){
          //这个初始数组看你列表有几个字段,我就8个显示列
          arr = [0,1,2,3,4,5,6,7]
        }
        //按照sortablejs拖动规则给数组排序
        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"))
	        // 移动到目标列,按照sortablejs 规则
	        const currRow = fullColumn.splice(oldIndex, 1)[0]
	        fullColumn.splice(newIndex, 0, currRow)
	        $grid.loadColumn(fullColumn)
        }

        //根据session的拖拽排序 重排,也就是加载后根据上次数组在模拟一次上次拖动
        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)
                  //按照sortablejs的规则排序数组
                  const old = arrOld.splice(j, 1)[0]
                  arrOld.splice(i, 0, old)
                }
              }

            }
          }
        }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值