JSFiddle - React, Tailwind, and code Playground
HTML
<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/element-ui@2.4.5/lib/index.js"></script>
<div id="app">
<template>
<div>
<el-table
:data="tableData"
border
:span-method="objectSpanMethod"
style="width: 100%">
<template v-for="col in colData">
<el-table-column
v-if="col.prop === 'index' || col.prop === 'selection'"
align="center"
:type="col.prop"
:label="col.label"
width="50"
>
</el-table-column>
<el-table-column
v-else
align="center"
:type="col.prop"
:label="col.label"
:prop="col.prop" >
</el-table-column>
</template>
</el-table>
</div>
</template>
</div>
SCSS
@import url("//unpkg.com/element-ui@2.4.5/lib/theme-chalk/index.css");
Babel + JSX
var cache = []; //储蓄一条数据
var cacheIndex = []; //位置
var cacheData = []
var colData = [
{
prop:'id', //
label:'id',
},
{
prop:'name',
label:'姓名'
},
{
prop:'amount1',
label:'数据1'
},
{
prop:'amount2',
label:'数据2'
},
{
prop:'amount3',
label:'数据3'
},
]
var Main = {
data() {
return {
colData:colData,
tableData: [{
id: '1',
name: '王小虎',
amount1: '234',
amount2: '3.2',
amount3: 10
}, {
id: '1',
name: '王小虎',
amount1: '165',
amount2: '4.43',
amount3: 12
}, {
id: '2',
name: '王小虎',
amount1: '324',
amount2: '1.9',
amount3: 9
}, {
id: '2',
name: '王小虎1',
amount1: '621',
amount2: '33',
amount3: 17
}, {
id: '2',
name: '王小虎1',
amount1: '539',
amount2: '33',
amount3: 15
}]
};
},
methods: {
objectSpanMethod({ row, column, rowIndex, columnIndex }) {
if(cacheData.length<=0){
return false
}
let colNum = cacheData[rowIndex][columnIndex];
if (colNum < 2) {
return {
rowspan: colNum,
colspan: colNum
}
} else {
return {
rowspan: colNum,
colspan: 1
}
}
},
combine(){
this.tableData.forEach((res,i)=> {
cacheData[i] = [];
colData.forEach((item, j) => {
if (i === 0) {
cacheData[0][j] = 1;
cache = JSON.parse(JSON.stringify(res));
cacheIndex[j] = 0;
} else {
if(res[item.prop] === cache[item.prop] && item.prop !=='index'){
cacheData[cacheIndex[j]][j]++;
cacheData[i][j] = 0
}else{
...