vue3 ant design 表格拖拽行排序实现方案

vue3 ant design 实现表格行拖拽排序功能,使用customRow api封装方法

 <a-table
      :data-source="tableData"
      :columns="tableColumns"
      :customRow="customRow"
     
 />
<script setup>
import { ref } from 'vue';
import tableRowDragSort from '@/utils/tableRowDragSort.js';
// 表格数据
const tableData = ref([
	{name: 'xx'}
]) 
// 表格列配置
const tableColumns = ref([
	{ title: '名称',dataIndex: 'name'}
]) 
// 拖拽排序
const customRow = (record, index) => {
  return tableRowDragSort(record, index, tableData.value, updateTable);
};
// 拖拽后的回调方法,可在此处执行更新数据等操作
const updateTable = () => {}
</script>

tableRowDragSort.js 代码如下

import { ref } from 'vue';
// 拖拽排序
const sourceObj = ref({});
const targetObj = ref({});
let sourceIndex;
let targetIndex;
const tableRowDragSort = (record, index, tableList, callback) => {
  const style = {
    cursor: 'pointer'
  };
  // 鼠标移入
  const onMouseenter = event => {
    // 兼容IE
    const ev = event || window.event;
    ev.target.draggable = true;
  };
  // 开始拖拽
  const onDragstart = event => {
    const ev = event || window.event;
    ev.stopPropagation();
    // 得到源目标数据
    sourceObj.value = record;
    sourceIndex = index;
  };
  // 拖动元素经过的元素
  const onDragover = event => {
    const ev = event || window.event;
    ev.preventDefault();
    ev.dataTransfer.dropEffect = 'move';
    ev.dataTransfer.effectAllowed = 'move';
    targetIndex = index;
  };
  // 拖动到达目标元素
  const onDragenter = event => {
    const ev = event || window.event;
    ev.preventDefault();
    const list = document.getElementsByClassName('ant-table-row');
    const node = document.getElementsByClassName('target');
    if (node.length) {
      node[0].classList.remove('target');
    }
    list[index].classList.add('target');
  };
  // 鼠标松开
  const onDrop = event => {
    const ev = event || window.event;
    ev.stopPropagation();
    // 得到目标数据
    targetObj.value = record;
    targetIndex = index;
    const node = document.getElementsByClassName('target');
    if (node.length) {
      node[0].classList.remove('target');
    }
    if (targetIndex === sourceIndex) return;
    tableList.splice(sourceIndex, 1);
    tableList.splice(targetIndex, 0, sourceObj.value);
    callback();
  };
  return {
    style,
    onMouseenter,
    onDragstart,
    onDragover,
    onDrop,
    onDragenter
  };
};
export default tableRowDragSort;

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
要在Ant Design Vue实现表格的拖动,你可以使用Ant Design Vue提供的Draggable组件。下面是一个示例,展示了如何使用Ant Design Vue实现表格的拖动: 1. 首先,确保你已经安装了Ant Design Vue。你可以使用以下命令来安装它: ```bash npm install ant-design-vue@next ``` 2. 在你的Vue组件中引入所需的组件和样式: ```vue <template> <div> <a-table :columns="columns" :dataSource="dataSource"> <template #body="{ row, index }"> <draggable v-model="dataSource" :element="'tr'" :options="dragOptions"> <tr :key="index"> <td>{{ row.name }}</td> <td>{{ row.age }}</td> <td>{{ row.address }}</td> </tr> </draggable> </template> </a-table> </div> </template> <script> import { Table, Draggable } from 'ant-design-vue'; import 'ant-design-vue/dist/antd.css'; export default { components: { 'a-table': Table, draggable: Draggable, }, data() { return { columns: [ { title: 'Name', dataIndex: 'name', key: 'name' }, { title: 'Age', dataIndex: 'age', key: 'age' }, { title: 'Address', dataIndex: 'address', key: 'address' }, ], dataSource: [ { key: 1, name: 'John', age: 28, address: 'New York' }, { key: 2, name: 'Alice', age: 32, address: 'London' }, { key: 3, name: 'Bob', age: 24, address: 'Paris' }, ], dragOptions: { animation: 200, handle: '.ant-table-row', }, }; }, }; </script> ``` 在上面的代码中,我们使用了Ant Design Vue的Table和Draggable组件。我们将Draggable组件包裹在表格的模板中,以实现的拖动。我们还定义了一些样式选项,例如动画效果和拖动的句柄。 这样,你就可以在Ant Design Vue实现表格的拖动了。根据你的需求,你可以自定义样式和为,以满足你的项目需求。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Nian.Baikal

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值