el-table合并单元格

el-tabel数据结构

此处为this.rolePermitItemList

合并后的样式:

el-table-column 需要添加property字段,属性值同props,用来判断需要合并的字段

html 复制代码
<el-table :data="rolePermitItemList" style="width: calc(100% );margin-top:10px; "
                                max-height="550" border ref="tableBoxTwo" :span-method="arraySpanMethod">
     <el-table-column :label="item.name" v-for="(item) in tableFieldTwo" :key="item.value"
         :width="item.width" :align="item.align" :property="item.value">
         <template slot="header">
             <div>
                 <span class="name">{{ item.name }}</span>
                 <span class="unit">{{ item.unit ? '(' + item.unit + ')' : '' }}</span>
             </div>
         </template>
         <template slot-scope="scope">
             <span style="font-size: 14px;font-family: MicrosoftYaHei;color: #4D4C4F;">{{
                 scope.row[item.value]
                 || '-'
             }}</span>
         </template>
     </el-table-column>
     <el-table-column align="left" :property="'peizhi'">
         <template slot="header">
             <div style="display: flex;align-items: center;justify-content: center;">
                 <span class="name">权限</span>
             </div>
         </template>
         <template slot-scope="scope">
             <div class="flex-row-nowrap flex-justify-space-around">
                 <el-checkbox v-model="scope.row.checked"
                     @change="checkboxChange(scope.row, 'two')" :data-a="responsive">{{
                         scope.row.operateName }}</el-checkbox>
             </div>
         </template>
     </el-table-column>
 </el-table>
javascript 复制代码
//方法
		data(){
			return{
				mergeObj: {}, // 用来记录需要合并行的下标
           	 	mergeArr: ['moduleName', 'pageName',], // 表格中的列名
           	 	tableFieldTwo: [
	                {
	                    name: "模块",
	                    unit: "",
	                    value: "moduleName",
	                    isNumber: false,
	                },
	                {
	                    name: "页面",
	                    unit: "",
	                    value: "pageName",
	                    isNumber: false,
	                },
	                {
	                    name: "子页面",
	                    unit: "",
	                    value: "childPageName",
	                    isNumber: false,
	                },
            	],
			}
		},
		getRolePermitItemList(roleId = '') {
            this.$api.getRolePermitItemList({ roleId: roleId }).then(res => {
                if (res.code = 200) {
                    this.rolePermitItemList = res.data
                    this.rolePermitItemList.forEach(item => {
                        item.checked = item.isChecked ? true : false
                    })
                    //拿到数据后调用getSpanArr,数据结构为el-table正常的list
                    this.getSpanArr(this.rolePermitItemList);
                }
            })
        },
     
		 getSpanArr(data) {
            this.mergeArr.forEach((key, index1) => {
                let count = 0; // 用来记录需要合并行的起始位置
                this.mergeObj[key] = []; // 记录每一列的合并信息
                data.forEach((item, index) => {
                    // index == 0表示数据为第一行,直接 push 一个 1
                    if (index === 0) {
                        this.mergeObj[key].push(1);
                    } else {
                        // 判断当前行是否与上一行其值相等 如果相等 在 count 记录的位置其值 +1 表示当前行需要合并 并push 一个 0 作为占位
                        if (item[key] === data[index - 1][key]) {
                            this.mergeObj[key][count] += 1;
                            this.mergeObj[key].push(0);
                        } else {
                            // 如果当前行和上一行其值不相等 
                            count = index; // 记录当前位置 
                            this.mergeObj[key].push(1); // 重新push 一个 1
                        }
                    }
                })
            })
            // this.mergeObj.peizhi= this.mergeObj.moduleName
            console.log('mergeObj', this.mergeObj,this.mergeArr)
        },
        arraySpanMethod({ row, column, rowIndex, columnIndex }) {
            // 判断列的属性
            if (this.mergeArr.indexOf(column.property) !== -1) {
                // 判断其值是不是为0 
                if (this.mergeObj[column.property][rowIndex]) {
                    return [this.mergeObj[column.property][rowIndex], 1]
                } else {
                    // 如果为0则为需要合并的行
                    return [0, 0];
                }
            }
        },
相关推荐
星月前端9 分钟前
[特殊字符]面向 ArcGIS for JavaScript(4.x)开发者的「坐标系统(CRS / 投影)」全面解读
开发语言·javascript·arcgis
星空的资源小屋36 分钟前
永久删除文件利器:Permadelete
java·javascript·人工智能
柒昀37 分钟前
Vue.js
前端·javascript·vue.js
2201_7578308739 分钟前
Stream的终结方法
java·服务器·前端
进阶的鱼41 分钟前
React+ts+vite脚手架搭建(五)【登录篇】
前端·javascript
safestar201242 分钟前
React深度实战:从组件抽象到性能优化的思考历程
前端·javascript·react.js
洗澡水加冰1 小时前
VSCode插件: 自动临时分配Theme以区分不同窗口
前端·typescript·visual studio code
我叫张小白。1 小时前
TypeScript类型断言与类型守卫:处理类型的不确定性
前端·javascript·typescript
阿笑带你学前端1 小时前
Flutter 实战:为开源记账 App 实现优雅的暗黑模式(Design Token + 动态主题)
前端
天渺工作室1 小时前
Chrome浏览器自带翻译的诡异Bug:ID翻译后竟然变化了
前端·chrome