一、实现效果,合并的多行同一颜色,如图:
二、实现原理:根据后端数据的id,本质就是多条数据合并单元格,所以这几个数据有相同的id,相同id的数据行设置同一颜色
1、首先在tr上面添加:style="getRowStyle(value.id)"
实现动态style绑定,传id
<tr v-for="(value, index) in obj" :key="index" :style="getRowStyle(value.id)">
</tr>
2、在data中定义颜色对象
data() {
return {
// 用于跟踪已分配的颜色
usedColors: {}
};
},
3、然后实现getRowStyle方法
methods: {
getRowStyle(id) {
let color = this.usedColors[id];
if (!color) {
const colors = ['rgb(255, 255, 255)', 'rgb(244, 244, 244)'];
const index = Object.keys(this.usedColors).length % colors.length;
color = colors[index];
this.usedColors[id] = color;
}
return `background-color: ${color};`;
},
}