Element中table組件按照屬性執(zhí)行合并操作詳解
在實際開發(fā)中,要求使用elementUI的table組件對表格數(shù)據(jù)上下行相鄰相同的數(shù)據(jù)進行合并,在elem官網(wǎng)上查看到是有對應(yīng)的組件和合并方法
<el-table :data="tableData" :span-method="objectSpanMethod"> <el-table-column prop="id" label="ID" width="180"> </el-table-column> </el-table>
其中官網(wǎng)的objectSpanMethod比較簡單,根據(jù)每隔兩行就合并兩行的數(shù)據(jù),并不能滿足實際的需求,下面直接上代碼
1、首先需要生成一個與行數(shù)相同的數(shù)組,通過判斷上下數(shù)據(jù)是否相同,記錄每一行設(shè)置的合并數(shù)。這里以合并三列屬性為例:
getSpanArr(data) { this.spanArr = []; this.spanCodeArr = []; this.spanProxyArr = []; for (var i = 0; i < data.length; i++) { if (i === 0) { this.spanArr.push(1); this.spanCodeArr.push(1); this.spanProxyArr.push(1); this.pos = 0; this.codePos = 0; this.proxyPos = 0; } else { Object.keys(this.columns).forEach((item, index) => { if (index === 0) { if (data[i][item] === data[i - 1][item]) { this.spanArr[this.pos] += 1; this.spanArr.push(0); } else { this.spanArr.push(1); this.pos = i; } } else if (index === 1) { if (data[i][item] === data[i - 1][item]) { this.spanCodeArr[this.codePos] += 1; this.spanCodeArr.push(0); } else { this.spanCodeArr.push(1); this.codePos = i; } } else if (index === 2) { if (data[i][item] === data[i - 1][item]) { this.spanProxyArr[this.proxyPos] += 1; this.spanProxyArr.push(0); } else { this.spanProxyArr.push(1); this.proxyPos = i; } } }); } } },
其中 if (data[i][item] === data[i - 1][item]) {}
這里就是判斷當(dāng)前元素與上一個元素是否相同
如果第一條記錄索引為0,向數(shù)組中push 1,設(shè)置索引值
如果不是第一條記錄,判斷與前一條記錄是否相等,相等則向?qū)?yīng)的屬性數(shù)組spanArr、spanCodeArr、spanProxyArr添加元素0
且將前一條記錄+1,即需要合并的行數(shù)+1,直到得到所有需要合并的行數(shù)
2、官方介紹是通過給table傳入span-method方法可以實現(xiàn)合并行或列,方法的參數(shù)是一個對象,里面包含當(dāng)前行row、當(dāng)前列column、當(dāng)前行號rowIndex、當(dāng)前列號columnIndex四個屬性。該函數(shù)可以返回一個包含兩個元素的數(shù)組,第一個元素代表rowspan,第二個元素代表colspan。也可以返回一個鍵名為rowspan和colspan的對象
objectSpanMethod({ row, column, rowIndex, columnIndex }) { if (columnIndex === 0) { const _row = this.spanArr[rowIndex]; const _col = _row > 0 ? 1 : 0; return { rowspan: _row, colspan: _col }; } if (columnIndex === 1) { const _row = this.spanCodeArr[rowIndex]; const _col = _row > 0 ? 1 : 0; return { rowspan: _row, colspan: _col }; } if (columnIndex === 2) { const _row = this.spanProxyArr[rowIndex]; const _col = _row > 0 ? 1 : 0; return { rowspan: _row, colspan: _col }; } }
3、結(jié)合組件使用
<q-table :data="tableData" border :span-method="objectSpanMethod" height="400" style="width: 100%"> <q-table-column v-for="(item,index) in Object.keys(columns)" :key="index" :prop="item" :label="columns[item]"> </q-table-column> </q-table>
到此這篇關(guān)于Element中table組件按照屬性執(zhí)行合并操作詳解的文章就介紹到這了,更多相關(guān)Element table組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue使用vue-video-player無法播放本地視頻的問題及解決
這篇文章主要介紹了vue使用vue-video-player無法播放本地視頻的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08Vue中router.beforeEach與beforeRouteEnter的區(qū)別及說明
這篇文章主要介紹了Vue中router.beforeEach與beforeRouteEnter的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10vue項目如何從session中獲取對象,并且使用里面的屬性
這篇文章主要介紹了vue項目如何從session中獲取對象,并且使用里面的屬性問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12Vue純前端實現(xiàn)導(dǎo)出Excel并修改樣式
這篇文章主要為大家詳細(xì)介紹了Vue如何利用xlsx-style庫實現(xiàn)導(dǎo)出Excel并修改樣式的功能,文中的示例代碼講解詳細(xì),有需要的可以參考下2024-01-01