sortablejs拖拽排序功能(vue)

有些需求是要求有表格拖拽排序,或者是一些div拖拽排序

所以我们可以用sortablejs进行,官方文档为:http://www.sortablejs.com/

一、下载

npm install sortablejs --save

二、使用

例子1: table拖拽(element-ui)

效果图:

  • 行拖拽

在这里插入图片描述

  • 列拖拽
    在这里插入图片描述
<template>
  <div class="home">
    <!-- 在这里要注意,表格一定添加row-key这个属性,因为是用来优化 Table 的渲染,不然拖拽无效果 -->
    <el-table :data="tableData" style="width: 60%" row-key="id">
      <el-table-column
        v-for="(item, index) of dropCol"
        :prop="dropCol[index].prop"
        :label="dropCol[index].label"
        :width="dropCol[index].width"
        :key="`column_${index}`"
      ></el-table-column>
    </el-table>
  </div>
</template>

<script>
// 引入sortablejs
import Sortable from "sortablejs";

export default {
  name: "home",
  data() {
    return {
      dropCol: [
        {
          label: "日期",
          prop: "date",
          width: "180"
        },
        {
          label: "姓名",
          prop: "name",
          width: "180"
        },
        {
          label: "地址",
          prop: "address",
          width: ""
        }
      ],
      tableData: [
        {
          id: 1,
          date: "20160502",
          name: "王小虎1",
          address: "上海市普陀区金沙江路 1518 弄"
        },
        {
          id: 2,
          date: "20160504",
          name: "王小虎2",
          address: "上海市普陀区金沙江路 1517 弄"
        },
        {
          id: 3,
          date: "20160501",
          name: "王小虎3",
          address: "上海市普陀区金沙江路 1519 弄"
        },
        {
          id: 4,
          date: "20160503",
          name: "王小虎4",
          address: "上海市普陀区金沙江路 1516 弄"
        }
      ]
    };
  },
  mounted() {
    //  拖拽事件一定要放在 mounted 生命周期内,因为这时真实的DOM才渲染出来
    this.rowDrop();
    this.columnDrop();
  },
  methods: {
    rowDrop() {
      const tbody = document.querySelector(".el-table__body-wrapper tbody");
      const _this = this;
      Sortable.create(tbody, {
        onEnd({ newIndex, oldIndex }) {
          const currRow = _this.tableData.splice(oldIndex, 1)[0];
          _this.tableData.splice(newIndex, 0, currRow);
        }
      });
    },
    //列拖拽
    columnDrop() {
      const wrapperTr = document.querySelector(".el-table__header-wrapper tr");
      Sortable.create(wrapperTr, {
        animation: 180,
        delay: 0,
        onEnd: evt => {
          const oldItem = this.dropCol[evt.oldIndex];
          this.dropCol.splice(evt.oldIndex, 1);
          this.dropCol.splice(evt.newIndex, 0, oldItem);
          // 截止上面为止,数组已经进行了更换,但是会看到表格的头部label没有更新,所以就进行了数组清空重新赋值
          const newArray = this.dropCol.slice(0);
          this.dropCol = [];
          this.$nextTick(() => {
            this.dropCol = newArray;
          });
        }
      });
    }
  }
};
</script>

<style>
</style>

例子2: div拖拽

效果:

<template>
  <div class="home">
    <div class="floor">
      <div class="column_box" v-for="(item, index) in floorArr" :key="`floor_${index}`">
        <span>{{item}}</span>
      </div>
    </div>
  </div>
</template>

<script>
// 引入sortablejs
import Sortable from "sortablejs";

export default {
  name: "home",
  data() {
    return {
      floorArr: ["王小虎1", "王小虎2", "王小虎3", "王小虎4"]
    };
  },
  mounted() {
    //  拖拽事件一定要放在 mounted 生命周期内,因为这时真实的DOM才渲染出来
    this.rowDrop();
  },
  methods: {
   // 行拖拽 
    rowDrop() {
      const tbody = document.querySelector(".floor");
      const _this = this;
      Sortable.create(tbody, {
        onEnd({ newIndex, oldIndex }) {
          const currRow = _this.floorArr.splice(oldIndex, 1)[0];
          _this.floorArr.splice(newIndex, 0, currRow);
          // 截止上面为止,数组已经进行了更换,但是会看到视图没有更新,所以就进行了数组清空重新赋值
          const newArray = _this.floorArr.slice(0);
          _this.floorArr = [];
          _this.$nextTick(() => {
            _this.floorArr = newArray;
          });
        }
      });
    }
  }
};
</script>

<style>
.column_box {
  width: 200px;
  height: 100px;
  text-align: center;
  border: 1px solid red;
}
</style>
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
问题描述: 使用Vue3和Sortable.js实现拖拽排序功能拖拽排序后重新赋值给原数组,出现回跳问题,即拖拽的元素会回到原来的位置。 原因分析: 这是因为拖拽排序后,Sortable.js会将原数组的顺序重新排列,而Vue.js对数组的响应式更新是通过监测数组的变异方法(如push、pop、shift、unshift、splice、sort、reverse等)来实现的,而Sortable.js并没有调用这些变异方法,所以Vue.js并没有检测到数组的变化,从而导致回跳问题。 解决方法: 一种解决方法是手动调用Vue.js的数组变异方法,来更新数组的顺序。具体实现方法如下: 1. 在Vue组件中引入Sortable.js库。 ```javascript import Sortable from 'sortablejs' ``` 2. 在组件的mounted生命周期钩子中初始化Sortable.js实例,并传入需要排序的元素和配置项。 ```javascript mounted() { this.$nextTick(() => { this.sortable = new Sortable(this.$refs.list, { onEnd: this.sortHandler }) }) } ``` 其中,onEnd是Sortable.js的一个回调函数,当拖拽结束后会自动调用该函数。 3. 实现sortHandler方法,该方法会在拖拽结束后自动调用,用来更新数组的顺序。 ```javascript sortHandler(event) { const { newIndex, oldIndex } = event const item = this.list.splice(oldIndex, 1)[0] this.list.splice(newIndex, 0, item) } ``` 其中,newIndex和oldIndex分别表示拖拽结束时元素的新索引和旧索引。通过splice方法,将拖拽的元素从旧索引位置删除,再将其插入到新索引位置即可。 4. 在组件的beforeUnmount生命周期钩子中,销毁Sortable.js实例。 ```javascript beforeUnmount() { this.sortable.destroy() } ``` 完整代码示例: ```javascript <template> <div ref="list"> <div v-for="(item, index) in list" :key="item.id"> {{ item.text }} </div> </div> </template> <script> import Sortable from 'sortablejs' export default { data() { return { list: [ { id: 1, text: 'Item 1' }, { id: 2, text: 'Item 2' }, { id: 3, text: 'Item 3' }, { id: 4, text: 'Item 4' }, { id: 5, text: 'Item 5' } ], sortable: null } }, mounted() { this.$nextTick(() => { this.sortable = new Sortable(this.$refs.list, { onEnd: this.sortHandler }) }) }, beforeUnmount() { this.sortable.destroy() }, methods: { sortHandler(event) { const { newIndex, oldIndex } = event const item = this.list.splice(oldIndex, 1)[0] this.list.splice(newIndex, 0, item) } } } </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值