【vue3表格单元格拖拽】

vue3表格单元格拖拽

<script setup lang="ts">
import { defineProps } from 'vue'
import { ref } from 'vue';
import { useAutoApi } from '/@/api/overview/Automaticscheduling';
import { ElMessage } from 'element-plus';

const props = defineProps({
  schedulingItems: {
    type: Array
  }
})
const generateCalendar = (year: number, month: number) => {
  let newDay = []
  const daysInMonth = new Date(year, month, 0).getDate();
  for (let day = 1; day <= daysInMonth; day++) {
    newDay.push(day)
  }
  return newDay
};
const ViewLoading = ref(false)
const currentYear = ref(new Date().getFullYear());
const calendarData = generateCalendar(currentYear.value, 4);
const tableData = ref([
  { rowLabel: '节拍1', 1: { code: 'sdsss11', name: 'Ⅰ型', packageNum: '1/15' } },
  { rowLabel: '节拍2', 1: { code: 'sdsss71', name: 'Ⅰ型', packageNum: '7/15' } },
  { rowLabel: '节拍3', 1: { code: 'sdsss21', name: 'Ⅰ型', packageNum: '9/15' } },
  { rowLabel: '节拍4', 1: { code: 'sdsss31', name: 'Ⅰ型', packageNum: '4/15' } },
  { rowLabel: '节拍5', 1: { code: 'sdsss51', name: 'Ⅰ型', packageNum: '8/15' } },
  { rowLabel: '节拍6', 1: { code: 'sdsss41', name: 'Ⅰ型', packageNum: '5/15' } },
  { rowLabel: '节拍7', 1: { code: 'sdsss61', name: 'Ⅰ型', packageNum: '2/15' } },
])

const handleDragStart = (rowIndex: number, cellIndex: number) => {
  console.log(rowIndex, cellIndex);
  // 记录拖拽的起始位置
  event.dataTransfer?.setData('text/plain', JSON.stringify({ rowIndex, cellIndex }));
};

const handleDragOver = (event: DragEvent) => {
  event.preventDefault();
};

const handleDrop = (toRowIndex: number, toCellIndex: number) => {
  const data = JSON.parse(event.dataTransfer?.getData('text/plain') || '');
  const { rowIndex, cellIndex } = data;
  // 移动单元格数据
  const draggedCell = tableData.value[rowIndex][cellIndex];
  tableData.value[rowIndex][cellIndex] = tableData.value[toRowIndex][toCellIndex]
  tableData.value[toRowIndex][toCellIndex] = draggedCell
};

const handleDragEnd = () => {
  // 清理拖拽样式或状态
};

</script>

<template>
  <div class="container">
    <div class="calendar">
      <table>
        <thead>
          <tr class="backg">
            <th>节拍数</th>
            <th v-for="day in calendarData" :key="day">{{ ('0' + day).slice(-2) }}</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item, index) in tableData" :key="index">
            <td>{{ item.rowLabel }}</td>
            <td v-for="(cell, cellIndex) in calendarData" :key="cell" :draggable="true"
              @dragstart="handleDragStart(index, cell)" @dragover="handleDragOver" @drop="handleDrop(index, cell)"
              @dragend="handleDragEnd">
              <div class="cell-content flex-col">
                <span>
                  {{ item[cell]?.code }} {{ item[cell]?.name }}
                </span>
                <span>
                  {{ item[cell]?.packageNum }}
                </span>


              </div>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>


<style lang="scss" scoped>
.container {
  margin-top: 10px;
}

.ghost {
  border: solid 1px rgb(19, 41, 239) !important;
}

table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 10px;
  margin-bottom: 10px;
  border: 1px solid #55ABDC;
}

.backg {
  background-color: #5A6FC0;
  color: #fff;

  th {
    min-width: 150px !important;
    padding: 10px 0px;
    border: 1px solid #55ABDC;
  }
}

tbody {
  tr td {
    text-align: center;
    padding: 10px 0px;
    border: 1px solid #55ABDC;
  }
}
</style>
Vue3 Draggable是一个强大的JavaScript库,它允许你在Vue组件中轻松地实现元素的拖放功能。对于双层表格拖拽交互,你可以结合Draggable和Vue的数据绑定以及父子组件通信机制来实现。 首先,在Vue项目中安装`@Sortablejs/vue3`库,它是Draggable的一个官方推荐的集成版本,支持Vue3。然后,你可以在每个表格单元格上应用draggable属性,通过设置选项如`group`、`sort`等来指定哪些元素可以一起拖动。 例如: ```html <template> <div> <table> <thead> <tr> <!-- 表头列 --> <th v-for="column in headerColumns" :key="column.key"> {{ column.title }} </th> </tr> </thead> <tbody> <tr v-for="(row, index) in tableData" :key="index"> <td v-for="(cell, cellIndex) in row.cells" :key="cellKey(cell)"> <draggable :data="cell" group="cells" @start="onStart(cell, row, cellIndex)" @end="onEnd(cell, row, cellIndex)" ></draggable> </td> </tr> </tbody> </table> </div> </template> <script> import draggable from '@sortablejs/v3' export default { data() { return { headerColumns: [], tableData: [] } }, mounted() { draggable.create(this.$refs.tableBody, { group: 'cells' }) }, methods: { onStart(cell, row, cellIndex) { // 开始拖动时的操作,比如更新数据源的状态 }, onEnd(cell, row, cellIndex) { // 结束拖动时的操作,比如更新数据源的位置 }, cellKey(cell) { // 返回每个单元格的唯一标识 } } } </script> ``` 在这个例子中,我们创建了一个动态的表格,其中单元格可以相互拖拽。`group`属性用于将同组的元素作为一组可以互相交换位置的对象,`@start`和`@end`事件监听器则允许你响应拖动开始和结束时的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值