el-table 行合并的實現(xiàn)示例
頁面實現(xiàn)行合并的情況還是比較常見的,但實現(xiàn)起來卻有一點點難。官網(wǎng)的例子有,不過人家的合并邏輯是從第一行開始兩行兩行合并,不能根據(jù)數(shù)據(jù)動態(tài)合并,感覺參考意義不太大。實際需求一般都是根據(jù)表數(shù)據(jù)動態(tài)來進行合并的,也會有更復(fù)雜的情況,比如循環(huán)表格的。
以下的例子中,表格數(shù)據(jù)是放在頁面的假數(shù)據(jù),hbxh 值相同時,表示需要合并
示例一:單個表格
單個表格的比較簡單,知道合并方法的使用基本就好弄了
<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="合并序號" 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 弄'
}
] // 測試數(shù)據(jù)
},
}
},
created() {
this.getSpanArr(this.testForm.tableData)
},
methods: {
getSpanArr(data) {
// data就是我們從后臺拿到的數(shù)據(jù)
for (var i = 0; i < data.length; i++) {
if (i === 0) {
this.spanArr.push(1);
this.pos = 0;
} else {
// 判斷當前對象的指定屬性值與上一個對象的指定屬性值是否相等
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ù)據(jù)傳入一個值,代表是第幾個表格,方便合并方法使用
<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="合并序號" 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
}
]
] // 測試數(shù)據(jù)
},
}
},
created() {
this.getSpanArr(this.testForm.tableList)
},
methods: {
getSpanArr(data) {
// 用一個對象數(shù)組來存放每個表中每一行記錄的合并數(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; // 記錄開始合并的行索引
data[i].forEach((item, index) => {
item.index = index
if (index === 0) {
this.spanArr[i].tbArr.push(1);
} else {
// 判斷當前對象的指定屬性值與上一個對象的指定屬性值是否相等
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ù)據(jù)結(jié)構(gòu)會比較復(fù)雜,需要耐心理清數(shù)據(jù)之間的關(guān)系
效果:

合并的關(guān)鍵主要就是要準確計算出每行記錄的合并數(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="合并序號" 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
}
]
] // 測試數(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ù)組長度大于1的值 存儲到this.orderIndexArr(也就是需要合并的項)
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>
效果:
同上
來看看前端打印的信息:

到此這篇關(guān)于el-table 行合并的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)el-table 行合并內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3+vite實現(xiàn)在線預(yù)覽pdf功能
這篇文章主要為大家詳細介紹了如何通過vue3和vite實現(xiàn)在線預(yù)覽pdf功能,文中的示例代碼簡潔易懂,具有一定的借鑒價值,感興趣的小伙伴可以學(xué)習(xí)一下2023-10-10
Vue TypeScript使用eval函數(shù)遇到的問題
近幾年前端對 TypeScript的呼聲越來越高,Typescript也成為了前端必備的技能。TypeScript是JS類型的超集,并支持了泛型、類型、命名空間、枚舉等特性,彌補了 JS 在大型應(yīng)用開發(fā)中的不足2023-01-01
vue+webpack實現(xiàn)異步加載三種用法示例詳解
這篇文章主要介紹了vue+webpack實現(xiàn)異步加載的三種用法,文中給大家提到了vue+webpack實現(xiàn)異步組件加載的代碼,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2018-04-04

