antdesign表格使用Sortable进行行列拖拽

本文介绍了如何在AntDesign Vue的表格组件中利用SortableJS实现行列的拖拽功能,详细讲解了配置过程和关键代码,帮助开发者提升前端交互体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<template>
  <a-table :columns="columns" :data-source="dataSource" bordered></a-table>
</template>
<script>
import Sortable from 'sortablejs'
import { defineComponent, onMounted, ref } from 'vue';
import { message } from 'ant-design-vue';

const data = [];
for (let i = 0; i < 100; i++) {
  data.push({
    key: i.toString(),
    name: `Edrward ${i}`,
    age: 32,
    address: `London Park no. ${i}`,
    operation: i
  });
}
export default defineComponent({
  setup() {
    const columns = ref([{
      title: 'name',
      dataIndex: 'name',
      width: '25%',
    }, {
      title: 'age',
      dataIndex: 'age',
      width: '15%',
    }, {
      title: 'address',
      dataIndex: 'address',
      width: '40%',
    }, {
      title: 'operation',
      dataIndex: 'operation',
    }]);
    const dataSource = ref(data);
    onMounted(() => {
      columnDrop();
      rowDrop();
    })
    const columnDrop = () => {
      const wrapperTr = document.querySelector('.ant-table-thead tr');
      Sortable.create(wrapperTr, {
        animation: 180,
        delay: 0,
        // onEnd: evt => {
        //   const oldItem = columns.value[evt.oldIndex]
        //   columns.value.splice(evt.oldIndex, 1)
        //   columns.value.splice(evt.newIndex, 0, oldItem)
        // }
        onEnd: evt => {
          const oldItem = columns.value[evt.oldIndex];
          columns.value.splice(evt.oldIndex, 1);
          columns.value.splice(evt.newIndex, 0, oldItem);
          // 不能跟第一列置换位置
          if (evt.newIndex == 0) {
            message.info('不能跟第一列置换位置。')
            const oldItem = columns.value[evt.newIndex];
            columns.value.splice(evt.newIndex, 1);
            columns.value.splice(evt.oldIndex, 0, oldItem);
            // 复原拖拽之前的 dom
            const tagName = evt.item.tagName;
            const items = evt.from.getElementsByTagName(tagName);
            if (evt.oldIndex > evt.newIndex) {
              evt.from.insertBefore(evt.item, items[evt.oldIndex + 1]);
            } else {
              evt.from.insertBefore(evt.item, items[evt.oldIndex]);
            }
          }
        }
      })
    }

    const rowDrop = () => {
      const wrapperTr = document.querySelector('.ant-table-tbody');
      Sortable.create(wrapperTr, {
        animation: 180,
        delay: 0,
        onEnd: evt => {
          const currRow = dataSource.value.splice(evt.oldIndex, 1)[0];
          dataSource.value.splice(evt.newIndex, 0, currRow);
        }
      })
    }
    return {
      dataSource,
      columns,
    };
  },
});
</script>
<style scoped>
.editable-row-operations a {
  margin-right: 8px;
}
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值