Ant Design Vue(a-table + vue-draggable-plus)

一,导入

npm install vue-draggable-plus

二,官网

VueDraggablePlus | 支持 Vue2 和 Vue3 的拖拽组件 (vue-draggable-plus.pages.dev)

三,代码

<!--
*@TestTable
*@author JIANG L
-->
<template>

<!--      
注意,排版的时候不要用:scroll="{x:'max-content',y:'500px'}" ,
改成:style="cursor: pointer;overflow: auto"样式控制,不然会冲突。
其他参考官网,这只是基本的实现。添加的注释删掉,基本功能就能实现了
-->
    <a-table
        bordered
        :row-class-name="rowsClassName" //添加class
        :columns="columns" //行
        :dataSource="tableData1" //表数据
        style="cursor: pointer" //鼠标样式改为手
        :pagination="false" //不分页
        ref="testTable1" //表地址
        size="small" //小表
    >
        <template #bodyCell="{ text, record, index, column }">
            <!--            操作-->
        </template>
    </a-table>
            <!--            分割线-->
    <a-divider style="height: 2px; background-color: blue" />

    <a-table
        bordered
        :row-class-name="rowsClassName"
        :columns="columns"
        :dataSource="tableData2"
        style="cursor: pointer"
        :pagination="false"
        ref="testTable2"
        size="small"
    >
        <template #bodyCell="{ text, record, index, column }">
            <!--            操作-->
        </template>
    </a-table>
</template>

<script setup>
import {nextTick, onMounted, ref} from "vue";
import {useDraggable} from "vue-draggable-plus";
const columns = [
    {title: '名称', dataIndex: 'name', key: 'name', align: "center"},
    {title: '年龄', dataIndex: 'age', key: 'age', align: "center"},
    {title: '性别', dataIndex: 'sex', key: 'sex', align: "center"}
]
const tableData1 = ref([
    {name: '张三', age: '18', sex: '男'},
    {name: '李四', age: '19', sex: '女'},
    {name: '王五', age: '20', sex: '男'},
    {name: '赵六', age: '21', sex: '女'},
    {name: '孙七', age: '22', sex: '男'},
    {name: '周八', age: '23', sex: '女'},
    {name: '吴九', age: '24', sex: '男'},
    {name: '郑十', age: '25', sex: '女'}
]) ;
const tableData2 = ref([
    {name: '刘十一', age: '26', sex: '男'},
    {name: '陈十二', age: '27', sex: '女'},
    {name: '蔡十三', age: '28', sex: '男'},
    {name: '沈十四', age: '29', sex: '女'},
    {name: '苏十五', age: '30', sex: '男'},
    {name: '杜十六', age: '31', sex: '女'}
]) ;
// 加上class
function rowsClassName(record,index) {
    return (index % 2 === 1) ? 'tableRowColor cursor-move': 'cursor-move';
}

const testTable1 = ref();
const testTable2 = ref();
onMounted(async () => { // 加载
    await nextTick();
    let tbody2 = testTable2.value.$el.querySelector('tbody'); //绑定表,获取表
    useDraggable(tbody2, tableData2,  {
        animation: 150,
        group: 'people', //相同的group表示可以相互拖动
        handle: '.cursor-move',
// 修改的时候
        // onUpdate: (evt) => {
        //     console.log('onUpdate',evt)
        // },
// 添加时候
        // onAdd: (evt) => {
        //     console.log('onAdd',evt)
        //     console.log('onAdd==========',evt.clonedData)
        // },
// 排序拖动
        // onSort: event => {
        //     console.log('onSort',event)
        // }
    });

    // 自定义克隆
    let tbody1 = testTable1.value.$el.querySelector('tbody');//绑定表,获取表
    useDraggable(tbody1, tableData1,  {
        animation: 150,//拖动时显示动画
        group: {
            name: 'people',
            pull: 'clone', //方式
            put: false //表示不能放入数据
        },
        handle: '.cursor-move',
        sort: false, // 是否排序
    });
})

</script>

<style scoped>
:deep(.tableRowColor) {
    background-color: #F4F4F4;
}

</style>

要禁用 Ant Design Vue 表格(a-table)中的特定单元格,您可以使用 `customRender` 属性来自定义单元格的渲染方式。通过在自定义渲染函数中添加逻辑,您可以控制单元格是否可编辑或禁用。 下面是一个示例,演示如何禁用特定单元格: ```vue <template> <a-table :data-source="dataSource"> <a-table-column title="Name" dataIndex="name" customRender="nameRender"></a-table-column> <a-table-column title="Age" dataIndex="age" customRender="ageRender"></a-table-column> </a-table> </template> <script> export default { data() { return { dataSource: [ { name: 'John', age: 30, editable: true }, { name: 'Jane', age: 25, editable: false }, ], }; }, methods: { nameRender(text, record, index) { return { children: <span>{{ text }}</span>, attrs: { // 根据 editable 属性来判断是否禁用单元格 disabled: !record.editable, }, }; }, ageRender(text, record, index) { return { children: <span>{{ text }}</span>, attrs: { // 根据 editable 属性来判断是否禁用单元格 disabled: !record.editable, }, }; }, }, }; </script> ``` 在上面的示例中,我们定义了一个 `dataSource` 数组,其中包含两个对象,每个对象代表一行数据。在 `nameRender` 和 `ageRender` 函数中,我们使用 `customRender` 属性来自定义单元格的渲染方式。根据 `editable` 属性的值,我们决定是否禁用单元格。 请注意,上述示例中的 `customRender` 函数使用了 Vue 的 JSX 语法。如果您不熟悉 JSX,您也可以使用普通的模板语法来实现相同的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值