根据自己设定的规则,合并动态行数
线上例子(可动态点击):https://codepen.io/luoting249/pen/QWKKZPp
下面是代码片段(可把代码粘贴到element在线运行上,可看到效果):
<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/element-ui@2.14.1/lib/index.js"></script>
<div id="app">
<template>
<div>
<el-table
:data="tableData"
:span-method="objectSpanMethod"
border
style="width: 100%; margin-top: 20px">
<el-table-column
prop="amount2"
align="center"
label="数值 2(元)">
</el-table-column>
<el-table-column
prop="amount3"
align="center"
label="数值 3(元)">
</el-table-column>
<el-table-column
prop="name"
align="center"
label="姓名">
</el-table-column>
<el-table-column
align="center"
label="操作">
<template slot-scope="scope">
<a style="color:red" @click="addRule(scope.$index)">新增</a>
<template v-if="scope.$index>0">
<a style="color:red" @click="deleteRule(scope.$index)">删除</a>
</template>
</template>
</el-table-column>
</el-table>
<div class="add-special">
<el-button type="primary" size="small" @click.native="addLabelRow">复制第一大行</el-button>
</div>
</div>
</template>
</div>
var Main = {
data() {
return {
tableData: [
{
name: "王小虎",
amount2: "3.2",
amount3: 10
},
{
name: "王小虎",
amount2: "4.43",
amount3: 12
},
{
name: "王小虎",
amount2: "1.9",
amount3: 9
},
{
name: "王小虎",
amount2: "2.2",
amount3: 17
},
{
name: "王小虎",
amount2: "4.1",
amount3: 15
}
],
clueLabelMergeRule: [2, 3] // 合并规则
};
},
computed: {
arr() {
// 合并规则的累加数组
let a = [];
this.clueLabelMergeRule.reduce((prev, curr) => {
a.push(prev + curr);
return prev + curr;
}, 0);
return a;
}
},
created() {
this.pushMergeNum();
},
methods: {
pushMergeNum() {
this.clueLabelMergeRule.forEach((item, index) => {
this.tableData.forEach((el, i) => {
if (item + i === this.arr[index]) {
this.$set(el, "mergeNum", item);
}
});
});
},
addRule (index, table) {
let flag = true
this.arr.forEach((item, i) => {
if (!flag) { // 一旦加一后就不用继续循环了
return
}
// 判断点击的下标是否小于总和,如果小于,则merge的规则里加1
if (index < item) {
flag = false
this.clueLabelMergeRule.splice(i, 1, ++this.clueLabelMergeRule[i])
}
})
// 往表格里添加一行数据
this.tableData.splice(index + 1, 0, {label: [], value: '14'})
// 因为规则变了,所以要把原先push进去的mergeNum值给清空初始化
this.tableData.map(item => delete item.mergeNum)
// 重新push mergeNum值
this.pushMergeNum()
},
deleteRule (index, table) {
let flag = true
this.arr.forEach((item, i) => {
if (!flag) { // 一旦减一后就不用继续循环了
return
}
// 判断点击的下标是否小于总和,如果小于,则merge的规则里减1
if (index < item) {
flag = false
this.clueLabelMergeRule.splice(i, 1, --this.clueLabelMergeRule[i])
}
})
// 如果规则里的数有零的话就删除那一项
this.$set(this, 'clueLabelMergeRule', this.clueLabelMergeRule.filter(item => item > 0))
// 删除这行table数据
this.tableData.splice(index, 1)
// 因为规则变了,所以要把原先push进去的mergeNum值给清空初始化
this.tableData.map(item => delete item.mergeNum)
// 重新push mergeNum值
this.pushMergeNum()
},
// 复制第一大行
addLabelRow () {
// 把第一个格子里的数据复制一份push进去
let arr = JSON.parse(JSON.stringify(this.tableData.slice(0, this.clueLabelMergeRule[0])))
arr.forEach(item => {
item.users = []
})
this.tableData.push(...arr)
// 把合并规则的第一个行数push合并规则里
this.clueLabelMergeRule.push(this.clueLabelMergeRule[0])
},
objectSpanMethod({ row, column, rowIndex, columnIndex }) {
if (columnIndex === 2) {
console.log(row, column, rowIndex, columnIndex);
if (row.mergeNum) {
// 如果存在mergeNum,就开始进行合并
return {
rowspan: row.mergeNum,
colspan: 1
};
} else {
return {
rowspan: 0,
colspan: 0
};
}
}
}
}
};
var Ctor = Vue.extend(Main);
new Ctor().$mount("#app");