vue+element用於pc後臺管理系統比較多,因此後臺管理系統通常以處理數據爲主,數據結構的複雜程度變高,相對應的前端展現成本也提升,
有些產品經理或許會要求表格跨行或跨列合併,若是你正在想怎麼實現,那就接着往下看
最新封裝了一個表格合併和編輯插件:vue-split-table,戳一戳
效果圖html
(注意是2.X新加的方法)前端
能夠實現合併行或列,vue
方法的參數是一個對象,裏面包含當前行row、當前列column、當前行號rowIndex、當前列號columnIndex四個屬性。segmentfault
該函數能夠返回一個包含兩個元素的數組,第一個元素表明rowspan,第二個元素表明colspan。 也能夠返回一個鍵名爲rowspan和colspan的對象數組
arraySpanMethod({ row, column, rowIndex, columnIndex }) { if (rowIndex % 2 === 0) {//判斷條件能夠設置成你想合併的行的起始位置 if (columnIndex === 0) {//判斷條件能夠設置成你想合併的列的起始位置 return [1, 2]; } else if (columnIndex === 1) { return [0, 0]; } } },
return [1,2]也能夠返回一個對象數據結構
return {
rowspan: 2,//實際上就是給td加上rowspan屬性
colspan: 1//實際上就是給td加上colspan屬性
};ide
<template> <div> <el-table :data="tableData6" :span-method="arraySpanMethod" border style="width: 100%"> <el-table-column prop="id" label="ID" width="180"> </el-table-column> <el-table-column prop="name" label="姓名"> </el-table-column> <el-table-column prop="amount1" sortable label="數值 1"> </el-table-column> <el-table-column prop="amount2" sortable label="數值 2"> </el-table-column> <el-table-column prop="amount3" sortable label="數值 3"> </el-table-column> </el-table> <el-table :data="tableData6" :span-method="objectSpanMethod" border style="width: 100%; margin-top: 20px"> <el-table-column prop="id" label="ID" width="180"> </el-table-column> <el-table-column prop="name" label="姓名"> </el-table-column> <el-table-column prop="amount1" label="數值 1(元)"> </el-table-column> <el-table-column prop="amount2" label="數值 2(元)"> </el-table-column> <el-table-column prop="amount3" label="數值 3(元)"> </el-table-column> </el-table> </div> </template> <script> export default { data() { return { tableData6: [{ id: '12987122', name: '王小虎', amount1: '234', amount2: '3.2', amount3: 10 }, { id: '12987123', name: '王小虎', amount1: '165', amount2: '4.43', amount3: 12 }, { id: '12987124', name: '王小虎', amount1: '324', amount2: '1.9', amount3: 9 }, { id: '12987125', name: '王小虎', amount1: '621', amount2: '2.2', amount3: 17 }, { id: '12987126', name: '王小虎', amount1: '539', amount2: '4.1', amount3: 15 }] }; }, methods: { arraySpanMethod({ row, column, rowIndex, columnIndex }) { console.log(row,column) if (row.id=='12987122') { if (columnIndex === 0) { return [2, 2]; } else if (columnIndex === 1) { return [0, 0]; } } }, objectSpanMethod({ row, column, rowIndex, columnIndex }) { if (columnIndex === 0) { if (rowIndex % 2 === 0) { return { rowspan: 2, colspan: 1 }; } else { return { rowspan: 0, colspan: 0 }; } } } } }; </script>
直接在對應的td裏面嵌套<table>的<tr>讓後臺對應返回一個數組,遍歷便可實現單元格拆分函數
強烈推薦方法二,這個實現成本最低,也便添加複選框進行增刪改查ui
直接能夠演示編碼
<table class="ground-route-table"> <thead> <tr> <td>城市</td> <td>美食</td> <td>好玩的地方</td> </tr> </thead> <tbody> <tr> <td>北京</td> <td>北京烤鴨</td> <td> <table class="ground-route-table-small"> <tr>故宮</tr> <tr>頤和園</tr> <tr>長城</tr> </table> </td> </tr> </tbody> </table> <style> .ground-route-table, .ground-route-table-samll { width: 100%; border: 1px solid #dfe6ec; border-collapse: collapse; } .ground-route-table td{ border: 1px solid #dfe6ec; } </style>
屬性colspan和rowspan實現合併行或列
可能有些項目是使用的element1.x版本,若是忽然升級風險過高,我作這個就是,因此仍是利用原生的table
的colspan和rowspan
原生的難點在於table都是經過循環產生的,若是直接經過設置類設置樣式,這樣表格就會變亂,由於v-for下面每一個td都建立了,因此要在v-for裏面進行判斷
colspan和rowspan的數據是應該是動態的,那麼他們怎麼動態綁定呢,可能會想到操做DOM,
可是這不是最好的方法,咱們能夠經過自定義指令將屬性與值關聯起來
mergerows: { // 指令的定義 inserted: function (el) { el.setAttribute('rowspan',3) } }
貼上詳解:https://cn.vuejs.org/v2/guide...
很高興你還能看到這裏,有啥問題能夠一塊兒交流,若是以爲有點用,能夠先收藏起來呢
<template> <table class="ground-route-table"> <thead> <tr> <td>城市</td> <td>班次編碼</td> <td>車輛編碼</td> <td>順序</td> <td>裝車點</td> <td>到車點</td> <td>最晚到達時間</td> <td>發車時間</td> <td>到車時間</td> <td>OMP配載代碼</td> <td>工做日</td> <td>線路類型</td> <td>線路類型</td> <td>資源類型</td> <td>車牌號</td> <td>司機ID</td> <td>司機姓名</td> <td>司機電話</td> <td>路線執行日期</td> </tr> </thead> <tbody> <tr v-for="(routeList,index) in groundRouteDataEnd" :key="index"> <td v-mergerows v-if="index!=1&&index!=2&&index!=4&&index!=5&&index!=7&&index!=8"><el-checkbox></el-checkbox></td> <td v-mergerows v-if="index!=1&&index!=2&&index!=4&&index!=5&&index!=7&&index!=8">{{routeList.groundRoute1}}</td> <td v-mergerows v-if="index!=1&&index!=2&&index!=4&&index!=5&&index!=7&&index!=8">{{routeList.groundRoute2}}</td> <td v-mergerows v-if="index!=1&&index!=2&&index!=4&&index!=5&&index!=7&&index!=8">{{routeList.groundRoute3}}</td> <td v-mergerows v-if="index!=1&&index!=2&&index!=4&&index!=5&&index!=7&&index!=8">{{routeList.groundRoute4}}</td> <td>{{routeList.groundRoute5}}</td> <td>{{routeList.groundRoute6}}</td> <td>{{routeList.groundRoute7}}</td> <td>{{routeList.groundRoute8}}</td> <td>{{routeList.groundRoute9}}</td> <td>{{routeList.groundRoute10}}</td> <td>{{routeList.groundRoute11}}</td> <td>{{routeList.groundRoute12}}</td> <td>{{routeList.groundRoute13}}</td> <td v-mergerows v-if="index!=1&&index!=2&&index!=4&&index!=5&&index!=7&&index!=8"> <el-button type="primary" size="mini">查看</el-button> </td> </tr> </tbody> </table> </template> <script> export default { data() { return { groundRouteDataEnd: [ { groundRoute1: "10", groundRoute2: "10", groundRoute3: "10", groundRoute4: "10", groundRoute5: "10", groundRoute6: "10", groundRoute7: "10", groundRoute8: "10", groundRoute9: "10", groundRoute10: "10", groundRoute11: "11" }, { groundRoute1: "10", groundRoute2: "10", groundRoute3: "10", groundRoute4: "10", groundRoute5: "10", groundRoute6: "10", groundRoute7: "10", groundRoute8: "10", groundRoute9: "10", groundRoute10: "10", groundRoute11: "11" }, { groundRoute1: "10", groundRoute2: "10", groundRoute3: "10", groundRoute4: "10", groundRoute5: "10", groundRoute6: "10", groundRoute7: "10", groundRoute8: "10", groundRoute9: "10", groundRoute10: "10", groundRoute11: "11" }, { groundRoute1: "10", groundRoute2: "40", groundRoute3: "10", groundRoute4: "10", groundRoute5: "10", groundRoute6: "10", groundRoute7: "10", groundRoute8: "10", groundRoute9: "10", groundRoute10: "10", groundRoute11: "11" } ] }; }, directives: { mergerows: { // 指令的定義 inserted: function(el) { el.setAttribute("rowspan", 3); } } } }; </script> <style scoped> .ground-route-table, .ground-route-table-samll { width: 100%; border: 1px solid #dfe6ec; border-collapse: collapse; } .ground-route-table td{ border: 1px solid #dfe6ec; } </style>