Ant design vue table 列可拖拽

安装插件

cnpm install --save vue-draggable-resizable@2.1.0

代码

<template>
  <a-table bordered :columns="columns" 
  :components="componentsa" :data-source="data">
  </a-table>
</template>
<script>
import Vue from 'vue'
import VueDraggableResizable from 'vue-draggable-resizable'
Vue.component('vue-draggable-resizable', VueDraggableResizable)
export default {
  name: 'App',
  data() {
    this.componentsa = {
      header: {
        cell: (h, props, children) => {
          const { key, ...restProps } = props
          console.log('ResizeableTitle', key)
          const col = this.columns.find(col => {
            const k = col.dataIndex || col.key
            return k === key
          })
          if (!col || !col.width) {
            return h('th', { ...restProps }, [...children])
          }
          const dragProps = {
            key: col.dataIndex || col.key,
            class: 'table-draggable-handle',
            attrs: {
              w: 10,
              x: col.width,
              z: 1,
              axis: 'x',
              draggable: true,
              resizable: false
            },
            on: {
              dragging: (x, y) => {
                col.width = Math.max(x, 1)
              }
            }
          }
          const drag = h('vue-draggable-resizable', { ...dragProps })
          return h('th', { ...restProps, class: 'resize-table-th' }, [...children, drag])
        }
      }
    }
    return {
      data: [
        {
          key: 0,
          date: '2018-02-11',
          amount: 120,
          type: 'income',
          note: 'transfer'
        },
        {
          key: 1,
          date: '2018-03-11',
          amount: 243,
          type: 'income',
          note: 'transfer'
        },
        {
          key: 2,
          date: '2018-04-11',
          amount: 98,
          type: 'income',
          note: 'transfer'
        }
      ],
      columns: [
        {
          title: 'Date',
          dataIndex: 'date',
          width: 200
        },
        {
          title: 'Amount',
          dataIndex: 'amount',
          width: 100
        },
        {
          title: 'Type',
          dataIndex: 'type',
          width: 100
        },
        {
          title: 'Note',
          dataIndex: 'note',
          width: 100
        },
        {
          title: 'Action',
          key: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ]
    }
  }
}
</script>
<style>
.resize-table-th {
  position: relative;
}
.table-draggable-handle {
  /* width: 10px !important; */
  height: 100% !important;
  left: auto !important;
  right: -5px;
  cursor: col-resize;
  touch-action: none;
  border: none;
}
</style>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用 `ant-design-vue` 的 `Table` 组件来实现的功能。 首先,在表格组件中添加 `row-key` 属性,用于指定每行数据的唯一标识。 然后,在表格组件中添加 `customRow` 属性,用于自定义行的渲染方式。在自定义行渲染函数中,可以使用 `draggable` 属性来设置是否可动。 最后,监听 `onRow` 事件,在动结束后,可以获取到新的顺序,并将其保存起来。 以下是示例代码: ```html <template> <a-table :columns="columns" :data-source="dataSource" row-key="id" :custom-row="customRow" @row="handleRow"></a-table> </template> <script> export default { data() { return { columns: [ { title: '姓名', dataIndex: 'name', key: 'name', draggable: true }, { title: '年龄', dataIndex: 'age', key: 'age', draggable: true }, { title: '地址', dataIndex: 'address', key: 'address', draggable: true }, ], dataSource: [ { id: 1, name: '张三', age: 18, address: '北京市' }, { id: 2, name: '李四', age: 20, address: '上海市' }, { id: 3, name: '王五', age: 22, address: '广州市' }, ], }; }, methods: { customRow(record, index, event) { return { on: { dragstart: (event) => { event.dataTransfer.setData('text/plain', index); event.dataTransfer.effectAllowed = 'move'; }, }, attrs: { draggable: record.draggable, }, }; }, handleRow(record, index) { return { on: { drop: (event) => { event.preventDefault(); const sourceIndex = event.dataTransfer.getData('text/plain'); if (sourceIndex !== index) { const newData = [...this.dataSource]; const sourceRecord = newData.splice(sourceIndex, 1)[0]; newData.splice(index, 0, sourceRecord); this.dataSource = newData; } }, dragover: (event) => { event.preventDefault(); }, }, }; }, }, }; </script> ``` 在上面的示例代码中,我们使用 `draggable` 属性来设置每是否可动,使用 `customRow` 属性来自定义行的渲染方式,使用 `onRow` 事件来监听动事件,并在动结束后更新数据源中的数据。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值