要实现同图片所示内容,前4列内容都是固定的。
渲染代码如下
<template>
<div>
<el-table :data="waterData" border :span-method="handleSpanM">
<el-table-column align="center" width="65"><template slot-scope="scope">{{scope.row.name }}</template></el-table-column>
<el-table-column align="center" width="70" label="系数">
<template slot-scope="scope"><el-input size="mini" class="" v-model="scope.row.factor"></el-input></template>
</el-table-column>
<el-table-column align="center" width="120" label="等级分数">
<template slot-scope="scope"><el-input size="mini" v-model="scope.row.grade"></el-input></template>
</el-table-column>
<el-table-column align="center" width="180" label="符号选择">
<template slot-scope="scope">
<div class="symbol">
<span style="display: none;">{{ scope.row.symbol }}</span>
<span class="symbol_range"></span>
</div>
</template>
</el-table-column>
<el-table-column width="120" v-for="(column,index) in 6" :key="`column-${index}`" ><template slot="header" slot-scope="scope"><div><el-input size="small" v-model="waterForm[`water${index + 1}_label`]" ></el-input> </div> </template> <template slot-scope="scope"><div><el-input size="small" v-model="waterForm[`water${index + 1}_factor`]" ></el-input></div> </template></el-table-column> </div></template>
<script>
export default {
data() {
return {
tableData:[
{name:'降水(mm)',factor:'0.7',grade:'',symbol: '1','':'','':'','':'','':'','':'','':''},
{name:'降水(mm)',factor:'0.7',grade:'',symbol: '1','':'','':'','':'','':'','':'','':''},
{name:'降水(mm)',factor:'0.7',grade:'',symbol: '1','':'','':'','':'','':'','':'','':''},
{name:'降水(mm)',factor:'0.7',grade:'',symbol: '1','':'','':'','':'','':'','':'','':''},
{name:'风速(m/s)',factor:'0.5',grade:'',symbol: '0','':'','':'','':'','':'','':'','':''},
{name:'风速(m/s)',factor:'0.5',grade:'',symbol: '0','':'','':'','':'','':'','':'','':''},
{name:'风速(m/s)',factor:'0.5',grade:'',symbol: '0','':'','':'','':'','':'','':'','':''},
{name:'风速(m/s)',factor:'0.5',grade:'',symbol: '0','':'','':'','':'','':'','':'','':''},
],
colFields:['name','factor','grade','symbol','','','','','',''],
spanArr:[],waterForm:{}, };
},
methods: {
getSpanArr() {
for (let i = 0; i < this.tableData.length; i++) {
let row = i;
// let col = i % this.colCount;
if (row === 0) {
// i 表示行 j表示列
for (let j = 0; j < this.colFields.length; j++) {
this.spanArr[i * this.colFields.length + j] = {
rowspan: 1,
colspan: 1,
};
}
} else {
for (let j = 0; j < this.colFields.length; j++) {
if (
this.colFields[j] == "name" ||
this.colFields[j] == "factor" ||
this.colFields[j] == "symbol"
) { // 指定合并哪些列
/*this.tableData[row]["School"] !==
this.tableData[row - 1]["School"]*/
if (
this.tableData[row][this.colFields[j]] !==
this.tableData[row - 1][this.colFields[j]]
) { // 哪些不合并:School不一样的,不合并
this.spanArr[row * this.colFields.length + j] = {
rowspan: 1,
colspan: 1,
};
} else if (
this.tableData[row][this.colFields[j]] ===
this.tableData[row - 1][this.colFields[j]]
) {
let beforeItem =
this.spanArr[(row - 1) * this.colFields.length + j];
this.spanArr[row * this.colFields.length + j] = {
rowspan: 1 + beforeItem.rowspan,// 合并几列
colspan: 1,// 合并几行
};
beforeItem.rowspan = 0;
beforeItem.colspan = 0;
} else {
// rowspan 和 colspan 都为1表格此单元格不合并
this.spanArr[row * this.colFields.length + j] = {
rowspan: 1,
colspan: 1,
};
}
}
}
}
}
// 对数据进行倒序
let stack = [];
for (let i = 0; i < this.colFields.length; i++) {
for (let j = 0; j < this.tableData.length; j++) {
// console.log("i=" + i + " j=" + j);
// i 表示列 j表示行
if (j === 0) {
if (this.spanArr[j * this.colFields.length + i].rowspan === 0) {
stack.push(this.spanArr[j * this.colFields.length + i]);
}
} else {
if (this.spanArr[j * this.colFields.length + i].rowspan === 0) {
stack.push(this.spanArr[j * this.colFields.length + i]);
} else {
stack.push(this.spanArr[j * this.colFields.length + i]);
while (stack.length > 0) {
let pop = stack.pop();
let len = stack.length;
this.spanArr[(j - len) * this.colFields.length + i] = pop;
}
}
}
}
}
// console.log(this.spanArr);
},
objectSpanMethod({ row, column, rowIndex, columnIndex }) {
return this.spanArr[rowIndex * this.colFields.length + columnIndex];
},}
},
mounted() {
this.getSpanArr();
},
渲染后结果 怎么实现效果呢前4列除了第一列和第4列不可编辑其他都是可编辑的(输入框样式的,后面的表头也设置成可编辑的了)
系统能实现效果内容,现在发现第4列怎么都不合并。
先看下效果图:
<template></template>内容没变,waterData保留你的数据,只是使用了新的表格合并方法,你可以参考
补充下:除了第一列和第四列不可编辑,后几列表头不可编辑问题