需 求:
一般要求分组都会进行表格合并,为了视觉上更直观。那需求就觉得这样边框线非常浅,数据多了就容易看岔劈,提出提亮合并后每组的边框线,为了能更直观区分组别。
效果图:

代 码:以下是全代码,复制运行即可出现上图效果
<template>
<el-table :data="tableData" border style="width: 50%" :span-method="spanMethod" :cell-class-name="cellclass">
<el-table-column label="序号" width="60" align="center">
<template slot-scope="scope">{{scope.row.pageIndex}}</template>
</el-table-column>
<el-table-column prop="name" label="姓名" width="180"/>
<el-table-column prop="date" label="日期" width="180"/>
<el-table-column prop="address" label="地址"/>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: [{
gropNo:1,
date: '2016-05-02',
name: '王小虎',
address: '上海市 1518 弄'
}, {
gropNo:1,
date: '2016-05-04',
name: '王小虎',
address: '上海市1517 弄'
}, {
gropNo:2,
date: '2016-05-01',
name: '王小虎1',
address: '浙江省15 弄'
}, {
gropNo:2,
date: '2016-05-03',
name: '王小虎1',
address: '浙江省 1516 弄'
}, {
gropNo:2,
date: '2016-05-08',
name: '王小虎1',
address: '浙江省1519 弄'
}, {
gropNo:3,
date: '2016-05-09',
name: '王小虎2',
address: '江苏省 1519 弄'
}],
spanArr:[] // 合并行数
}
},
mounted(){
this.tableDatas()
},
methods:{
// 给符合条件边框提亮
cellclass(data){
if(data.row.isTop) return 'topStyle'
},
// 合并行和列
spanMethod({rowIndex,columnIndex}) {
if (columnIndex === 1 || columnIndex === 0 ) {
//检查对象名称
const _row = this.spanArr[rowIndex]
const _col = _row > 0 ? 1 : 0
return {
rowspan: _row,
colspan: _col
}
}
},
// 表格合并
tableDatas() {
let contactDot = 0
let pageIndex = 1 //序号
this.spanArr = []
for (let index = 0; index < this.tableData.length; index++) {
if (index === 0) {
// 这是第一组中第一行上边框样式
// this.$set(this.tableData,index,{...this.tableData[index],isTop:1})
this.spanArr.push(1)
this.tableData[0].pageIndex = pageIndex
} else {
// 判断第二列
if (this.tableData[index].gropNo === this.tableData[index - 1].gropNo) {
this.spanArr[contactDot] += 1
this.spanArr.push(0)
} else {
// 这是其余组中第一行上边框样式
this.$set(this.tableData,index,{...this.tableData[index],isTop:1})
this.spanArr.push(1)
contactDot = index
pageIndex++
this.tableData[index].pageIndex = pageIndex
}
}
}
},
}
}
</script>
<style scoped>
::v-deep th.el-table__cell{
background-color: #66b1ffa6!important;
}
::v-deep th.el-table__cell>.cell{
color: #464748;
}
::v-deep .topStyle {
border-top:1px solid blue;
}
</style>
下面咱描述下我的思路:
首先我们根据需求可以判断这个边框肯定是和合并的组有关系,但是审查元素可以看到这个合并的组并不是在一个元素中的,那就排除了给这个合并组添加border。那思路转化一下,我们修改边框颜色都是通过cell,那我们给每组合并数据中第一行添加border-top不就可以实现视觉分割了。
那想法有了,咱就看下怎么实现,可以优先看下组件提供的原生属性,然后cell-class-name这个可以给单元格设置className的回调函数则可以解决这个问题。我们给合并组里的第一行都添加判断标识。在cell-class-name里判断满足条件的添加border-top样式即可
PS:表格合并有各种不同的方法,我上面的仅仅是其中一种,不同的方法添加标识代码的位置都不同。但是万变不离其宗,我的思路是只要给每个合并组中第一个添加标志那就完成了主要步骤,cell-class-name的判断依旧是相同的。所有代码都是为思路逻辑服务的。
以上,over!欢迎讨论~


2719

被折叠的 条评论
为什么被折叠?



