一、效果圖 二、代碼實現 註:需要安裝依賴 pnpm i sortablejs -S <template> <div style="padding: 15px"> <h3 style="text-align: left; padding: 10px; background: #ccc"> vue+e ...
一、效果圖
二、代碼實現
註:需要安裝依賴 pnpm i sortablejs -S
<template> <div style="padding: 15px"> <h3 style="text-align: left; padding: 10px; background: #ccc"> vue+element-ui+sortable.js表格行和列拖拽 </h3> <!-- 屬性border可實現單元格拖拽獲得欄位寬度的改變--`@header-dragend`具體參考官網的方法 --> <el-table ref="dataTable" :data="customColumnList" class="customer-table" border row-key="id" align="left" @select="handleSelectionChange" > <el-table-column type="selection" :selectable="checkSelectable" align="center" width="55" ></el-table-column> <el-table-column prop="label" label="欄位名" width="180"> <template slot-scope="scope"> {{ scope.row.label }} </template> </el-table-column> <el-table-column prop="width" label="寬度" width="180"> <template slot-scope="scope"> <div class="right-box"> <p style="padding-right: 5px">寬度</p> <el-input style="width: 60px" size="mini" type="text" v-model="scope.row.width" @keyup.enter.native="this.rowDrop()" ></el-input> <p style="padding-left: 5px">px</p> </div> </template> </el-table-column> </el-table> </div> </template>
<script> import Sortable from "sortablejs"; export default { name: "login", data() { return { customColumnList: [ { id: 1, label: "linger1", width: "220", }, { id: 2, label: "linger2", resizable: false, width: "70", }, { id: 3, label: "linger3", width: "150", }, { id: 4, label: "linger4", width: "120", resizable: false, }, { id: 5, label: "linger5", width: "200", resizable: false, }, ], hasSelectList: [1, 5, 2], }; }, mounted() { this.getTableData(); this.rowDrop(); }, methods: { getTableData() { // 預設選中 this.$nextTick(() => { this.customColumnList.forEach((row) => { if (this.hasSelectList.indexOf(row.id) >= 0) { this.$refs.dataTable.toggleRowSelection(row, true); } }); }); }, // 監聽覆選框的選擇 handleSelectionChange(section) { this.hasSelectList = section.map((item) => item.id); const List = this.customColumnList.filter((item) => section.map((items) => items.id).some((_item) => item.id === _item) ); // 排序後的列表 console.log(List); // console.log(this.hasSelectList); }, // 設置失效的行數id=5不可勾選 checkSelectable(row) { if (row.id !== 5) { return true } }, //行拖拽 rowDrop() { const tbody = document.querySelector(".el-table__body-wrapper tbody"); const _this = this; Sortable.create(tbody, { onEnd({ newIndex, oldIndex }) { const currRow = _this.customColumnList.splice(oldIndex, 1)[0]; _this.customColumnList.splice(newIndex, 0, currRow); console.log(_this.customColumnList); }, }); console.log(_this.customColumnList); }, //列拖拽 // columnDrop() { // const wrapperTr = document.querySelector(".el-table__header-wrapper tr"); // console.log("wrapperTr:", wrapperTr); // this.sortable = Sortable.create(wrapperTr, { // animation: 180, // delay: 0, // handle: ".move", // 只有帶move類名的元素才能拖動,多選框禁止拖動 // onEnd: (evt) => { // // 因為手動加了一個多選框, 不在表頭迴圈數組內, 所以這裡減1 // let oldIndx = evt.oldIndex - 1; // let newIndx = evt.newIndex - 1; // const oldItem = this.dropCol[oldIndx]; // // 真正改變列數據--變化列頭,就能實現列拖動 列數據按列頭索引取值 {{ scope.row[dropCol[index].prop] }} // this.dropCol.splice(oldIndx, 1); // 刪除舊一行 刪除為1 // this.dropCol.splice(newIndx, 0, oldItem); // 插入新一行 插入為0 // }, // }); // }, }, }; </script>
<style scoped> .right-box { display: flex; justify-content: center; align-items: center; } </style> <style> /* 取消單元格的列的border */ .el-table--border .el-table__cell { border: none; } .el-table--border, .el-table--group { border: none; } /* 去掉表格單元格邊框 */ /* 頭部邊框設置 */ /* 表格最外邊框 */ .el-table--border, .el-table--group { border: none; } .customer-table thead tr th.is-leaf{ border: 1px solid #EBEEF5; border-right: none; } .customer-table thead tr th:nth-last-of-type(2){ border-right: 1px solid #EBEEF5; } </style>