1.最近在做的erp項目,有一個需求是同一個客戶下的同種訂單,需要合并展示。使用elementUI table組件的方法 :span-method="objectSpanMethod"。官網(wǎng)上看了一下demo,做的很直白,不過不太符合業(yè)務(wù)。在網(wǎng)上找了篇文章參考了一下
2.效果圖如下:
在動態(tài)處理從后端拿回來的數(shù)據(jù)的時候,是需要從數(shù)據(jù)中找到一個唯一的“標(biāo)識”去判斷是否是相同種類的數(shù)據(jù)。然后根據(jù)這個“標(biāo)識”去做邏輯判斷。
3.代碼:
//合并單元格 二維數(shù)組-> 根據(jù)“標(biāo)識”去遍歷數(shù)據(jù)data() {return { spanArr: [], //遍歷數(shù)據(jù)時,根據(jù)相同的標(biāo)識去存儲記錄 pos: 0 // 二維數(shù)組的索引}}// methods中定義方法getSpanArr(data) {let that = this//頁面展示的數(shù)據(jù),不一定是全部的數(shù)據(jù),所以每次都清空之前存儲的 保證遍歷的數(shù)據(jù)是最新的數(shù)據(jù)。以免造成數(shù)據(jù)渲染混亂that.spanArr = []that.pos = 0//遍歷數(shù)據(jù)data.forEach((item, index) => { //判斷是否是第一項 if (index === 0) { this.spanArr.push(1) this.pos = 0 } else { //不是第一項時,就根據(jù)標(biāo)識去存儲 if (data[index].moldName === data[index - 1].moldName) { // 查找到符合條件的數(shù)據(jù)時每次要把之前存儲的數(shù)據(jù)+1 this.spanArr[this.pos] += 1 this.spanArr.push(0) } else { // 沒有符合的數(shù)據(jù)時,要記住當(dāng)前的index this.spanArr.push(1) this.pos = index } } })console.log(this.spanArr, this.pos)},// 列表方法objectSpanMethod({rowIndex, columnIndex}) {// 頁面列表上 表格合并行 -> 第幾列(從0開始)// 需要合并多個單元格時 依次增加判斷條件即可if (columnIndex === 3) { // 二維數(shù)組存儲的數(shù)據(jù) 取出 const _row = this.spanArr[rowIndex] const _col = _row > 0 ? 1 : 0 return { rowspan: _row, colspan: _col } //不可以return {rowspan:0, colspan: 0} 會造成數(shù)據(jù)不渲染, 也可以不寫else,eslint過不了的話就返回false} else { return false}}created() {let data = xxxxxxxxxthis.getSpanArr(data)}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點
疑難解答