elementUi table 的合并行列

您所在的位置:网站首页 el-table动态合并单元格 elementUi table 的合并行列

elementUi table 的合并行列

#elementUi table 的合并行列| 来源: 网络整理| 查看: 265

程序员健身是为了保养还是保命?参与话题讨论赢好礼 >>>

通过给table传入span-method方法可以实现合并行或列,方法的参数是一个对象,里面包含当前行row、当前列column、当前行号rowIndex、当前列号columnIndex四个属性。该函数可以返回一个包含两个元素的数组,第一个元素代表rowspan(合并行数),第二个元素代表colspan(合并列数)。 也可以返回一个键名为rowspan和colspan的对象。

合并列

html部分 js部分 arraySpanMethod({ row, column, rowIndex, columnIndex }) { if (rowIndex % 2 === 0) { if (columnIndex === 0) { //条件判断 return [1, 2]; //实现1行两列的合并 } else if (columnIndex === 1) { return [0, 0]; //合并之后的行列置为0 } } }

合并行

html部分 js部分 objectSpanMethod({ row, column, rowIndex, columnIndex }) { if (columnIndex === 0) { if (rowIndex % 2 === 0) { return { rowspan: 2,//合并两行为一行 colspan: 1 }; } else { return { rowspan: 0,//合并后的重置0 colspan: 0 }; } } }

动态合并

spanArr: [], // 一个空的数组,用于存放每一行记录的合并数 pos: 0, // spanArr 的索引 contentSpanArr: [], position: 0 js 合并单元格方法 objectSpanMethod({ row, column, rowIndex, columnIndex }) { if (columnIndex === 1 || columnIndex === 2) { const _row = this.spanArr[rowIndex]; const _col = _row > 0 ? 1 : 0; return { rowspan: _row, colspan: _col }; } else if (columnIndex === 3) { const _row = this.contentSpanArr[rowIndex]; const _col = _row > 0 ? 1 : 0; return { rowspan: _row, colspan: _col }; } } // spanArr/contentSpanArr记录合并的格数,pos/position 下标定位 this.spanArr = []; this.contentSpanArr = []; 遍历返回数据做合并判断 for (var i = 0; i < data.length; i++) { if (i === 0) { this.spanArr.push(1); this.pos = 0; this.contentSpanArr.push(1); this.position = 0; } else { // 判断当前元素与上一个元素是否相同(第1和第2列) if (data[i].INDEX_ID === data[i - 1].INDEX_ID) { this.spanArr[this.pos] += 1; this.spanArr.push(0); } else { this.spanArr.push(1); this.pos = i; } // 判断当前元素与上一个元素是否相同(第3列) if (data[i].CONTENT === data[i - 1].CONTENT) { this.contentSpanArr[this.position] += 1; this.contentSpanArr.push(0); } else { this.contentSpanArr.push(1); this.position = i; } }

 

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3