el-table 实现行拖拽排序

el-table 实现行拖拽排序,排序后更改背景色添加标记
使用插件:Sortablejs

安装插件

npm install sortablejs --save

引入插件

import Sortable from 'sortablejs';

使用方法根据el-tabla组件

          <el-table
              ref="tableNodeTop"
              :data="tableData"
              border
              style="width: 100%"
              row-key="id"
              @selection-change="handleSelectionChange"
              @row-click="handleClick"
              highlight-current-row
            >

切记必须使用row-key="id"必须添加,必须添加,必须添加不然会排序错乱

使用 ref=“tableNodeTop”

注意这里我是用了 handle: “.my-handle”, 默认拖拽时间鼠标右键不能复制文本,无法检索表单,不需要右键复制文本得可以不用,需要得表单里面这样写

**我这里只能拖拽序号列,其他列不能拖拽,但是可以右键复制本文 **

 <el-table-column prop="order" label="序号">
                <template slot-scope="scope">
                  <div :class="'my-handle'">
                    {{ scope.row.order }}
                  </div>
                </template>
   </el-table-column>


---------------------------------
<style lang="scss">
.my-handle {
  cursor: move;
  cursor: -webkit-grabbing;
}
</style>
  mounted() {
    this.pullSort(); //声明表格拖动排序方法
  },
 methods: {

 //表格拖动排序方法
    pullSort() {
      // 通过ref获取Dom节点
      const el = this.$refs.tableNode.$el.querySelectorAll(
        ".el-table__body-wrapper > table > tbody"
      )[0];
      this.sortable = new Sortable.create(el, {
        handle: ".my-handle",//格式为简单css选择器的字符串,使列表单元中符合选择器的元素成为拖                 动的手柄,只有按住拖动手柄才能使列表单元进行拖动
        animation: 600, //拖拽动画(毫秒)
        setData: function (dataTransfer) {
          dataTransfer.setData("Text", "");
        },
        // 开始拖拽
        onStart: (evt) => {
          // 更改拖拽元素的背景色
          evt.item.style.backgroundColor = "#cce8ff";
        },
        // 结束拖拽
        onEnd: (evt) => {
          const { oldIndex, newIndex } = evt;
          // 创建一个新数组,这是为了确保原数组不被直接修改
          const newDataArray = [...this.detaitableData];
          // 使用splice方法移动元素到新位置
          const movedItem = newDataArray.splice(oldIndex, 1)[0];
          newDataArray.splice(newIndex, 0, movedItem);

          // 更新组件的data
          this.detaitableData = newDataArray;
          // 打印更新后的数组
          detailOrder(this.detaitableData).then((res) => {
            if (res.status == "000000") {
              this.getBoxTables();
              this.$message.success("排序数据成功!");
            } else {
              this.$message.info(res.errors);
            }
          });
        },
      });
    },
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
el-table实现拖拽排序,可以通过以下步骤来实现: 1. 首先,确保你已经引入了element-ui库,并正确使用了el-table组件。 2. 在el-table中,给需要排序的列添加一个自定义的拖拽排序指令。可以使用vuedraggable或其他类似的拖拽库来实现。 3. 在自定义指令中,监听拖拽事件,获取拖拽的起始位置和目标位置。 4. 根据起始位置和目标位置,更新数据源中对应的数据项的顺序。 5. 在el-table中使用v-for指令遍历数据源,确保数据项的顺序与数据源一致。 下面是一个简单的示例代码: ```html <template> <el-table :data="tableData"> <el-table-column label="姓名" prop="name"> <template slot-scope="scope"> <div v-drag-sort class="drag-handle">{{ scope.row.name }}</div> </template> </el-table-column> <el-table-column label="年龄" prop="age"></el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 20 }, { name: '李四', age: 25 }, { name: '王五', age: 30 }, ], }; }, directives: { dragSort: { bind(el, binding, vnode) { const dragHandle = el.querySelector('.drag-handle'); dragHandle.addEventListener('dragstart', (event) => { event.dataTransfer.setData('text/plain', vnode.key); }); el.addEventListener('dragover', (event) => { event.preventDefault(); }); el.addEventListener('drop', (event) => { event.preventDefault(); const sourceKey = event.dataTransfer.getData('text/plain'); const targetKey = vnode.key; const sourceIndex = vnode.context.tableData.findIndex((item) => item.name === sourceKey); const targetIndex = vnode.context.tableData.findIndex((item) => item.name === targetKey); if (sourceIndex > -1 && targetIndex > -1) { const [removed] = vnode.context.tableData.splice(sourceIndex, 1); vnode.context.tableData.splice(targetIndex, 0, removed); } }); }, }, }, }; </script> ``` 这样,你就可以在el-table实现拖拽排序了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值