最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Element?UI中table單元格合并的解決過程

 更新時間:2022年08月25日 11:48:12   作者:beijixing333y  
element?ui中的table表格數(shù)據(jù)是動態(tài)生成的,最近遇到個需求,要求我們對單元格進行合并,所以下面這篇文章主要給大家介紹了關于Element?UI中table單元格合并的解決過程,需要的朋友可以參考下

引言

項目中遇到表格單元格合并的需求,在此記錄整個解決過程。

項目使用的是 Element UI,表格使用的是 table 組件。Element UI 的 table 表格組件中對單元格進行合并,需要使用 table 組件的 span-method 屬性。

先看一張成果圖(完整代碼放在末尾):

解決思路:

1、格式化后臺返回的數(shù)據(jù)(根據(jù)實際數(shù)據(jù)格式處理)

項目后臺返回的數(shù)據(jù)格式為樹形結構,這里做簡化展示:

[
  {
    'column1': '111',
    'column2': '222',
    'column3': '333',
    'children1': [
      {
        'column6': 666,
        'column4': '4440',
        'column5': '5550',
        'children2': [
          {
            'column7': '77701',
            'column8': '88801',
            'column9': '99901'
          }
        ]
      }
    ]
  }
]

需要先將樹結構數(shù)據(jù)轉為一維數(shù)組:

// table 表格數(shù)據(jù)初始化處理,將樹結構數(shù)據(jù)轉為一維數(shù)組
    handleData(data, parentId) {
      data.map((res, index) => {
        var obj = {
          id: parentId
        }
        for (const key in res) {
          const isarr = Object.values(res).find((age) => {
            return Array.isArray(age)
          })
          if (isarr) {
            if (Array.isArray(res[key])) {
              for (let i = 0; i < res[key].length; i++) {
                Object.assign(obj, res[key][i])
                data.push(obj)
                res[key].splice(i, 1)
                if (res[key].length === 0) {
                  data.splice(index, 1)
                }
                this.handleData(data, parentId)
              }
            } else {
              Object.assign(obj, { [key]: res[key] })
            }
          }
        }
      })
      return data
    }

因為后臺返回的數(shù)據(jù)里沒有唯一標識符,所以需要單獨添加一個唯一標識表示轉換為一維數(shù)組的數(shù)據(jù)是出自同一組樹結構里。故此處在展開時單獨加了一個 id 屬性,用來代替唯一標識。如果后臺返回的數(shù)據(jù)格式就是一個一維數(shù)組,可跳過數(shù)據(jù)格式化步驟。

2、在 data 中定義數(shù)據(jù),需要合并幾列就定義幾個數(shù)組和索引

  data() {
    return {
      tableData: [],
      // 合并單元格
      column1Arr: [], // column1
      column1Index: 0, // column1索引
      column2Arr: [], // column2
      column2Index: 0, // column2索引
      column3Arr: [], // column3
      column3Index: 0, // column3索引
      column4Arr: [], // column4
      column4Index: 0, // column4
      column5Arr: [], // column5
      column5Index: 0, // column5索引
      column6Arr: [], // column6
      column6Index: 0 // column6索引
    }
  }

3、定義合并函數(shù)

以第一行為基準,一層層對比,參數(shù) data 就是格式化以后的表格數(shù)據(jù),以每個數(shù)據(jù)里的唯一標識 id 作為合并的參照字段:

    // 初始化合并行數(shù)組
    mergeInit() {
      this.column1Arr = [] // column1
      this.column1Index = 0 // column1索引
      this.column2Arr = [] // column2
      this.column2Index = 0 // column2索引
      this.column3Arr = [] // column3
      this.column3Index = 0 // column3索引
      this.column4Arr = [] // column4
      this.column4Index = 0 // column4索引
      this.column5Arr = [] // column5
      this.column5Index = 0 // column5索引
      this.column6Arr = [] // column6
      this.column6Index = 0 // column6索引
    },
    // 合并表格
    mergeTable(data) {
      this.mergeInit()
      if (data.length > 0) {
        for (var i = 0; i < data.length; i++) {
          if (i === 0) {
            // 第一行必須存在,以第一行為基準
            this.column1Arr.push(1) // column1
            this.column1Index = 0
 
            this.column2Arr.push(1) // column2
            this.column2Index = 0
 
            this.column3Arr.push(1) // column3
            this.column3Index = 0
 
            this.column4Arr.push(1) // column4
            this.column4Index = 0
 
            this.column5Arr.push(1) // column5
            this.column5Index = 0
 
            this.column6Arr.push(1) // column6
            this.column6Index = 0
          } else {
            // 判斷當前元素與上一元素是否相同
            // column1
            if (
              data[i].column1 === data[i - 1].column1 &&
              data[i].id === data[i - 1].id
            ) {
              this.column1Arr[this.column1Index] += 1
              this.column1Arr.push(0)
            } else {
              this.column1Arr.push(1)
              this.column1Index = i
            }
 
            // column2
            if (
              data[i].column2 === data[i - 1].column2 &&
              data[i].id === data[i - 1].id
            ) {
              this.column2Arr[this.column2Index] += 1
              this.column2Arr.push(0)
            } else {
              this.column2Arr.push(1)
              this.column2Index = i
            }
 
            // column3
            if (
              data[i].column3 === data[i - 1].column3 &&
              data[i].id === data[i - 1].id
            ) {
              this.column3Arr[this.column3Index] += 1
              this.column3Arr.push(0)
            } else {
              this.column3Arr.push(1)
              this.column3Index = i
            }
 
            // column4
            if (
              data[i].column4 === data[i - 1].column4 &&
              data[i].id === data[i - 1].id
            ) {
              this.column4Arr[this.column4Index] += 1
              this.column4Arr.push(0)
            } else {
              this.column4Arr.push(1)
              this.column4Index = i
            }
 
            // column5
            if (
              data[i].column5 === data[i - 1].column5 &&
              data[i].column4 === data[i - 1].column4 &&
              data[i].id === data[i - 1].id
            ) {
              this.column5Arr[this.column5Index] += 1
              this.column5Arr.push(0)
            } else {
              this.column5Arr.push(1)
              this.column5Index = i
            }
 
            // column6
            if (
              data[i].column6 === data[i - 1].column6 &&
              data[i].column4 === data[i - 1].column4 &&
              data[i].id === data[i - 1].id
            ) {
              this.column6Arr[this.column6Index] += 1
              this.column6Arr.push(0)
            } else {
              this.column6Arr.push(1)
              this.column6Index = i
            }
          }
        }
      }
    },

注意,同一組數(shù)據(jù)里可能會有多個  children1 或者 children2,這時合并的時候會有多個條件進行判斷:

4、table 組件屬性 span-method 的單元格合并方法:

    handleSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0 || columnIndex === 1) {
        // 第一列 column1
        const _row_1 = this.column1Arr[rowIndex]
        const _col_1 = _row_1 > 0 ? 1 : 0
        return {
          rowspan: _row_1,
          colspan: _col_1
        }
      } else if (columnIndex === 2) {
        // 第二列 column2
        const _row_2 = this.column2Arr[rowIndex]
        const _col_2 = _row_2 > 0 ? 1 : 0
        return {
          rowspan: _row_2,
          colspan: _col_2
        }
      } else if (columnIndex === 3) {
        // 第三列 column3
        const _row_2 = this.column3Arr[rowIndex]
        const _col_2 = _row_2 > 0 ? 1 : 0
        return {
          rowspan: _row_2,
          colspan: _col_2
        }
      } else if (columnIndex === 4) {
        // 第四列 column4
        const _row_2 = this.column4Arr[rowIndex]
        const _col_2 = _row_2 > 0 ? 1 : 0
        return {
          rowspan: _row_2,
          colspan: _col_2
        }
      } else if (columnIndex === 5) {
        // 第五列 column5
        const _row_2 = this.column5Arr[rowIndex]
        const _col_2 = _row_2 > 0 ? 1 : 0
        return {
          rowspan: _row_2,
          colspan: _col_2
        }
      } else if (columnIndex === 6) {
        // 第六列 column6
        const _row_2 = this.column6Arr[rowIndex]
        const _col_2 = _row_2 > 0 ? 1 : 0
        return {
          rowspan: _row_2,
          colspan: _col_2
        }
      }
    }

至此,整個單元格合并就完成了!

如果覺得寫得還不錯,還請點贊支持,感謝感謝感謝?。?!

完整代碼:

<template>
  <div class="table-wrap">
    <el-table
      :data="tableData"
      :span-method="handleSpanMethod"
      :cell-style="{ background: '#FFFFFF' }"
      border
      style="width: 100%"
    >
      <el-table-column prop="id" label="序號" align="center" width="80">
        <template slot-scope="scope">
          {{ scope.row.id + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="column1" label="column1" align="center" />
      <el-table-column prop="column2" label="column2" align="center" />
      <el-table-column prop="column3" label="column3" align="center" />
      <el-table-column prop="column4" label="column4" align="center" />
      <el-table-column prop="column5" label="column5" align="center" />
      <el-table-column prop="column6" label="column6" align="center" />
      <el-table-column prop="column7" label="column7" align="center" />
      <el-table-column prop="column8" label="column8" align="center" />
      <el-table-column prop="column9" label="column9" align="center" />
    </el-table>
  </div>
</template>
 
<script>
export default {
  name: 'CellMerge',
  data() {
    return {
      tableData: [],
      // 合并單元格
      column1Arr: [], // column1
      column1Index: 0, // column1索引
      column2Arr: [], // column2
      column2Index: 0, // column2索引
      column3Arr: [], // column3
      column3Index: 0, // column3索引
      column4Arr: [], // column4
      column4Index: 0, // column4
      column5Arr: [], // column5
      column5Index: 0, // column5索引
      column6Arr: [], // column6
      column6Index: 0 // column6索引
    }
  },
  mounted() {
    this.initTableData()
  },
  methods: {
    // 初始化表格數(shù)據(jù)
    initTableData() {
      const newTableData = [
        {
          'column1': '111',
          'column2': '222',
          'column3': '333',
          'children1': [
            {
              'column6': 666,
              'column4': '4440',
              'column5': '5550',
              'children2': [
                {
                  'column7': '77701',
                  'column8': '88801',
                  'column9': '99901'
                },
                {
                  'column7': '77702',
                  'column8': '88802',
                  'column9': '99902'
                },
                {
                  'column7': '77703',
                  'column8': '88803',
                  'column9': '99903'
                }
              ]
            },
            {
              'column6': 666,
              'column4': '4441',
              'column5': '5551',
              'children2': [
                {
                  'column7': '77711',
                  'column8': '88811',
                  'column9': '99911'
                }
              ]
            },
            {
              'column6': 666,
              'column4': '4442',
              'column5': '5552',
              'children2': [
                {
                  'column7': '77721',
                  'column8': '88821',
                  'column9': '99921'
                },
                {
                  'column7': '77722',
                  'column8': '88822',
                  'column9': '99922'
                }
              ]
            }
          ]
        },
        {
          'column1': '111',
          'column2': '222',
          'column3': '333',
          'children1': [
            {
              'column6': 666,
              'column4': '4440',
              'column5': '5550',
              'children2': [
                {
                  'column7': '77701',
                  'column8': '88801',
                  'column9': '99901'
                }
              ]
            },
            {
              'column6': 666,
              'column4': '4441',
              'column5': '5551',
              'children2': [
                {
                  'column7': '77711',
                  'column8': '88811',
                  'column9': '99911'
                },
                {
                  'column7': '77712',
                  'column8': '88812',
                  'column9': '99912'
                }
              ]
            }
          ]
        },
        {
          'column1': '111',
          'column2': '222',
          'column3': '333',
          'children1': [
            {
              'column6': 666,
              'column4': '4440',
              'column5': '5550',
              'children2': [
                {
                  'column7': '77701',
                  'column8': '88801',
                  'column9': '99901'
                },
                {
                  'column7': '77702',
                  'column8': '88802',
                  'column9': '99902'
                },
                {
                  'column7': '77703',
                  'column8': '88803',
                  'column9': '99903'
                }
              ]
            },
            {
              'column6': 666,
              'column4': '4441',
              'column5': '5551',
              'children2': [
                {
                  'column7': '77711',
                  'column8': '88811',
                  'column9': '99911'
                }
              ]
            }
          ]
        }
      ]
      this.tableData = []
      newTableData.map((res, index) => {
        const parentId = index
        this.tableData.push.apply(
          this.tableData,
          this.handleData([res], parentId)
        )
      })
      this.mergeTable(this.tableData)
    },
    // table 表格數(shù)據(jù)初始化處理,將樹結構數(shù)據(jù)轉為一維數(shù)組
    handleData(data, parentId) {
      data.map((res, index) => {
        var obj = {
          id: parentId
        }
        for (const key in res) {
          const isarr = Object.values(res).find((age) => {
            return Array.isArray(age)
          })
          if (isarr) {
            if (Array.isArray(res[key])) {
              for (let i = 0; i < res[key].length; i++) {
                Object.assign(obj, res[key][i])
                data.push(obj)
                res[key].splice(i, 1)
                if (res[key].length === 0) {
                  data.splice(index, 1)
                }
                this.handleData(data, parentId)
              }
            } else {
              Object.assign(obj, { [key]: res[key] })
            }
          }
        }
      })
      return data
    },
    // 初始化合并行數(shù)組
    mergeInit() {
      this.column1Arr = [] // column1
      this.column1Index = 0 // column1索引
      this.column2Arr = [] // column2
      this.column2Index = 0 // column2索引
      this.column3Arr = [] // column3
      this.column3Index = 0 // column3索引
      this.column4Arr = [] // column4
      this.column4Index = 0 // column4索引
      this.column5Arr = [] // column5
      this.column5Index = 0 // column5索引
      this.column6Arr = [] // column6
      this.column6Index = 0 // column6索引
    },
    // 合并表格
    mergeTable(data) {
      this.mergeInit()
      if (data.length > 0) {
        for (var i = 0; i < data.length; i++) {
          if (i === 0) {
            // 第一行必須存在,以第一行為基準
            this.column1Arr.push(1) // column1
            this.column1Index = 0
 
            this.column2Arr.push(1) // column2
            this.column2Index = 0
 
            this.column3Arr.push(1) // column3
            this.column3Index = 0
 
            this.column4Arr.push(1) // column4
            this.column4Index = 0
 
            this.column5Arr.push(1) // column5
            this.column5Index = 0
 
            this.column6Arr.push(1) // column6
            this.column6Index = 0
          } else {
            // 判斷當前元素與上一元素是否相同
            // column1
            if (
              data[i].column1 === data[i - 1].column1 &&
              data[i].id === data[i - 1].id
            ) {
              this.column1Arr[this.column1Index] += 1
              this.column1Arr.push(0)
            } else {
              this.column1Arr.push(1)
              this.column1Index = i
            }
 
            // column2
            if (
              data[i].column2 === data[i - 1].column2 &&
              data[i].id === data[i - 1].id
            ) {
              this.column2Arr[this.column2Index] += 1
              this.column2Arr.push(0)
            } else {
              this.column2Arr.push(1)
              this.column2Index = i
            }
 
            // column3
            if (
              data[i].column3 === data[i - 1].column3 &&
              data[i].id === data[i - 1].id
            ) {
              this.column3Arr[this.column3Index] += 1
              this.column3Arr.push(0)
            } else {
              this.column3Arr.push(1)
              this.column3Index = i
            }
 
            // column4
            if (
              data[i].column4 === data[i - 1].column4 &&
              data[i].id === data[i - 1].id
            ) {
              this.column4Arr[this.column4Index] += 1
              this.column4Arr.push(0)
            } else {
              this.column4Arr.push(1)
              this.column4Index = i
            }
 
            // column5
            if (
              data[i].column5 === data[i - 1].column5 &&
              data[i].column4 === data[i - 1].column4 &&
              data[i].id === data[i - 1].id
            ) {
              this.column5Arr[this.column5Index] += 1
              this.column5Arr.push(0)
            } else {
              this.column5Arr.push(1)
              this.column5Index = i
            }
 
            // column6
            if (
              data[i].column6 === data[i - 1].column6 &&
              data[i].column4 === data[i - 1].column4 &&
              data[i].id === data[i - 1].id
            ) {
              this.column6Arr[this.column6Index] += 1
              this.column6Arr.push(0)
            } else {
              this.column6Arr.push(1)
              this.column6Index = i
            }
          }
        }
      }
    },
    handleSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0 || columnIndex === 1) {
        // 第一列 column1
        const _row_1 = this.column1Arr[rowIndex]
        const _col_1 = _row_1 > 0 ? 1 : 0
        return {
          rowspan: _row_1,
          colspan: _col_1
        }
      } else if (columnIndex === 2) {
        // 第二列 column2
        const _row_2 = this.column2Arr[rowIndex]
        const _col_2 = _row_2 > 0 ? 1 : 0
        return {
          rowspan: _row_2,
          colspan: _col_2
        }
      } else if (columnIndex === 3) {
        // 第三列 column3
        const _row_2 = this.column3Arr[rowIndex]
        const _col_2 = _row_2 > 0 ? 1 : 0
        return {
          rowspan: _row_2,
          colspan: _col_2
        }
      } else if (columnIndex === 4) {
        // 第四列 column4
        const _row_2 = this.column4Arr[rowIndex]
        const _col_2 = _row_2 > 0 ? 1 : 0
        return {
          rowspan: _row_2,
          colspan: _col_2
        }
      } else if (columnIndex === 5) {
        // 第五列 column5
        const _row_2 = this.column5Arr[rowIndex]
        const _col_2 = _row_2 > 0 ? 1 : 0
        return {
          rowspan: _row_2,
          colspan: _col_2
        }
      } else if (columnIndex === 6) {
        // 第六列 column6
        const _row_2 = this.column6Arr[rowIndex]
        const _col_2 = _row_2 > 0 ? 1 : 0
        return {
          rowspan: _row_2,
          colspan: _col_2
        }
      }
    }
  }
}
</script>
<style lang="scss" scoped>
  .table-wrap {
    width: 100%;
    height: 100%;
    padding: 20px;
  }
</style>

總結

到此這篇關于Element UI中table單元格合并的文章就介紹到這了,更多相關Element UI table單元格合并內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • vue基于viewer實現(xiàn)的圖片查看器功能

    vue基于viewer實現(xiàn)的圖片查看器功能

    這篇文章主要介紹了vue基于viewer實現(xiàn)的圖片查看器的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-04-04
  • vue遞歸生成樹狀結構的示例代碼

    vue遞歸生成樹狀結構的示例代碼

    這篇文章主要介紹了vue遞歸生成樹狀結構的示例,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2024-07-07
  • vue實踐---vue不依賴外部資源實現(xiàn)簡單多語操作

    vue實踐---vue不依賴外部資源實現(xiàn)簡單多語操作

    這篇文章主要介紹了vue實踐---vue不依賴外部資源實現(xiàn)簡單多語操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • vue轉electron項目及解決使用fs報錯:Module?not?found:?Error:?Can't?resolve?'fs'?in

    vue轉electron項目及解決使用fs報錯:Module?not?found:?Error:?Can&apo

    這篇文章主要給大家介紹了關于vue轉electron項目及解決使用fs報錯:Module?not?found:?Error:?Can‘t?resolve?‘fs‘?in的相關資料,文中通過圖文以及實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-11-11
  • Element?Plus修改表格行和單元格樣式詳解

    Element?Plus修改表格行和單元格樣式詳解

    在使用Element Plus中的table組件展示數(shù)據(jù)時,由于需要對表格行內數(shù)據(jù)的數(shù)據(jù)進行修改,下面這篇文章主要給大家介紹了關于Element?Plus修改表格行和單元格樣式的相關資料,需要的朋友可以參考下
    2022-04-04
  • Vue package.json配置深入分析

    Vue package.json配置深入分析

    這篇文章主要介紹了Vue package.json配置,package.json是每個前端項目都會有的json文件,位于項目的根目錄中。很多腳手架在創(chuàng)建項目的時候會幫我們自動初始化好 package.json
    2023-01-01
  • vue移動端項目緩存問題實踐記錄

    vue移動端項目緩存問題實踐記錄

    最近在做一個vue移動端項目,被緩存問題搞得頭都大了,積累了一些經(jīng)驗,特此記錄總結下,分享到腳本之家平臺,對vue移動端項目緩存問題實踐記錄感興趣的朋友跟隨小編一起看看吧
    2018-10-10
  • vue拖拽改變寬度的實現(xiàn)示例

    vue拖拽改變寬度的實現(xiàn)示例

    本文主要介紹了vue拖拽改變寬度的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-07-07
  • 基于vue.js中關于下拉框的值默認及綁定問題

    基于vue.js中關于下拉框的值默認及綁定問題

    今天小編就為大家分享一篇基于vue.js中關于下拉框的值默認及綁定問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • 關閉eslint檢查和ts檢查的簡單步驟記錄

    關閉eslint檢查和ts檢查的簡單步驟記錄

    這篇文章主要給大家介紹了關于關閉eslint檢查和ts檢查的相關資料,eslint是一個JavaScript的校驗插件,通常用來校驗語法或代碼的書寫風格,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-02-02

最新評論

江口县| 夏津县| 达拉特旗| 阿图什市| 铅山县| 城步| 文登市| 仁寿县| 高邑县| 上虞市| 项城市| 杭州市| 宁津县| 平原县| 分宜县| 峡江县| 许昌市| 合江县| 庄浪县| 三原县| 旺苍县| 新田县| 顺昌县| 蚌埠市| 仪征市| 深州市| 板桥市| 晋宁县| 海口市| 库车县| 远安县| 浦城县| 康平县| 札达县| 上栗县| 资阳市| 涟源市| 阿克苏市| 天峨县| 双城市| 嘉黎县|