ant-design-vue表格实现拖拽排序及动画效果
    		       		warning:
    		            这篇文章距离上次修改已过450天,其中的内容可能已经有所变动。
    		        
        		                
                
<template>
  <a-table
    :columns="columns"
    :dataSource="data"
    :components="components"
    @update:data="onDataChange"
  >
    <template slot="name" slot-scope="name">
      {{ name }}
    </template>
  </a-table>
</template>
 
<script>
import Vue from 'vue';
import { Icon, Table } from 'ant-design-vue';
import 'ant-design-vue/dist/antd.css';
 
export default {
  components: {
    'a-icon': Icon,
    'a-table': Table,
  },
  data() {
    this.components = {
      header: {
        cell: DragableHeaderCell,
      },
    };
    return {
      data: [
        { key: '1', name: 'John', age: 32, address: 'New York No. 1 Lake Park' },
        // ...
      ],
      columns: [
        {
          title: 'Name',
          dataIndex: 'name',
          key: 'name',
          width: 100,
          // 指定该列使用自定义的渲染组件
          scopedSlots: { customRender: 'name' },
        },
        // ...
      ],
    };
  },
  methods: {
    onDataChange(newData) {
      this.data = newData;
    },
  },
};
 
// 自定义表头单元格组件
const DragableHeaderCell = Vue.extend({
  props: {
    column: Object,
    onHeaderCell: Function,
  },
  render() {
    const { column, onHeaderCell } = this;
    const props = onHeaderCell(column);
    return (
      <th {...{ props }} class="dragable-header-cell">
        { column.title }
      </th>
    );
  },
});
</script>
 
<style>
.dragable-header-cell {
  cursor: move;
}
</style>这个代码实例展示了如何在Ant Design Vue中实现一个表格的拖拽排序功能。我们定义了一个自定义的表头单元格组件DragableHeaderCell,并通过props传递了必要的信息。在onHeaderCell方法中,我们可以定义拖拽逻辑和动画效果。这个例子简化了实现,但它展示了如何将拖拽库(如vuedraggable)与Ant Design Vue组件集成的基本方法。
评论已关闭