el-table 行合并的實(shí)現(xiàn)示例
頁(yè)面實(shí)現(xiàn)行合并的情況還是比較常見(jiàn)的,但實(shí)現(xiàn)起來(lái)卻有一點(diǎn)點(diǎn)難。官網(wǎng)的例子有,不過(guò)人家的合并邏輯是從第一行開(kāi)始兩行兩行合并,不能根據(jù)數(shù)據(jù)動(dòng)態(tài)合并,感覺(jué)參考意義不太大。實(shí)際需求一般都是根據(jù)表數(shù)據(jù)動(dòng)態(tài)來(lái)進(jìn)行合并的,也會(huì)有更復(fù)雜的情況,比如循環(huán)表格的。
以下的例子中,表格數(shù)據(jù)是放在頁(yè)面的假數(shù)據(jù),hbxh 值相同時(shí),表示需要合并
示例一:?jiǎn)蝹€(gè)表格
單個(gè)表格的比較簡(jiǎn)單,知道合并方法的使用基本就好弄了
<template>
<div>
<el-form
ref="testForm"
:model="testForm"
style="height: 600px;"
>
<el-row
style="padding: 10px"
>
<el-table
:data="testForm.tableData"
:span-method="colSpanMethod"
border
style="width: 100%;"
>
<el-table-column prop="hbxh" label="合并序號(hào)" width="100" />
<el-table-column prop="name" label="姓名" width="180" />
<el-table-column prop="address" label="地址" />
</el-table>
</el-row>
</el-form>
</div>
</template>
<script>
export default {
data() {
return {
spanArr: [], // 用于存放每一行記錄的合并數(shù)
testForm: {
tableData: [
{
hbxh: '1',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1518 弄'
}, {
hbxh: '2',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1517 弄'
}, {
hbxh: '2',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1519 弄'
}, {
hbxh: '3',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1516 弄'
}, {
hbxh: '4',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1516 弄'
}, {
hbxh: '4',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1516 弄'
}, {
hbxh: '4',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1516 弄'
}
] // 測(cè)試數(shù)據(jù)
},
}
},
created() {
this.getSpanArr(this.testForm.tableData)
},
methods: {
getSpanArr(data) {
// data就是我們從后臺(tái)拿到的數(shù)據(jù)
for (var i = 0; i < data.length; i++) {
if (i === 0) {
this.spanArr.push(1);
this.pos = 0;
} else {
// 判斷當(dāng)前對(duì)象的指定屬性值與上一個(gè)對(duì)象的指定屬性值是否相等
if (data[i].hbxh === data[i - 1].hbxh) {
this.spanArr[this.pos] += 1;
this.spanArr.push(0);
} else {
this.spanArr.push(1);
this.pos = i;
}
}
}
},
colSpanMethod({ row, column, rowIndex, columnIndex }) {
if (columnIndex === 0 || columnIndex === 2) {
const _row = this.spanArr[rowIndex];
const _col = _row > 0 ? 1 : 0;
console.log(`rowspan:${_row} colspan:${_col}`);
return {
// [0,0] 表示這一行不顯示, [2,1]表示行的合并數(shù)
rowspan: _row,
colspan: _col
};
}
}
}
}
</script>
效果:

示例二:循環(huán)表格
循環(huán)表格時(shí),需要在表的每行數(shù)據(jù)傳入一個(gè)值,代表是第幾個(gè)表格,方便合并方法使用
<template>
<div>
<el-form
ref="testForm"
:model="testForm"
style="height: 600px;"
>
<el-row
v-for="(item, index) in testForm.tableList"
:key="index"
style="padding: 10px"
>
<el-table
:data="item"
:span-method="colSpanMethod"
border
style="width: 100%; margin-bottom: 20px;"
>
<el-table-column prop="hbxh" label="合并序號(hào)" width="100" />
<el-table-column prop="name" label="姓名" width="180" />
<el-table-column prop="address" label="地址" />
</el-table>
</el-row>
</el-form>
</div>
</template>
<script>
export default {
data() {
return {
spanArr: [], // 用于存放每一行記錄的合并數(shù)
testForm: {
tableList: [
[
{
hbxh: '1',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1518 弄',
tbIndex: 0
}, {
hbxh: '1',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1517 弄',
tbIndex: 0
}, {
hbxh: '2',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1519 弄',
tbIndex: 0
}, {
hbxh: '2',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1516 弄',
tbIndex: 0
}
],
[
{
hbxh: '1',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1518 弄',
tbIndex: 1
}, {
hbxh: '2',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1517 弄',
tbIndex: 1
}, {
hbxh: '2',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1519 弄',
tbIndex: 1
}, {
hbxh: '3',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1516 弄',
tbIndex: 1
}, {
hbxh: '4',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1516 弄',
tbIndex: 1
}, {
hbxh: '4',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1516 弄',
tbIndex: 1
}, {
hbxh: '4',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1516 弄',
tbIndex: 1
}
]
] // 測(cè)試數(shù)據(jù)
},
}
},
created() {
this.getSpanArr(this.testForm.tableList)
},
methods: {
getSpanArr(data) {
// 用一個(gè)對(duì)象數(shù)組來(lái)存放每個(gè)表中每一行記錄的合并數(shù)
for (let m = 0, n = data.length; m < n; m++) {
this.spanArr.push({
tbArr: []
});
}
for (let i = 0, l = data.length; i < l; i++) {
let contactDot = 0; // 記錄開(kāi)始合并的行索引
data[i].forEach((item, index) => {
item.index = index
if (index === 0) {
this.spanArr[i].tbArr.push(1);
} else {
// 判斷當(dāng)前對(duì)象的指定屬性值與上一個(gè)對(duì)象的指定屬性值是否相等
if (item.hbxh === data[i][index - 1].hbxh) {
this.spanArr[i].tbArr[contactDot] += 1;
this.spanArr[i].tbArr.push(0);
} else {
this.spanArr[i].tbArr.push(1);
contactDot = index;
}
}
})
}
console.log('==========this.spanArr==========')
console.log(this.spanArr)
/* [
[2, 0, 2, 0],
[1, 2, 0, 1, 3, 0, 0]
] */
},
colSpanMethod({ row, column, rowIndex, columnIndex }) {
if (columnIndex === 2) {
const _row = this.spanArr[row.tbIndex].tbArr[rowIndex];
const _col = _row > 0 ? 1 : 0;
return {
// [0,0] 表示這一行不顯示, [2,1]表示行的合并數(shù)
rowspan: _row,
colspan: _col
};
}
}
}
}
</script>
有循環(huán)的需求時(shí),數(shù)據(jù)結(jié)構(gòu)會(huì)比較復(fù)雜,需要耐心理清數(shù)據(jù)之間的關(guān)系
效果:

合并的關(guān)鍵主要就是要準(zhǔn)確計(jì)算出每行記錄的合并數(shù),計(jì)算邏輯可能每個(gè)人會(huì)想得不太一樣,以上的栗子僅代表一種實(shí)現(xiàn)方式,不同方式的朋友可以留言討論
>>>>>>>>>今天又發(fā)現(xiàn)了一種新的邏輯>>>>>>>>>>>>>(2021-11-29)
<template>
<div>
<el-form
ref="testForm"
:model="testForm"
style="height: 600px;"
>
<el-row
v-for="(item, index) in testForm.tableList"
:key="index"
style="padding: 10px"
>
<el-table
:data="item"
:span-method="colSpanMethod2"
border
style="width: 100%; margin-bottom: 20px;"
>
<el-table-column prop="hbxh" label="合并序號(hào)" width="100" />
<el-table-column prop="name" label="姓名" width="180" />
<el-table-column prop="address" label="地址" />
</el-table>
</el-row>
</el-form>
</div>
</template>
<script>
export default {
data() {
return {
spanArr: [], // 用于存放每一行記錄的合并數(shù)
testForm: {
tableList: [
[
{
hbxh: '1',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1518 弄',
tbIndex: 0
}, {
hbxh: '1',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1517 弄',
tbIndex: 0
}, {
hbxh: '2',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1519 弄',
tbIndex: 0
}, {
hbxh: '2',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1516 弄',
tbIndex: 0
}
],
[
{
hbxh: '1',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1518 弄',
tbIndex: 1
}, {
hbxh: '2',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1517 弄',
tbIndex: 1
}, {
hbxh: '2',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1519 弄',
tbIndex: 1
}, {
hbxh: '3',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1516 弄',
tbIndex: 1
}, {
hbxh: '4',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1516 弄',
tbIndex: 1
}, {
hbxh: '4',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1516 弄',
tbIndex: 1
}, {
hbxh: '4',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1516 弄',
tbIndex: 1
}
]
] // 測(cè)試數(shù)據(jù)
},
}
},
created() {
this.getSpanArr2(this.testForm.tableList)
},
methods: {
getSpanArr2(data) {
for (let i = 0, l = data.length; i < l; i++) {
let orderObj = {}
data[i].forEach((item, index) => {
// item.index = index
if (orderObj[item.hbxh]) {
orderObj[item.hbxh].push(index)
} else {
orderObj[item.hbxh] = []
orderObj[item.hbxh].push(index)
}
this.spanArr[i] = {
orderIndexArr: []
}
// 將數(shù)組長(zhǎng)度大于1的值 存儲(chǔ)到this.orderIndexArr(也就是需要合并的項(xiàng))
Object.keys(orderObj).forEach((key) => {
if (orderObj[key].length > 1) {
this.spanArr[i].orderIndexArr.push(orderObj[key])
}
})
})
}
console.log('==========this.spanArr==========')
console.log(this.spanArr)
},
colSpanMethod2({ row, column, rowIndex, columnIndex }) {
if (columnIndex === 2) {
for (let i = 0, l = this.spanArr[row.tbIndex].orderIndexArr.length; i < l; i++) {
let element = this.spanArr[row.tbIndex].orderIndexArr[i]
for (let j = 0; j < element.length; j++) {
let item = element[j]
if (rowIndex === item) {
if (j === 0) {
return {
rowspan: element.length,
colspan: 1
}
} else {
return {
rowspan: 0,
colspan: 0
}
}
}
}
}
}
}
}
}
</script>
效果:
同上
來(lái)看看前端打印的信息:

到此這篇關(guān)于el-table 行合并的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)el-table 行合并內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3+vite實(shí)現(xiàn)在線預(yù)覽pdf功能
這篇文章主要為大家詳細(xì)介紹了如何通過(guò)vue3和vite實(shí)現(xiàn)在線預(yù)覽pdf功能,文中的示例代碼簡(jiǎn)潔易懂,具有一定的借鑒價(jià)值,感興趣的小伙伴可以學(xué)習(xí)一下2023-10-10
el-table樹(shù)形數(shù)據(jù)序號(hào)排序處理方案
這篇文章主要介紹了el-table樹(shù)形數(shù)據(jù)序號(hào)排序處理方案,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-03-03
Vue實(shí)現(xiàn)簡(jiǎn)單搜索功能的示例代碼
在vue項(xiàng)目中,搜索功能是我們經(jīng)常需要使用的一個(gè)場(chǎng)景,最常用的是在列表數(shù)據(jù)中搜索一個(gè)想要的,今天的例子就是我們實(shí)現(xiàn)vue從列表數(shù)據(jù)中搜索,希望對(duì)大家有所幫助2023-03-03
Vue TypeScript使用eval函數(shù)遇到的問(wèn)題
近幾年前端對(duì) TypeScript的呼聲越來(lái)越高,Typescript也成為了前端必備的技能。TypeScript是JS類(lèi)型的超集,并支持了泛型、類(lèi)型、命名空間、枚舉等特性,彌補(bǔ)了 JS 在大型應(yīng)用開(kāi)發(fā)中的不足2023-01-01
全新打包工具parcel零配置vue開(kāi)發(fā)腳手架
parcel-vue 一個(gè)基于Parcel打包工具的 VueJS急速開(kāi)發(fā)腳手架解決方案,強(qiáng)烈建議使用node8.0以上。下面通過(guò)本文給大家介紹全新打包工具parcel零配置vue開(kāi)發(fā)腳手架的相關(guān)知識(shí),感興趣的朋友一起看看吧2018-01-01
vue+webpack實(shí)現(xiàn)異步加載三種用法示例詳解
這篇文章主要介紹了vue+webpack實(shí)現(xiàn)異步加載的三種用法,文中給大家提到了vue+webpack實(shí)現(xiàn)異步組件加載的代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2018-04-04

