vxe-gantt 实现任务拖拽调整排序,手动点击排序的方法

vxe-gantt 实现任务拖拽调整排序,手动点击排序的方法

查看官网:https://vxeui.com
Github:https://github.com/x-extends/vxe-gantt
Gitee:https://gitee.com/x-extends/vxe-gantt

通过调用 moveRowTo 方法来手动移动行的顺序

请添加图片描述

代码

<template>
  <div>
    <vxe-button status="success" @click="resultEvent">获取数据</vxe-button>

    <vxe-gantt ref="ganttRef" v-bind="ganttOptions">
      <template #action="{ row, rowIndex }">
        <vxe-button mode="text" status="primary" :disabled="rowIndex <= 0" @click="modePrevRow(row)">上移</vxe-button>
        <vxe-button mode="text" status="primary" :disabled="rowIndex >= ganttOptions.data.length - 1" @click="modeNextRow(row)">下移</vxe-button>
      </template>
    </vxe-gantt>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
const ganttRef = ref()
const ganttOptions = reactive({
  border: true,
  rowConfig: {
    drag: true
  },
  rowDragConfig: {
    showIcon: false
  },
  columnConfig: {
    resizable: true
  },
  taskBarConfig: {
    showProgress: true,
    showContent: true
  },
  taskViewConfig: {
    tableStyle: {
      width: 480
    }
  },
  columns: [
    { type: 'seq', width: 70 },
    { field: 'title', title: '任务名称', minWidth: 200, dragSort: true },
    { field: 'start', title: '开始时间', width: 100 },
    { field: 'end', title: '结束时间', width: 100 },
    { field: 'action', title: '操作', fixed: 'right', width: 120, slots: { default: 'action' } }
  ],
  data: [
    { id: 10001, title: 'A项目', start: '2024-03-01', end: '2024-03-04', progress: 3 },
    { id: 10002, title: '城市道路修理进度', start: '2024-03-03', end: '2024-03-08', progress: 10 },
    { id: 10003, title: 'B大工程', start: '2024-03-03', end: '2024-03-11', progress: 90 },
    { id: 10004, title: '超级大工程', start: '2024-03-05', end: '2024-03-11', progress: 15 },
    { id: 10005, title: '地球净化项目', start: '2024-03-08', end: '2024-03-15', progress: 100 },
    { id: 10006, title: '一个小目标项目', start: '2024-03-10', end: '2024-03-21', progress: 5 },
    { id: 10007, title: '某某计划', start: '2024-03-15', end: '2024-03-24', progress: 70 },
    { id: 10008, title: '某某科技项目', start: '2024-03-20', end: '2024-03-29', progress: 50 },
    { id: 10009, title: '地铁建设工程', start: '2024-03-19', end: '2024-03-20', progress: 5 },
    { id: 10010, title: '铁路修建计划', start: '2024-03-12', end: '2024-03-20', progress: 10 }
  ]
})

const modeRow = (row, targetRow) => {
  const $gantt = ganttRef.value
  if ($gantt) {
    $gantt.moveRowTo(row, targetRow)
  }
}
const resultEvent = () => {
  const $gantt = ganttRef.value
  if ($gantt) {
    const tableData = $gantt.getFullData()
    console.log(tableData)
  }
}
</script>

https://gitee.com/x-extends/vxe-gantt

在使用 `vxe-table` 时,如果希望实现通过点击按钮调整表格行的上下排序功能,可以通过以下方式实现: ### 1. 数据源准备 `vxe-table` 支持通过操作数据源来实现行的上下移动。通常情况下,表格的数据源是一个数组,每项代表一行数据。可以通过操作数组的顺序来实现行的上下移动。 ### 2. 实现按钮点击事件 定义两个按钮,分别用于“上移”和“下移”操作。在点击按钮时,通过获取当前选中行的索引,对数组进行操作,调整其顺序。 ### 3. 示例代码 以下是一个完整的示例代码,展示如何通过按钮点击调整行的顺序: ```vue <template> <div> <el-button @click="moveUp">上移</el-button> <el-button @click="moveDown">下移</el-button> <vxe-table :data="tableData" ref="xTable"> <vxe-column type="index" title="序号"></vxe-column> <vxe-column field="name" title="名称"></vxe-column> <vxe-column field="age" title="年龄"></vxe-column> </vxe-table> </div> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 25 }, { name: '李四', age: 30 }, { name: '王五', age: 28 } ] }; }, methods: { // 上移 moveUp() { const selectedRow = this.$refs.xTable.getCheckboxRecords()[0]; // 获取选中行 if (!selectedRow) return; const index = this.tableData.findIndex(row => row === selectedRow); if (index > 0) { const temp = this.tableData[index - 1]; this.tableData[index - 1] = this.tableData[index]; this.tableData[index] = temp; } }, // 下移 moveDown() { const selectedRow = this.$refs.xTable.getCheckboxRecords()[0]; // 获取选中行 if (!selectedRow) return; const index = this.tableData.findIndex(row => row === selectedRow); if (index < this.tableData.length - 1) { const temp = this.tableData[index + 1]; this.tableData[index + 1] = this.tableData[index]; this.tableData[index] = temp; } } } }; </script> ``` ### 4. 功能说明 - **数据源操作**:通过 `tableData` 数组存储表格数据,并通过 `findIndex` 方法找到选中行的索引。 - **按钮事件**:`moveUp` 和 `moveDown` 方法分别实现上移和下移功能,通过交换数组中相邻的元素来调整顺序。 - **表格组件**:使用 `vxe-table` 渲染表格,并通过 `ref` 获取表格实例以获取选中行。 ### 5. 注意事项 - 确保在操作前获取选中行,避免未选中行时触发错误。 - 数据源的更新会自动触发表格的重新渲染,因此无需额外操作。 通过上述方法,可以轻松实现通过点击按钮调整 `vxe-table` 行的上下排序功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值