vue 中el-tablr合并相同列
1.合并相同列
:span-method 属性
<el-table v-loading="xxloading" :data="xueqianxxList" :span-method="objectSpanMethod" border fit stripe> <el-table-column label="一级指标" align="center" prop="onelever" width="200"/> <el-table-column label="二级指标" align="center" prop="twolever" width="200" /> <el-table-column label="指标内容" align="left" prop="content" width="700"/> <el-table-column label="附件依据" align="left" prop="filepath"width="500"> <template slot-scope="scope"> <el-upload ref="upload" :limit="10" :action="upload.url" :headers="upload.headers" multiple :file-list="scope.row.fileList" :on-exceed="handleExceed" :before-upload="beforeAvatarUpload" :on-remove="handleRemove" :on-progress="handleFileUploadProgress" :on-success=" (response, file, fileList) => handleFileSuccess( response, file, fileList, scope.$index ) " :on-preview="handlePreview" :auto-upload="true"> <el-button slot="trigger" size="small" type="primary" @click="savefile(scope.row)">选取文件</el-button> <!-- <el-button style="margin-left: 30px;margin-top: 20px" size="small" type="primary" :loading="upload.isUploading" @click="submitUpload">确认</el-button>--> </el-upload> </template>
js(我写的第一列与第二列,相同项都合并)
objectSpanMethod ({ row, column, rowIndex, columnIndex }) {
if (columnIndex === 0) {
// this.tableData 修改
const _row = (this.flitterData(this.xueqianxxList).one)[rowIndex];
const _col = _row > 0 ? 1 : 0;
return {
rowspan: _row,
colspan: _col
};
}
else if (columnIndex ===1) {
const _row = (this.flitterData(this.xueqianxxList).two)[rowIndex];
const _col = _row > 0 ? 1 : 0; // 如果这一行隐藏了,这列也隐藏
return {
rowspan: _row, //合并的行数
colspan: _col, //合并的列数,设为0则直接不显示
};
}
},
flitterData (arr) {
let spanOneArr = []
let concatOne = 0
let spanTwoArr = []
let concatTwo = 0
arr.forEach((item, index) => {
if (index === 0) {
spanOneArr.push(1);
spanTwoArr.push(1)
} else {
//onelever 修改
if (item.onelever === arr[index - 1].onelever) { //第一列需合并相同内容的判断条件
spanOneArr[concatOne] += 1;
spanOneArr.push(0);
} else {
spanOneArr.push(1);
concatOne = index;
};
//name 修改
if (item.twolever === arr[index - 1].twolever) { //第二列需合并相同内容的判断条件
spanTwoArr[concatTwo] += 1;
spanTwoArr.push(0);
} else {
spanTwoArr.push(1);
concatTwo = index;
};
}
});
return {
one: spanOneArr,
two: spanTwoArr,
}
},