vue中el-table合并列的具體實現(xiàn)
問題描述
有時候,產(chǎn)品讓我們做的表格,會有合并列的功能,但是官方的demo略有不清晰,本文舉個例子簡述之。我們先看下效果圖:

假設產(chǎn)品的需求是這樣的:設備類別那一列,同類的,做成分堆形式,也就是合并列形式
分析
分析寫在代碼注釋中里面哦
方式一 計算以后再合并
<template>
<div class="vueWrap">
<el-table
:span-method="objectSpanMethod"
style="width: 800px"
:data="tableBody"
border
:header-cell-style="{
background: '#FAFAFA',
color: '#333333',
fontWeight: 'bold',
fontSize: '14px',
}"
>
<el-table-column
type="index"
label="序號"
width="58"
align="center"
></el-table-column>
<el-table-column
prop="toolsKinds"
label="設備類別"
align="center"
></el-table-column>
<el-table-column prop="toolsName" label="設備名稱" align="center"></el-table-column>
<el-table-column prop="price" label="價格(元)" align="center"></el-table-column>
<el-table-column prop="remark" label="備注" align="center"></el-table-column>
</el-table>
</div>
</template>
<script>
export default {
data() {
return {
// 表體數(shù)據(jù)
tableBody: [
{
toolsKinds: "螺絲刀",
toolsName: "一號螺絲刀",
price: 10,
remark: "",
},
{
toolsKinds: "螺絲刀",
toolsName: "二號螺絲刀",
price: 20,
remark: "",
},
{
toolsKinds: "螺絲刀",
toolsName: "三號螺絲刀",
price: 30,
remark: "",
},
{
toolsKinds: "扳手",
toolsName: "大號扳手",
price: 88,
remark: "",
},
{
toolsKinds: "扳手",
toolsName: "中號扳手",
price: 44,
remark: "",
},
{
toolsKinds: "老虎鉗子",
toolsName: "火星專供老虎鉗",
price: 999,
remark: "",
},
{
toolsKinds: "老虎鉗子",
toolsName: "土星專供老虎鉗",
price: 1001,
remark: "",
},
],
cellList: [], // 單元格數(shù)組
count: null, // 計數(shù)
};
},
mounted() {
// 第1步,根據(jù)表體信息,計算合并單元格的信息
this.computeCell(this.tableBody);
},
methods: {
computeCell(tableBody) {
// 循環(huán)遍歷表體數(shù)據(jù)
for (let i = 0; i < tableBody.length; i++) {
if (i == 0) {
// 先設置第一項
this.cellList.push(1); // 初為1,若下一項和此項相同,就往cellList數(shù)組中追加0
this.count = 0; // 初始計數(shù)為0
console.log("索引", 0, this.count);
} else {
// 判斷當前項與上項的設備類別是否相同,因為是合并這一列的單元格
if (tableBody[i].toolsKinds == tableBody[i - 1].toolsKinds) {
// 如果相等
this.cellList[this.count] += 1; // 增加計數(shù)
this.cellList.push(0); // 相等就往cellList數(shù)組中追加0
console.log("索引", i, this.count);
} else {
this.cellList.push(1); // 不等就往cellList數(shù)組中追加1
this.count = i; // 將索引賦值為計數(shù)
console.log("索引", i, this.count);
}
}
}
},
// 第2步,將計算好的結(jié)果返回給el-table,這樣的話表格就會根據(jù)這個結(jié)果做對應合并列渲染
objectSpanMethod({ row, column, rowIndex, columnIndex }) {
// 給第二列做單元格合并。0是第一列,1是第二列。
if (columnIndex === 1) {
console.log("單元格數(shù)組,若下一項為0,則代表合并上一項", this.cellList);
const rowCell = this.cellList[rowIndex];
if (rowCell > 0) {
const colCell = 1;
console.log(`動態(tài)豎向合并單元格, 第${colCell}列,豎向合并${rowCell}個單元格 `);
return {
rowspan: rowCell,
colspan: colCell,
};
} else {
// 清除原有的單元格,必須要加,否則就會出現(xiàn)單元格會被橫著擠到后面了?。。?
// 本例中數(shù)據(jù)是寫死的不會出現(xiàn),數(shù)據(jù)若是動態(tài)后端獲取的,就會出現(xiàn)了?。?!
return {
rowspan: 0,
colspan: 0,
};
}
}
},
},
};
</script>打印截圖
注意打印的結(jié)果

方式二 直接合并(更直觀的做法)
適用于固定的數(shù)據(jù),比如年度、季度等...
<template>
<div id="kkk">
<el-table
:data="tableData"
:span-method="objectSpanMethod"
border
style="width: 100%"
>
<el-table-column type="index" label="序號" width="50"> </el-table-column>
<el-table-column prop="type" label="設備類別" align="center">
</el-table-column>
<el-table-column prop="mcName" label="設備名稱" align="center">
</el-table-column>
<el-table-column prop="price" label="價格" align="center">
</el-table-column>
</el-table>
</div>
</template>
<script>
export default {
data() {
return {
tableData: [
{
type: "螺絲刀",
mcName: "一號螺絲刀",
price: "10",
},
{
type: "螺絲刀",
mcName: "二號螺絲刀",
price: "20",
},
{
type: "螺絲刀",
mcName: "三號螺絲刀",
price: "30",
},
{
type: "扳手",
mcName: "大號扳手",
price: "88",
},
{
type: "扳手",
mcName: "中號扳手",
price: "44",
},
{
type: "老虎鉗子",
mcName: "火星專供",
price: "999",
},
{
type: "老虎鉗子",
mcName: "土星專供",
price: "1001",
},
],
};
},
methods: {
/**
* 1. 若是objectSpanMethod不返回任何東西,表格不會變化
* 2. 最外層的判斷一般是,先從第幾列開始合并
* 3. 這次從第0行合并2個,下次就要從第3行開始合并(0行加倆,就到3行了)
* 4. 這種方式是有多少條數(shù)據(jù),合并多少條數(shù)據(jù),比如本案例中有7條數(shù)據(jù)(從第0條合并到第7條)
* 5. return { rowspan: 0, colspan: 0 } // 表示不合并
* */
objectSpanMethod({ row, column, rowIndex, columnIndex }) {
console.log("rowIndex", rowIndex);
// 準備在第二列進行合并操作
if (columnIndex == 1) {
// 從第0行進行合并
if (rowIndex == 0) {
return {
rowspan: 3, // 合并3行
colspan: 1, // 合并1列(當前列)
};
}
if (rowIndex == 3) {
return {
rowspan: 2, // 合并2行
colspan: 1, // 合并1列
};
}
if (rowIndex == 5) {
return {
rowspan: 2, // 合并1行
colspan: 1, // 合并1列
};
}
}
},
},
};
</script>到此這篇關于vue中el-table合并列的具體實現(xiàn)的文章就介紹到這了,更多相關vue el-table合并列內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Vue3中el-table表格數(shù)據(jù)不顯示的原因和解決方法
- vue?el-table實現(xiàn)動態(tài)添加行和列具體代碼
- Vue+EleMentUI實現(xiàn)el-table-colum表格select下拉框可編輯功能實例
- Vue動態(tài)設置el-table操作列的寬度自適應
- vue element-ui實現(xiàn)el-table表格多選以及回顯方式
- Vue element el-table-column中對日期進行格式化方式(全局過濾器)
- Vue中如何合并el-table第一列相同數(shù)據(jù)
- vue element-ui el-table組件自定義合計(summary-method)的坑
- el-table 選擇框根據(jù)條件設置某項不可選中的操作代碼
相關文章
解決vue.js在編寫過程中出現(xiàn)空格不規(guī)范報錯的問題
下面小編就為大家?guī)硪黄鉀Qvue.js在編寫過程中出現(xiàn)空格不規(guī)范報錯的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
Vue iview-admin框架二級菜單改為三級菜單的方法
這篇文章主要介紹了Vue iview-admin框架二級菜單改為三級菜單的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
Vite結(jié)合Vue刪除指定環(huán)境的console.log問題
這篇文章主要介紹了Vite結(jié)合Vue刪除指定環(huán)境的console.log問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
uniapp組件uni-file-picker中對上傳的圖片進行壓縮至1兆以內(nèi)(推薦)
我在做uniapp項目時,用的uni-file-picker組件,這是我做的一個項目實例,主要是將圖片通過接口傳至后臺服務器,本文給大家分享uniapp組件uni-file-picker中對上傳的圖片進行壓縮再上傳,感興趣的朋友跟隨小編一起看看吧2022-11-11
Vue中Router路由兩種模式hash與history詳解
這篇文章主要介紹了Vue中Router路由的兩種模式,分別對hash模式與history模式作了簡要分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09
詳解Vue.js組件可復用性的混合(mixin)方式和自定義指令
本篇文章主要介紹了詳解Vue.js組件可復用性的混合(mixin)方式和自定義指令,具有一定的參考價值,有興趣的可以了解一下2017-09-09
Element-ui table中過濾條件變更表格內(nèi)容的方法
下面小編就為大家分享一篇Element-ui table中過濾條件變更表格內(nèi)容的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
VUE+node(express)實現(xiàn)前后端分離
在本篇文章里小編給大家分享的是關于VUE+node(express)前后端分離實例內(nèi)容,有需要的朋友們參考下。2019-10-10

