基于sortable对表格进行行列的拖拽

直接上代码:

<!--
 * @Company: wwww.glaway.com
 * @Copyright: (c) 2020. All Rights Reserved
 * @Author: zq
 * @Date: 2020-10-30 17:14:37
 * @Description: 
 * @LastEditors: zq
 * @LastEditTime: 2020-11-03 14:42:09
-->
<template>
  <div>
    <div class="table-container">
      <table border="1" cellspacing="0" class="table-parent" ref="xTable">
        <thead>
          <tr class="trtr">
            <th v-for="(item, index) in col" :key="index">{{ item.label }}</th>
          </tr>
        </thead>
        <div class="body">
          <tbody class="bodybody">
            <tr v-for="(item, index) in tableData" :key="index">
              <td>
                <i class="el-icon-menu drag-btn"></i>
              </td>
              <td v-for="(m, i) in dropCol" :key="i">{{ item[m.prop] }}</td>
            </tr>
          </tbody>
        </div>
      </table>
    </div>
  </div>
</template>

<script>
import Sortable from "sortablejs";
export default {
  data() {
    return {
      tableData: [
        {
          date: "2016",
          name: "王小虎",
          address: "上海市"
        },
        {
          date: "2017",
          name: "王小虎",
          address: "上海市普"
        },
        {
          date: "2018",
          name: "王小虎",
          address: "上海市普陀"
        },
        {
          date: "2019",
          name: "王小虎",
          address: "上海市普陀区"
        },
        {
          date: "2020",
          name: "王小虎",
          address: "上海市普陀区金"
        },
        {
          date: "2019",
          name: "王小虎",
          address: "上海市普陀区金沙"
        },
        {
          date: "2013",
          name: "王小虎",
          address: "上海市普陀区金沙江"
        },
        {
          date: "2014",
          name: "王小虎",
          address: "上海市普陀区金沙江路"
        },
        {
          date: "2015",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1"
        }
      ],
      col: [
        {
          label: "日期",
          prop: "date"
        },
        {
          label: "姓名",
          prop: "name"
        },
        {
          label: "地址",
          prop: "address"
        }
      ],
      dropCol: [
        {
          label: "日期",
          prop: "date"
        },
        {
          label: "姓名",
          prop: "name"
        },
        {
          label: "地址",
          prop: "address"
        }
      ]
    };
  },
  methods: {
    // 列拖动
    columnDrop() {
      const wrapperTr = document.getElementsByClassName("trtr")[0];
      this.sortable = Sortable.create(wrapperTr, {
        animation: 180,
        delay: 0,
        onEnd: ({ newIndex, oldIndex }) => {
          console.log(evt.oldIndex, evt.newIndex);
          const oldItem = this.dropCol[oldIndex];
          this.dropCol.splice(oldIndex, 1);
          this.dropCol.splice(newIndex, 0, oldItem);
        }
      });
    },
    // 行拖动
    rowDrop() {
      this.$nextTick(() => {
        let xTable = this.$refs.xTable;
        this.sortable = Sortable.create(
          document.getElementsByClassName("bodybody")[0],
          {
            handle: ".drag-btn",
            onEnd: ({ newIndex, oldIndex }) => {
              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;
                console.log(this.tableData);
              });
            }
          }
        );
      });
    }
  },
  mounted() {
	  getDataByPartNum("AL1.100.001", "A.124", "").then(res => {
	      // 重置一下
	      this.tableData = [];
	      this.col = [];
	      this.dropCol = [];
	
	      this.tableData = JSON.parse(res.data.obj);
	      let newArray = [];
	      for (let i in this.tableData[0]) {
	        newArray.push({ label: i, prop: i });
	      }
	      this.col = newArray;
	      this.dropCol = JSON.parse(JSON.stringify(newArray));
	    });
   		this.columnDrop();
    	this.rowDrop();
  }
};
</script>
<style lang="scss" scoped>
.table-container {
  width: 100%;
  height: 500px;
  overflow-x: auto;
  overflow-y: hidden;
  .table-parent {
    height: 500px;
    box-sizing: border-box;
    thead {
      background-color: #eee;
      tr {
        display: flex;
        height: 50px;
        float: left;
        margin-left: 258px;

        th {
          background-color: #eee;
          line-height: 50px;
          width: 200px;
          height: 50px;
          border: 1px solid #ccc;
          border-top: none;
          border-bottom: none;
        }
      }
    }
    .body {
      height: 100%;
      overflow-y: auto;
      tbody {
        height: 200px;
        overflow: auto;
        tr {
          display: flex;
          td {
            text-align: center;
            width: 200px;
            height: 50px;
            line-height: 50px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
          td:nth-child(1) {
            width: 50px;
          }
        }
      }
    }
    // ::-webkit-scrollbar {
    //   width: 0px;
    // }
  }
}
</style>

效果图:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

库库的写代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值