文章目录
- html代码
- script代码
- arraySpanMethod.js代码
html代码
<template>
<div class="rightBar">
<cl-table
ref="tableData"
border
:span-method="arraySpanMethod"
:data="tableData"
:columns="columns"
:max-height="maxHeight"
row-key="id"
cell-default>
</cl-table>
</div>
</template>
script代码
import arraySpanMethod from '@/mixin/arraySpanMethod'
export default{
mixins: [arraySpanMethod],
data(){
tableData: [],
maxHeight: 500,
columns:[
{
label: "业务大类",
prop: "busi",
align: "center",
render: (vn, row, index) => {
return (
<div>
{row.busi && <span>{row.busi}</span>}
{!row.busi && <span style="color:#cccccc">--</span>}
</div>
)
}
},
{
label: "业务细类",
prop: "busiDetail",
align: "center",
render: (vn, row, index) => {
return (
<div>
{row.busiDetail && <span>{row.busiDetail}</span>}
{!row.busiDetail && <span style="color:#cccccc">--</span>}
</div>
)
}
},
]
},
watch: {
tableData: {
deep: true,
immediate: true,
handler: function (val) {
if (val.length) {
this.$nextTick(() => {
var ss = this.mergeCell(this.$refs.tableData, ['busi', 'busiDetail'], true, true)
this.mergeMap = ss
})
}
}
}
},
methods:{
resize() {
this.$nextTick(() => {
this.maxHeight = this.$refs['rightBar'].clientHeight - 140
})
},
},
mounted() {
this.resize();
window.addEventListener('resize', this.resize)
},
destroyed() {
window.removeEventListener('resize', this.resize)
},
}
arraySpanMethod.js代码
export default {
data() {
return {
mergeMap: []
}
},
methods: {
arraySpanMethod({ rowIndex, columnIndex }) {
var map = this.mergeMap
try {
var r = map[rowIndex][columnIndex]
return r
} catch (error) {
return r
}
},
mergeCell(table, colMap, isSpanRow = true, isSpanCol = true) {
var list = table.data
var columns = []
for (const item of table.columns) {
columns.push(item.prop)
}
var result = []
for (let row = 0, rowLen = list.length; row < rowLen; row++) {
const ss = []
for (let col = 0, colLen = columns.length; col < colLen; col++) {
ss.push([1, 1])
}
result.push(ss)
}
if (isSpanRow) {
result = this.mergeRow(list, columns, colMap, result)
}
if (isSpanCol) {
result = this.mergeCol(list, columns, colMap, result)
}
return result
},
mergeRow(list, columns, colMap, result) {
for (const item of colMap) {
const p = {
colKey: item,
col: columns.indexOf(item),
row: 0,
val: ''
}
for (let row = 0, rowLen = list.length; row < rowLen; row++) {
if (p.val === list[row][p.colKey]) {
result[p.row][p.col][0] += 1
result[row][p.col][0] = 0
} else {
p.row = row
p.val = list[row][p.colKey]
}
}
}
return result
},
mergeCol(list, columns, colMap, result) {
for (let row = 0, rowLen = list.length; row < rowLen; row++) {
const p = {
col: 0,
row: row,
val: ''
}
for (let col = 0, colLen = columns.length; col < colLen; col++) {
if (!colMap.includes(columns[col])) {
p.col = col
p.val = list[row][columns[col]]
continue
}
if (p.val === list[row][columns[col]]) {
result[p.row][p.col][1] += 1
result[p.row][col][1] = 0
} else {
p.col = col
p.val = list[row][columns[col]]
}
}
}
return result
}
}
}