如何在vue中使用拖动排序组件sortablejs

效果图:

在这里插入图片描述
1.首先,我们需要在vue项目中安装依赖:

npm install -save sortablejs

2.创建demo文件=>demoTest.vue,直接附上实例代码:

<template>
  <div>
    <div id="table-names">
      <div class="tableItem" v-for="item of tableData" :key="item.id">
        <span>{{ item.id }}</span>
        =>
        <span>{{ item.name }}</span>
        =>
        <span>{{ item.sort }}</span>
      </div>
    </div>
    <el-button @click="refreshData">刷新</el-button>
    <el-button @click="initData">取缓存</el-button>
  </div>
</template>

<script>
import Sortable from 'sortablejs'
let tableDemoSort = 'DEMOSORT'
const list = [
  {name: 'laowang1', id: '0', sort: 1},
  {name: 'laowang2', id: '1', sort: 2},
  {name: 'laowang3', id: '2', sort: 3},
  {name: 'laowang4', id: '3', sort: 4}
]
export default {
  data(){
    return{
      tableData:[]
    }
  },
  computed: {},
  created() {
    this.initData()
  },
  mounted(){
    this.dragTable()
  },
  methods:{
    initData() {
      let stData = this.getTableDataToLocalStorage()
      console.log('initData=>stData', JSON.stringify(stData))
      if (stData) {
        this.tableData = JSON.parse(JSON.stringify(stData))
        // this.$forceUpdate()
      } else {
        this.tableData = list
      }
      this.setTableDataToLocalStorage(this.tableData)
    },
    refreshData() {
      this.tableData = list
      this.setTableDataToLocalStorage(this.tableData)
    },
    setTableDataToLocalStorage(data) {
      localStorage.setItem(tableDemoSort, JSON.stringify(data))
    },
    getTableDataToLocalStorage() {
      return JSON.parse(localStorage.getItem(tableDemoSort))
    },
    dragTable(){
      let el = document.getElementById('table-names')
      Sortable.create(el, {
        animation: 200,
        chosenClass: "chosenClass",  // 被选中项的css 类名
        // dragClass: "dragClass",  // 正在被拖拽中的css类名
        onEnd: evt => {
          let {oldIndex, newIndex} = evt
          this.switchMapOrder(oldIndex, newIndex)
        }
      })
    },
    switchMapOrder(oldIndex, newIndex){
      console.log(`oldIndex: ${oldIndex}, newIndex: ${newIndex}`)
      const tableData = this.tableData
      let resultData = [] // 结果数组
      // 先把被移动的那条数据单独取出来
      let beSpliceItem = tableData.splice(oldIndex, 1)
      // 把剩下的数据复制给结果数组
      resultData = tableData
      // 把被移动的那条数据赋值给结果数组
      resultData.splice(newIndex, 0, beSpliceItem[0])
      // 遍历结果数组,让sort重新排序
      let newTableData = []
      resultData.forEach((item, index) => {
        item.sort = index + 1
        newTableData.push(item)
      })
      this.tableData = newTableData
      // this.$forceUpdate()
      this.setTableDataToLocalStorage(newTableData)
      // this.dragTable()
    }
  }
}
</script>

<style>
#table-names {
  display: flex;
  flex-direction: column;
}
.tableItem{
  padding: 6px 10px;
  cursor: pointer;
}
.sortable-chosen{
  background-color: blue;
}

/* // 拖拽 */
.dragClass {
  background-color: rgba(0, 188, 235, 0.5) !important;
  color: #0000a3 !important;
}
/* // 停靠 */
.ghostClass {
  background-color: rgba(0, 191, 243, 0.1) !important;
}
/* // 选择 */
.chosenClass {
  background-color: rgba(0, 188, 235, 0.2) !important;
}
</style>

3.提供一个列表拖动排序的文档,想要了解更多,请查看文档:http://www.sortablejs.com/options.html

  • 26
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vue3项目使用VxeColumn组件实现sortable的方法如下所示: 1. 首先安装sortablejs依赖 ```bash npm install sortablejs --save ``` 2. 在Vue3项目引入sortable.js文件 ```javascript import Sortable from 'sortablejs'; ``` 3. 在VxeTable的columns属性使用VxeColumn组件并在其添加slot插槽 ```javascript <vxe-table :data="tableData"> <vxe-column field="id" title="ID" sortable></vxe-column> <vxe-column field="name" title="Name" sortable> <template #header="{ column }"> <span class="drag-handle">☰</span> {{ column.title }} </template> </vxe-column> <vxe-column field="age" title="Age" sortable></vxe-column> </vxe-table> ``` 4. 在VxeColumn组件的header插槽添加拖拽排序的图标 ```javascript <vxe-column field="name" title="Name" sortable> <template #header="{ column }"> <span class="drag-handle">☰</span> {{ column.title }} </template> </vxe-column> ``` 5. 在VxeTable的mounted函数,对VxeColumn组件进行初始化sortable ```javascript <template> <vxe-table :data="tableData" ref="xTable"></vxe-table> </template> <script> import Sortable from 'sortablejs'; export default { data() { return { tableData: [ { id: 1, name: 'Tom', age: 18 }, { id: 2, name: 'Jerry', age: 20 }, { id: 3, name: 'Lucy', age: 22 }, { id: 4, name: 'John', age: 24 }, ], }; }, mounted() { const el = this.$refs.xTable.getTableColumn().$el.querySelector('.vxe-table--header'); Sortable.create(el, { animation: 150, handle: '.drag-handle', onEnd: ({ oldIndex, newIndex }) => { const data = [...this.$refs.xTable.getTableData()]; const item = data.splice(oldIndex - 1, 1)[0]; data.splice(newIndex - 1, 0, item); this.$refs.xTable.loadData(data); }, }); }, }; </script> ``` 以上是在Vue3项目使用VxeColumn组件实现sortable的方法,你可以根据自己的需求进行修改。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值