ant design vue a-table实现拖拽排序

最近在把react项目使用vue3进行重构,vue项目组件库使用ant design vue。

重构的功能包括:

1,M站菜单配置列表的展示

2,菜单列表新增

3,菜单列表的编辑

4,列表的拖拽排序

阅读ant design vue文档看到有table拖拽功能,准备直接使用,此处埋下彩蛋一颗

顺利的重构了1,2,3功能之后,照着文档配置好拖拽属性发现不生效

,翻阅源码没有找到对应的配置代码,原来拖拽功能属于付费内容,一年4999,ops~(ps:支持知识付费哈~但是我穷)

那就只能自己手写功能4了

梳理思路如下:

1,鼠标移入table行,修改改行gragable=true,修改鼠标展示样式为move

2,添加dragstart事件,在该事件中记录sourceRecord数据

3,添加dragend事件,在该事件中记录targetRecord数据

4,整体list数据中交换这两条数据,更新list

ant design vue a-table提供了customRow属性

该属性可以对每一行添加对应的样式,事件,为我们的实现思路打开了缺口。

最后代码如下:

<a-table
      :columns="columns"
      :data-source="data"
      :pagination="false"
      :customRow="customRow"
      @row-drag-end="onRowDragEnd"
    >
      <template #bodyCell="{ column, text, record }">
        <template v-if="column.dataIndex === 'weight'">
          {{ ++text }}
        </template>
        <template v-if="column.dataIndex === 'navName'">
          <div :title="record.navName" class="overflowdiv">
            {{ record.navName }}
          </div>
        </template>
        <template v-if="column.dataIndex === 'iconUrl'">
          <img class="appMenu-icon-img" :src="text" />
        </template>
        <template v-if="column.dataIndex === 'status'">
          {{ text ? "已启用" : "未启用" }}
        </template>
        <template v-if="column.dataIndex === 'operatorName'">
          <div :title="record.operatorName" class="overflowdiv">
            {{ record.operatorName }}
          </div>
        </template>
        <template v-if="column.dataIndex === 'updateTime'">
          {{ text ? text : record.createTime }}
        </template>
        <template v-if="column.dataIndex === 'operation'">
          <span class="appMenu-operation-span">
            <span @click="() => this.handleEdit(record)"
              >编辑<a-divider type="vertical"></a-divider
            ></span>
            <span @click="() => handleEnableMenu(record)">{{
              record.status ? "禁用" : "启用"
            }}</span>
          </span>
        </template>
      </template>
    </a-table>
<script>
methods: {
    customRow(record, index) {
      console.log(record, index);
      return {
        style: {
          cursor: "move",
        },
        // 鼠标移入
        onMouseenter: (event) => {
          // 兼容IE
          var ev = event || window.event;
          ev.target.draggable = true;
        },
        // 开始拖拽
        onDragstart: (event) => {
          // 兼容IE
          var ev = event || window.event;
          // 阻止冒泡
          ev.stopPropagation();
          // 得到源目标数据
          this.sourceObj = record;
        },
        // 拖动元素经过的元素
        onDragover: (event) => {
          // 兼容 IE
          var ev = event || window.event;
          // 阻止默认行为
          ev.preventDefault();
        },
        // 鼠标松开
        onDrop: (event) => {
          // 兼容IE
          var ev = event || window.event;
          // 阻止冒泡
          ev.stopPropagation();
          // 得到目标数据
          this.targetObj = record;
          console.log(this.sourceObj, this.targetObj);
          const tempDta = this.data;
          tempDta[this.targetObj.weight] = this.sourceObj;
          tempDta[this.sourceObj.weight] = this.targetObj;
          let weightList = [];
          tempDta.forEach((item, index) => {
            item.weight = index;
            weightList.push({
              id: item.id,
              weight: index,
            });
          });
          this.handleWeightModify(weightList);// 更改顺序接口
        },
      };
    }
}
</script>

至此,拖拽排序table顺利重构完成。

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现两个table相互拖拽排序可以使用ant-design-vue的`<a-transfer>`组件。具体实现步骤如下: 1. 使用`<a-transfer>`组件分别渲染两个table,设置`:data-source`和`:target-keys`属性。 ``` <a-transfer :data-source="leftTableData" :target-keys="leftSelectedKeys" :render="item => item.title" :operations="['>', '<']" :list-style="{'width': '200px'}" ></a-transfer> <a-transfer :data-source="rightTableData" :target-keys="rightSelectedKeys" :render="item => item.title" :operations="['>', '<']" :list-style="{'width': '200px'}" ></a-transfer> ``` 2. 监听`change`事件,实现数据的拖拽操作。 ``` <a-transfer :data-source="leftTableData" :target-keys="leftSelectedKeys" :render="item => item.title" :operations="['>', '<']" :list-style="{'width': '200px'}" @change="handleTransferChange" ></a-transfer> <a-transfer :data-source="rightTableData" :target-keys="rightSelectedKeys" :render="item => item.title" :operations="['>', '<']" :list-style="{'width': '200px'}" @change="handleTransferChange" ></a-transfer> ``` ``` methods: { handleTransferChange (nextTargetKeys, direction, moveKeys) { const { leftTableData, rightTableData } = this if (direction === 'right') { const moveData = leftTableData.filter(item => moveKeys.includes(item.key)) this.rightTableData = rightTableData.concat(moveData) this.leftTableData = leftTableData.filter(item => !moveKeys.includes(item.key)) } else if (direction === 'left') { const moveData = rightTableData.filter(item => moveKeys.includes(item.key)) this.leftTableData = leftTableData.concat(moveData) this.rightTableData = rightTableData.filter(item => !moveKeys.includes(item.key)) } } } ``` 3. 根据需要自定义样式和数据源。 完整代码示例: ``` <template> <div> <a-transfer :data-source="leftTableData" :target-keys="leftSelectedKeys" :render="item => item.title" :operations="['>', '<']" :list-style="{'width': '200px'}" @change="handleTransferChange" ></a-transfer> <a-transfer :data-source="rightTableData" :target-keys="rightSelectedKeys" :render="item => item.title" :operations="['>', '<']" :list-style="{'width': '200px'}" @change="handleTransferChange" ></a-transfer> </div> </template> <script> export default { data () { return { leftTableData: [ { key: '1', title: '项目1' }, { key: '2', title: '项目2' }, { key: '3', title: '项目3' }, { key: '4', title: '项目4' } ], rightTableData: [ { key: '5', title: '项目5' }, { key: '6', title: '项目6' }, { key: '7', title: '项目7' }, { key: '8', title: '项目8' } ], leftSelectedKeys: [], rightSelectedKeys: [] } }, methods: { handleTransferChange (nextTargetKeys, direction, moveKeys) { const { leftTableData, rightTableData } = this if (direction === 'right') { const moveData = leftTableData.filter(item => moveKeys.includes(item.key)) this.rightTableData = rightTableData.concat(moveData) this.leftTableData = leftTableData.filter(item => !moveKeys.includes(item.key)) } else if (direction === 'left') { const moveData = rightTableData.filter(item => moveKeys.includes(item.key)) this.leftTableData = leftTableData.concat(moveData) this.rightTableData = rightTableData.filter(item => !moveKeys.includes(item.key)) } } } } </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值