实现行数据拖拽sortablejs

拖拽实现参考   sortablejs中文网 sortablejs官方实现参考

需求:实现列表行拖拽,比如将列表的第二行拖动到第一行,序号不变

1-安装:

npm install sortablejs

2-引入:

 import Sortable from "sortablejs";

3-vue3+elementplus+sortablejs实现,代码如下:

保持拖拽后序号不变——最终用到了比对的方法(getRowNumber:比对当前的名称在拖拽后的数组中的序号,就修改当前序号)

详见代码具体逻辑:

<script setup lang="ts">
  import { nextTick, ref, computed } from 'vue';
  import type { FormInstance } from "element-plus";
  import Sortable from "sortablejs";
  const systemStore = useSystemStore();
  // 表格数据展示 默认数据
  const tableData = ref([
    '自定义1', '自定义2', '自定义3', '自定义4',
    '自定义5', '自定义6', '自定义7', '自定义8'
  ].map((name, index) => ({
    id: String(index + 1),
    name,
    showName: name,
    isShow: false
  })));

  // 拖拽之后的数组
  const dragInfoArr = ref([...tableData.value]);
  const tableDataRef = ref < FormInstance > ();
 
  // 具体逻辑1——行拖拽实现
  const rowDrop = () => {
    nextTick(() => {
      if (terminalInfoShowRef.value) {
        const tableEl = tableDataRef.value.$el.querySelector('.el-table__body-wrapper tbody');
        Sortable.create(tableEl, {
          animation: 180,
          delay: 0,
          onEnd ({ newIndex, oldIndex }) {
          //实现得到交换的数据
            let dragItem = dragInfoArr.value.splice(oldIndex, 1);
            dragInfoArr.value.splice(newIndex, 0, ...dragItem);
          },
          filter: '.disabled-drag'
        })
      }

    });
  }
  //具体逻辑2——保持索引不变  
  const getRowNumber = (name) => {
    // 在 dragInfoArr 中查找对应名字的项
    const index = dragInfoArr.value.findIndex(item => item.name === name);
    // 如果找到了对应项,则返回该项的索引值加一;否则返回 -1
    return index !== -1 ? index + 1 : -1;
  };

</script>

<template>
  <div>
          <el-table :data="tableData" :height="600" class="border border-solid border-[1px] border-[#eee]"
            ref="tableDataRef"
            :header-cell-style="{ background: 'rgba(240,243,245,0.39)',color: '#333', fontWeight: 'bold', fontSize: '12px' }"
            empty-text="暂无数据" :cell-class-name="setCellClassName" use-virtual show-overflow-tooltip
            showBodyOverflow="tooltip" showHeaderOverflow="title" tooltip-effect="light" highlight-current-row stripe
            resizable>
            <el-table-column label="序号" fixed="left" width="60">
              <template #default="scope">
                {{ getRowNumber(scope.row.name) }}
              </template>
            </el-table-column>
            <el-table-column width="60" label="显示" align="center">
              <template #default="scope">
                <el-checkbox v-model="scope.row.isShow"></el-checkbox>
              </template>
            </el-table-column>
            <el-table-column min-width="80" prop="name" label="属性" align="center" />
            <el-table-column prop="showName" label="名称" min-width="120px">
              <template #default="scope">
                <el-input v-model="scope.row.showName" style="width:140px;"></el-input>
              </template>
            </el-table-column>
            <el-table-column min-width="40" label="调整" fixed="right">
              <template #default="scope">
                <div class="p-[10px]" @mouseenter="rowDrop">
                拖动调整
                </div>
              </template>
            </el-table-column>
          </el-table>
  </div>
</template>

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个完整的示例代码,基于Vue2,antd和sortablejs实现了两个表格之间的数据拖拽功能。 ```html <template> <div> <h2>表格1</h2> <a-table :columns="columns" :dataSource="list1" rowKey="id"> <template #name="{text, record}"> <span v-text="text" v-sortable="'table1'"></span> </template> </a-table> <h2>表格2</h2> <a-table :columns="columns" :dataSource="list2" rowKey="id"> <template #name="{text, record}"> <span v-text="text" v-sortable="'table2'"></span> </template> </a-table> </div> </template> <script> import Sortable from 'sortablejs' import { Table } from 'ant-design-vue' export default { components: { 'a-table': Table, }, data() { return { columns: [ { title: '姓名', dataIndex: 'name' }, { title: '年龄', dataIndex: 'age' }, ], list1: [ { id: 1, name: '张三', age: 18 }, { id: 2, name: '李四', age: 20 }, { id: 3, name: '王五', age: 22 }, ], list2: [], } }, mounted() { Sortable.create(this.$el.querySelector('.ant-table-tbody'), { group: { name: 'table1', put: ['table2'], }, animation: 150, fallbackOnBody: true, swapThreshold: 0.65, onEnd: this.onEnd, }) Sortable.create(this.$el.querySelectorAll('.ant-table-tbody')[1], { group: { name: 'table2', put: ['table1'], }, animation: 150, fallbackOnBody: true, swapThreshold: 0.65, onEnd: this.onEnd, }) }, methods: { onEnd(evt) { const { oldIndex, newIndex, item } = evt if (oldIndex !== newIndex) { const table = evt.to.dataset.table if (table === 'table1') { // 从表格1拖拽到表格2 this.list2.splice(newIndex, 0, this.list1.splice(oldIndex, 1)[0]) } else { // 从表格2拖拽到表格1 this.list1.splice(newIndex, 0, this.list2.splice(oldIndex, 1)[0]) } } }, }, } </script> ``` 在此示例中,我们使用了Ant Design Vue中的`<a-table>`组件来渲染表格,并在模板中使用了Vue的`v-sortable`指令来绑定Sortable实例。在mounted钩子函数中,我们为每个表格的tbody元素创建了Sortable实例,并将它们分组为“table1”和“table2”。然后,我们在onEnd方法中处理拖拽完成事件,并根据拖拽的方向和目标表格移动数据。 需要注意的是,在模板中,我们使用了`<span>`元素包装了表格中的文本数据,来实现拖拽时只能拖拽文本数据而不是整个。同时,在Sortable实例的options中,我们设置了`fallbackOnBody`为true,这样拖拽元素在拖拽结束时会回到原来的位置,而不是消失。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值