欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue.js el-table動態(tài)單元格列合并方式

 更新時(shí)間:2024年02月28日 09:52:16   作者:Lemon今天學(xué)習(xí)了嗎  
這篇文章主要介紹了vue.js el-table動態(tài)單元格列合并方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

一、業(yè)務(wù)需求

一個(gè)展示列表,表格中有一部分列是根據(jù)后端接口動態(tài)展示,對于不同類型的數(shù)據(jù)展示效果不一樣。

如果接口返回?cái)?shù)據(jù)是’類型1‘的,則正常展示,如果是’類型2‘的數(shù)據(jù),則合并當(dāng)前數(shù)據(jù)的動態(tài)表格。

二、實(shí)現(xiàn)思路

1、先將普通表格實(shí)現(xiàn),不考慮合并效果;

2、在表格上對要合并的單元格類型進(jìn)行區(qū)分;

3、 在表格上使用:span-method="arraySpanMethod"方法觸發(fā)表格;

4、在arraySpanMethod方法內(nèi)接收數(shù)據(jù)處理合并,確定從哪一列開始合并到哪一列合并結(jié)束;

三、代碼展示

<el-table
    ref="table"
    size="mini"
    height="100%"
    :data="tableData"
    :span-method="arraySpanMethod"
    :header-cell-style="{
      background: '#f5f7fa',
      fontWeight: 'bold',
      color: '#303133'
    }"
    border
  >
    <el-table-column
      type="index"
      header-align="center"
      align="center"
      label="序號"
      width="50"
    ></el-table-column>
    <el-table-column
      width="120"
      prop="indexShowName"
      label="名稱"
      show-overflow-tooltip
    ></el-table-column>
    <el-table-column
      width="80"
      prop="type"
      label="類型種類"
      show-overflow-tooltip
    >
      <template slot-scope="scope">
        {{ scope.row.type === '1' ? '類型1' : '類型2' }}
      </template>
    </el-table-column>
    <el-table-column
      v-for="(item, index) in tableColumns"
      :key="index"
      width="80"
      :label="item.year"
      show-overflow-tooltip
    >
      <template slot-scope="scope">
        <!-- 類型1展示name -->
        <div
          v-if="scope.row.type === '1'"
          style="text-align: center"
        >
          {{
            scope.row.uploadValueList[index]?.uploadValueName
          }}
        </div>
        <!-- 類型2展示value -->
        <div v-else>
          {{ scope.row.uploadValueList[index].uploadValue }}
        </div>
      </template>
    </el-table-column>
    <el-table-column
      width="160"
      prop="reportDate"
      label="上報(bào)時(shí)間"
      show-overflow-tooltip
    ></el-table-column>
    <el-table-column
      width="195"
      label="操作"
      header-align="center"
      align="center"
      fixed="right"
    >
      <template slot-scope="scope">
        <el-button
          size="small"
          style="color: #409eff; padding: 0"
          type="text"
          @click="detailClick(scope.row)"
          >數(shù)據(jù)明細(xì)</el-button
        >
      </template>
    </el-table-column>
  </el-table>
// --------------methods方法--------------------
 
// 右側(cè)表格
  initTable() {
    const params = {
      pageNum: this.pages.pageIndex,
      pageSize: this.pages.pageSize,
    }
    this.tableLoading = true
    //api接口調(diào)用xxx
    xxx(params)
      .then((res) => {
        if (res.code === 200) {
          const { total } = res.result
          // const { records, total } = res.result
          //后端接口數(shù)據(jù)返回形式如下:
          const records = [
            {
              indexShowName: '測試001',
              type: '1',
              reportDate: '2023-12-01 15:53:46',
              uploadValueList: [
                {
                  id: '1',
                  year: '2021年',
                  uploadValue: '0',
                  uploadValueName: '完全符合'
                },
                {
                  id: '2',
                  year: '2022年',
                  uploadValue: '0',
                  uploadValueName: '完全符合'
                },
                {
                  id: '3',
                  year: '2023年',
                  uploadValue: '0',
                  uploadValueName: '完全符合'
                },
                {
                  id: '4',
                  year: '2024年',
                  uploadValue: '0',
                  uploadValueName: '完全符合'
                }
              ]
            },
            {
              indexShowName: '測試002',
              type: '2',
              reportDate: '2023-12-01 13:45:53',
              uploadValueList: [
                {
                  id: '5',
                  year: '2021年',
                  uploadValue: '99.00'
                },
                {
                  id: '6',
                  year: '2022年',
                  uploadValue: '98.00'
                },
                {
                  id: '7',
                  year: '2023年',
                  uploadValue: '77.00'
                },
                {
                  id: '8',
                  year: '2024年',
                  uploadValue: '34.00'
                }
              ]
            }
          ]
          if (records && records.length > 0) {
            // 使用第一個(gè)元素的 uploadValueList 來創(chuàng)建列
            this.tableColumns = records[0].uploadValueList.map((item) => ({
              year: item.year, // 使用 year 作為列的標(biāo)簽
              id: item.id // 用于做key
            }))
          }
          this.tableData = records
          this.pages.total = total
        } else {
          this.$message.error(res.message)
        }
      })
      .finally(() => {
        this.tableLoading = false
      })
  },
 
  // 單元格合并 {當(dāng)前行row、當(dāng)前列column、當(dāng)前行號rowIndex、當(dāng)前列號columnIndex}
  arraySpanMethod({ row, column, rowIndex, columnIndex }) {
    // 類型1,且動態(tài)數(shù)據(jù)長度>1
    if (row.type === '1' && row?.uploadValueList?.length > 1) {
      const len = row?.uploadValueList?.length
      // 合并從下標(biāo)為0開始的【下標(biāo)為3的第四列,動態(tài)數(shù)據(jù)長度】
      if ( columnIndex > 2 && columnIndex <= 2 + Number(len) ) {
        return {
          rowspan: 1,
          colspan: columnIndex === 3 ? len : 0
        }
      }
    }
  },
  

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue mounted組件的使用

    vue mounted組件的使用

    這篇文章主要介紹了vue mounted組件的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-06-06
  • Vue.set()實(shí)現(xiàn)數(shù)據(jù)動態(tài)響應(yīng)的方法

    Vue.set()實(shí)現(xiàn)數(shù)據(jù)動態(tài)響應(yīng)的方法

    這篇文章主要介紹了Vue.set()實(shí)現(xiàn)數(shù)據(jù)動態(tài)響應(yīng)的相關(guān)知識,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • Vue如何使用本地JSON文件

    Vue如何使用本地JSON文件

    這篇文章主要介紹了Vue如何使用本地JSON文件問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 使用Pinia Persistedstate插件實(shí)現(xiàn)狀態(tài)持久化的操作方法

    使用Pinia Persistedstate插件實(shí)現(xiàn)狀態(tài)持久化的操作方法

    Pinia 作為 Vue 的新一代狀態(tài)管理工具,以其輕量化和易用性受到開發(fā)者的喜愛,然而,Pinia 默認(rèn)使用內(nèi)存存儲狀態(tài),為了解決這個(gè)問題,我們可以借助 Pinia Persistedstate 插件來實(shí)現(xiàn)狀態(tài)的持久化存儲,本文將詳細(xì)介紹該插件的使用方法,需要的朋友可以參考下
    2024-11-11
  • 前端vuex中dispatch的使用方法總結(jié)

    前端vuex中dispatch的使用方法總結(jié)

    這篇文章主要給大家介紹了關(guān)于前端vuex中dispatch使用方法的相關(guān)資料,vuex的dispatch方法用于觸發(fā)一個(gè)action,以便更新state,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-04-04
  • vue3中如何使用live2D

    vue3中如何使用live2D

    本文介紹了如何在Vue3項(xiàng)目中整合Live2D技術(shù),從Live2D的基本介紹到在Vue3中的具體實(shí)現(xiàn)方法,Live2D技術(shù)允許開發(fā)者將二維圖像轉(zhuǎn)化為可動畫的三維模型,主要應(yīng)用于游戲、虛擬角色等領(lǐng)域,文章詳細(xì)說明了在vue3項(xiàng)目中使用Live2D的步驟,感興趣的朋友一起看看吧
    2024-10-10
  • Vue 中為什么不推薦用index 做 key屬性值

    Vue 中為什么不推薦用index 做 key屬性值

    Vue 中使用虛擬 dom 且根據(jù) diff 算法進(jìn)行新舊 DOM 對比,從而更新真實(shí) dom ,key 是虛擬 DOM 對象的唯一標(biāo)識, 在 diff 算法中 key 起著極其重要的作用,本文講解 key 的作用以及為什么最好不要使用 index 作為 key 的屬性值
    2021-11-11
  • Vue 實(shí)現(xiàn)可視化拖拽頁面編輯器

    Vue 實(shí)現(xiàn)可視化拖拽頁面編輯器

    這篇文章主要介紹了Vue 實(shí)現(xiàn)可視化拖拽頁面編輯器的方法,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下
    2021-02-02
  • 前端vue中實(shí)現(xiàn)嵌入代碼編輯器的詳細(xì)代碼

    前端vue中實(shí)現(xiàn)嵌入代碼編輯器的詳細(xì)代碼

    隨著Web技術(shù)的不斷發(fā)展,前端開發(fā)也正日新月異,下面這篇文章主要給大家介紹了關(guān)于前端vue中實(shí)現(xiàn)嵌入代碼編輯器的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-07-07
  • Vue3使用mitt進(jìn)行組件通信的步驟

    Vue3使用mitt進(jìn)行組件通信的步驟

    這篇文章主要介紹了Vue3使用mitt進(jìn)行組件通信的步驟,幫助大家更好的理解和學(xué)習(xí)使用vue,感興趣的朋友可以了解下
    2021-05-05

最新評論