使用antdv的a-table行拖拽

使用antdv的a-table行拖拽

使用a-table行拖拽,实现上下换行

sortablejs中文网

    <div ref="tableContainer">
        <a-table
                bordered
                size="middle"
                :columns="columns"
                :data-source="[{name: 1}, {name: 2}, {name: 3}]"
                :pagination="false"
                :scroll="{ x: true }"
        >
            <template slot="code" slot-scope="text, row">
                <a-input style="width: 200px" v-model="row.code" placeholder="请输入顺序号"></a-input>
            </template>

            <template slot="action" slot-scope="text, row">
                <a-icon :style="setMultStyle" style="transform: rotate(45deg);color: #1890ff;font-size: 20px;"
                        type="fullscreen"/>
            </template>
        </a-table>
    </div>
export default {
  name: 'sotrTable',
  data(){
     return {
       	  enabled:true,
		  dataSource: [{nodeName: 1}, {nodeName: 2}, {nodeName: 3}],
		  columns: [
		      {
		          title: '名称',
		          dataIndex: 'name',
		          align: 'center'
		      },
		      {
		          title: '顺序号',
		          dataIndex: 'code',
		          align: 'center',
		          scopedSlots: {customRender: 'code'}
		      },
		      {
		          title: '操作',
		          dataIndex: 'action',
		          scopedSlots: {customRender: 'action'},
		          align: 'center',
		          // fixed: 'right',
		          width: 150
		      }
		  ],
     }
  },
       computed: {
            setMultStyle() {
                return {
                    cursor: this.enabled ? 'move' : 'default'
                }
            }
        },
        mounted() {
            this.$nextTick(() => {
                let tableContainer = this.$refs.tableContainer
                this.rowDrop(tableContainer)
            })
        },
        methods: {
            rowDrop(dom) {
                console.log(dom.querySelector('.ant-table>.ant-table-content .ant-table-tbody'))
                new Sortable.create(dom.querySelector('.ant-table>.ant-table-content .ant-table-tbody'), {
                    handle: '.ant-table-row', // 行元素
                    animation: 150,
                    onEnd: ({newIndex, oldIndex}) => {
                        console.log(newIndex, oldIndex)
                        // 拖拽后回调
                        const currRow = this.dataSource.splice(oldIndex, 1)[0]
                        console.log(currRow)
                        console.log(this.dataSource[newIndex])
                        this.dataSource.splice(newIndex, 0, currRow)
                        this.dataSource.forEach((row, index) => {
                            row.sort = index + 1
                        })
                    }
                })
            },
        }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值