vue:遇到的坑之-----table拖拽(element-ui)

效果如下:

拖拽情况:

1、表格嵌套,子表进行拖拽,不能跨表拖拽,父表不能拖拽,引用了sortable.js插件

以下是嵌套表格拖拽代码:

html:

<template>
  <el-table :data="tableData" style="width: 100%" border @expand-change="expandChange" default-expand-all>
    <el-table-column type="expand" width="45">
      <template slot-scope="scope">
        <el-table :data="scope.row.children" border class="expandTable">
          <el-table-column prop="date" label="日期2" width="180">
          </el-table-column>
          <el-table-column prop="name" label="姓名2" width="180">
          </el-table-column>
          <el-table-column prop="address" label="地址2">
          </el-table-column>
        </el-table>
      </template>
    </el-table-column>
    <el-table-column prop="date" label="日期" width="180">
    </el-table-column>
    <el-table-column prop="name" label="姓名" width="180">
    </el-table-column>
    <el-table-column prop="address" label="地址">
    </el-table-column>
  </el-table>
</template>

js:

import Sortable from 'sortablejs'
export default {
  data() {
    return {
      arr: [
        {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄',
          children: [{
            date: '2016-05-04',
            name: '1',
            address: '上海市普陀区金沙江路 1517 弄2'
          }, {
            date: '2016-05-04',
            name: '2',
            address: '上海市普陀区金沙江路 1517 弄2'
          }, {
            date: '2016-05-04',
            name: '3',
            address: '上海市普陀区金沙江路 1517 弄2'
          }]
        }, {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄',
          children: [{
            date: '2016-05-04',
            name: '4',
            address: '上海市普陀区金沙江路 1517 弄2'
          }, {
            date: '2016-05-04',
            name: '5',
            address: '上海市普陀区金沙江路 1517 弄2'
          }, {
            date: '2016-05-04',
            name: '6',
            address: '上海市普陀区金沙江路 1517 弄2'
          },
          {
            date: '2016-05-04',
            name: '8',
            address: '上海市普陀区金沙江路 1517 弄2'
          }]
        }, {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄',
          children: [{
            date: '2016-05-04',
            name: '9',
            address: '上海市普陀区金沙江路 1517 弄2'
          }, {
            date: '2016-05-04',
            name: '10',
            address: '上海市普陀区金沙江路 1517 弄2'
          }, {
            date: '2016-05-04',
            name: '11',
            address: '上海市普陀区金沙江路 1517 弄2'
          },
          {
            date: '2016-05-04',
            name: '12',
            address: '上海市普陀区金沙江路 1517 弄2'
          }]
        }, {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄'
        }
      ], // 模拟数据
      tableData: [], //表格数据
      tempTableData: [], // 暂存表格数据
      tbodyList:null, //表格html
    }
  },
  created() {
    this.getTableData()
  },
  mounted() {
    // 拿到所有子表的html,并注册拖拽事件,如果子表不是默认全部展开的话,可以不用写这一步骤
    this.$nextTick(()=>{
      this.tbodyList = document.querySelectorAll('.expandTable .el-table__body-wrapper tbody');
      this.dropTable(this.tbodyList);
    })
  },
  methods: {
    getTableData() {
      // 模拟请求获取表格数据
      //this.$axios(`url`).then(res => {
        this.tableData = this.arr;
        this.tempTableData = this.arr; //存起来,拖拽后进行对比
      //})
    },
    // 表格展开重新渲染
    expandChange() {
      this.$nextTick(() => {
        this.tbodyList = document.querySelectorAll('.expandTable .el-table__body-wrapper tbody');
        this.dropTable( this.tbodyList)
      })
    },

    //行拖拽
    dropTable(tbodyList) {
      let _this = this;
      let tbody = tbodyList;
      console.log(tbody);
      
      for (let i = 0; i < tbody.length; i++) {
        Sortable.create(tbody[i], {
          onEnd({ newIndex, oldIndex }) {
            console.log(11111111);
            
            // 手误拖拽,不执行后面操作
            if (newIndex == oldIndex) return;
            // 重新渲染
            _this.tableData[i]['children'].splice(newIndex, 0, _this.tableData[i]['children'].splice(oldIndex, 1)[0]);
            var newArray = _this.tableData[i]['children'].slice(0);
            _this.tableData[i]['children'] = [];
            _this.$nextTick(function () {
              _this.tableData[i]['children'] = newArray;
              _this.dropTableSendRequest(i);// i 为父表下标
            });
          },
        })
      }
    },

    // 将数据传到后台
    dropTableSendRequest(i) {
      // 找到拖拽子表的父表
      let temparr = this.tempTableData.find((item, index) => {
        if (index == i) {
          return item
        }
      })
      console.log(temparr); 
      // url,data按需传
      //this.axios.post('url', 'data').then(res => {
        this.getTableData();
       // this.$message({
       //  type: res.data.type,
       //  message: res.data.message
       // })
      //})
    }

  }
}

2、单表拖拽 

单表拖拽比较简单,以下是代码

html:

<template>
  <el-table :data="tableData" style="width: 100%" border>
    <el-table-column prop="date" label="日期" width="180">
    </el-table-column>
    <el-table-column prop="name" label="姓名" width="180">
    </el-table-column>
    <el-table-column prop="address" label="地址">
    </el-table-column>
  </el-table>
</template>

js:

import Sortable from 'sortablejs'
export default {
  data() {
    return {
      arr: [
        {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄',
        }, {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄',
        }, {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄',
        }, {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄'
        }
      ], // 模拟数据
      tableData: [], //表格数据
      tempTableData: [], // 暂存表格数据
      tbodyList:null, //表格html
    }
  },
  created() {
    this.getTableData()
  },
  mounted() {
    // 拿到所有子表的html,并注册拖拽事件,如果子表不是默认全部展开的话,可以不用写这一步骤
    this.$nextTick(()=>{
      this.tbodyList = document.querySelectorAll('.expandTable .el-table__body-wrapper tbody');
      this.dropTable(this.tbodyList);
    })
  },
  methods: {
    getTableData() {
      // 模拟请求获取表格数据
      //this.$axios(`url`).then(res => {
        this.tableData = this.arr;
        this.tempTableData = this.arr; //存起来,拖拽后进行对比
      //})
    },

    //行拖拽
    dropTable() {
      tbody = document.querySelector('.el-table__body-wrapper tbody');
      Sortable.create(tbody, {
        onEnd({ newIndex, oldIndex }) {
          if (newIndex == oldIndex) return;

          this.tableData.splice(newIndex, 0, this.tableData.splice(oldIndex, 1)[0]);
          var newArray = this.tableData.slice(0);
          this.tableData= [];
          this.$nextTick(function () {
            this.tableData= newArray;
            this.dropTableSendRequest(oldIndex, newIndex)
          });
        }
      })
    },

    // 将数据传到后台
    dropTableSendRequest(oldIndex,newIndex) { 
      // url,data按需传
      //this.axios.post('url', 'data').then(res => {
        this.getTableData();
       // this.$message({
       //  type: res.data.type,
       //  message: res.data.message
       // })
      //})
    }

  }
}

有问题欢迎留言=3=

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
如果您想要实现Element Table树形表格动功能,可以考虑使用Vue.js框架并结合element-ui组件库,再使用一些自定义指令和事件来实现。 以下是一种实现方式: 1. 在Vue组件中引入element-ui组件库,并在模板中使用el-table组件,设置其tree-props属性为{children: 'children', hasChildren: 'hasChildren'},以支持树形结构。 2. 使用自定义指令v-draggable,在表格行上绑定该指令,使得行可以被动。 3. 监听el-table组件的row-drop事件,在该事件中更新数据源,以实现拖拽行的排序。 下面是示例代码: ```html <template> <el-table :data="tableData" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" @row-drop="handleRowDrop" > <el-table-column prop="name" label="名称"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> <el-table-column label="操作"> <template slot-scope="scope"> <button v-if="scope.row.hasChildren" @click="toggleNode(scope.row)"> {{ scope.row.expanded ? '收起' : '展开' }} </button> </template> </el-table-column> <el-table-column label="动" width="80"> <template slot-scope="scope"> <div v-draggable class="drag-handle"> <i class="el-icon-s-grid"></i> </div> </template> </el-table-column> </el-table> </template> <script> import { directive } from 'vuedraggable' export default { directives: { draggable: directive }, data() { return { tableData: [ { name: '张三', age: 18, address: '北京市朝阳区', children: [ { name: '李四', age: 22, address: '北京市海淀区', children: [], hasChildren: false } ], hasChildren: true }, { name: '王五', age: 30, address: '上海市浦东区', children: [], hasChildren: false } ] } }, methods: { toggleNode(node) { this.$refs.table.toggleRowExpansion(node) }, handleRowDrop(event) { const { treeData, newIndex, oldIndex } = event // 更新数据源 treeData.splice(newIndex, 0, treeData.splice(oldIndex, 1)[0]) } } } </script> <style> .drag-handle { cursor: move; display: inline-block; padding: 5px; border: 1px solid #ccc; border-radius: 4px; text-align: center; } </style> ``` 在上面的代码中,我们首先引入了vuedraggable库中的directive指令,并将其作为自定义指令draggable注册到Vue实例中。 在模板中,我们将el-table组件的tree-props属性设置为指定的树形结构字段,以支持树形表格的展示。然后在动列中使用v-draggable指令,使得行可以被拖拽。 最后,我们监听了el-table组件的row-drop事件,在事件中更新数据源,以实现行的拖拽排序。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值