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

el-table 行合并的實(shí)現(xiàn)示例

 更新時(shí)間:2022年08月18日 10:29:03   作者:愛(ài)游戲愛(ài)動(dòng)漫的肥宅  
本文主要介紹了el-table 行合并的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

頁(yè)面實(shí)現(xiàn)行合并的情況還是比較常見(jiàn)的,但實(shí)現(xiàn)起來(lái)卻有一點(diǎn)點(diǎn)難。官網(wǎng)的例子有,不過(guò)人家的合并邏輯是從第一行開(kāi)始兩行兩行合并,不能根據(jù)數(shù)據(jù)動(dòng)態(tài)合并,感覺(jué)參考意義不太大。實(shí)際需求一般都是根據(jù)表數(shù)據(jù)動(dòng)態(tài)來(lái)進(jìn)行合并的,也會(huì)有更復(fù)雜的情況,比如循環(huán)表格的。

以下的例子中,表格數(shù)據(jù)是放在頁(yè)面的假數(shù)據(jù),hbxh 值相同時(shí),表示需要合并

示例一:?jiǎn)蝹€(gè)表格

單個(gè)表格的比較簡(jiǎn)單,知道合并方法的使用基本就好弄了

<template>
  <div>
    <el-form
      ref="testForm"
      :model="testForm"
      style="height: 600px;"
    >
      <el-row
        style="padding: 10px"
      >
        <el-table
          :data="testForm.tableData"
          :span-method="colSpanMethod"
          border
          style="width: 100%;"
        >
          <el-table-column prop="hbxh" label="合并序號(hào)" width="100" />
          <el-table-column prop="name" label="姓名" width="180" />
          <el-table-column prop="address" label="地址" />
        </el-table>
      </el-row>
    </el-form>
  </div>
</template>

<script>

export default {
  data() {
    return {
      spanArr: [], // 用于存放每一行記錄的合并數(shù)
      testForm: {
        tableData: [
          {
            hbxh: '1',
            name: '王小虎',
            address: '上海市普陀區(qū)金沙江路 1518 弄'
          }, {
            hbxh: '2',
            name: '王小虎',
            address: '上海市普陀區(qū)金沙江路 1517 弄'
          }, {
            hbxh: '2',
            name: '王小虎',
            address: '上海市普陀區(qū)金沙江路 1519 弄'
          }, {
            hbxh: '3',
            name: '王小虎',
            address: '上海市普陀區(qū)金沙江路 1516 弄'
          }, {
            hbxh: '4',
            name: '王小虎',
            address: '上海市普陀區(qū)金沙江路 1516 弄'
          }, {
            hbxh: '4',
            name: '王小虎',
            address: '上海市普陀區(qū)金沙江路 1516 弄'
          }, {
            hbxh: '4',
            name: '王小虎',
            address: '上海市普陀區(qū)金沙江路 1516 弄'
          }
        ] // 測(cè)試數(shù)據(jù)
      },
    }
  },
  created() {
    this.getSpanArr(this.testForm.tableData)
  },
  methods: {
    getSpanArr(data) {
      // data就是我們從后臺(tái)拿到的數(shù)據(jù)
      for (var i = 0; i < data.length; i++) {
        if (i === 0) {
          this.spanArr.push(1);
          this.pos = 0;
        } else {
          // 判斷當(dāng)前對(duì)象的指定屬性值與上一個(gè)對(duì)象的指定屬性值是否相等
          if (data[i].hbxh === data[i - 1].hbxh) {
            this.spanArr[this.pos] += 1;
            this.spanArr.push(0);
          } else {
            this.spanArr.push(1);
            this.pos = i;
          }
        }
      }
    },
    colSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0 || columnIndex === 2) {
        const _row = this.spanArr[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        console.log(`rowspan:${_row} colspan:${_col}`);
        return {
          // [0,0] 表示這一行不顯示, [2,1]表示行的合并數(shù)
          rowspan: _row,
          colspan: _col
        };
      }
    }
  }

}
</script>

效果:

示例二:循環(huán)表格

循環(huán)表格時(shí),需要在表的每行數(shù)據(jù)傳入一個(gè)值,代表是第幾個(gè)表格,方便合并方法使用

<template>
  <div>
    <el-form
      ref="testForm"
      :model="testForm"
      style="height: 600px;"
    >
      <el-row
        v-for="(item, index) in testForm.tableList"
        :key="index"
        style="padding: 10px"
      >
        <el-table
          :data="item"
          :span-method="colSpanMethod"
          border
          style="width: 100%; margin-bottom: 20px;"
        >
          <el-table-column prop="hbxh" label="合并序號(hào)" width="100" />
          <el-table-column prop="name" label="姓名" width="180" />
          <el-table-column prop="address" label="地址" />
        </el-table>
      </el-row>
    </el-form>
  </div>
</template>

<script>

export default {
  data() {
    return {
      spanArr: [], // 用于存放每一行記錄的合并數(shù)
      testForm: {
        tableList: [
          [
            {
              hbxh: '1',
              name: '王小虎',
              address: '上海市普陀區(qū)金沙江路 1518 弄',
              tbIndex: 0
            }, {
              hbxh: '1',
              name: '王小虎',
              address: '上海市普陀區(qū)金沙江路 1517 弄',
              tbIndex: 0
            }, {
              hbxh: '2',
              name: '王小虎',
              address: '上海市普陀區(qū)金沙江路 1519 弄',
              tbIndex: 0
            }, {
              hbxh: '2',
              name: '王小虎',
              address: '上海市普陀區(qū)金沙江路 1516 弄',
              tbIndex: 0
            }
          ],
          [
            {
              hbxh: '1',
              name: '王小虎',
              address: '上海市普陀區(qū)金沙江路 1518 弄',
              tbIndex: 1
            }, {
              hbxh: '2',
              name: '王小虎',
              address: '上海市普陀區(qū)金沙江路 1517 弄',
              tbIndex: 1
            }, {
              hbxh: '2',
              name: '王小虎',
              address: '上海市普陀區(qū)金沙江路 1519 弄',
              tbIndex: 1
            }, {
              hbxh: '3',
              name: '王小虎',
              address: '上海市普陀區(qū)金沙江路 1516 弄',
              tbIndex: 1
            }, {
              hbxh: '4',
              name: '王小虎',
              address: '上海市普陀區(qū)金沙江路 1516 弄',
              tbIndex: 1
            }, {
              hbxh: '4',
              name: '王小虎',
              address: '上海市普陀區(qū)金沙江路 1516 弄',
              tbIndex: 1
            }, {
              hbxh: '4',
              name: '王小虎',
              address: '上海市普陀區(qū)金沙江路 1516 弄',
              tbIndex: 1
            }
          ]
        ] // 測(cè)試數(shù)據(jù)
      },
    }
  },
  created() {
    this.getSpanArr(this.testForm.tableList)
  },
  methods: {
    getSpanArr(data) {
      // 用一個(gè)對(duì)象數(shù)組來(lái)存放每個(gè)表中每一行記錄的合并數(shù)
      for (let m = 0, n = data.length; m < n; m++) {
        this.spanArr.push({
          tbArr: []
        });
      }
      for (let i = 0, l = data.length; i < l; i++) {
        let contactDot = 0; // 記錄開(kāi)始合并的行索引
        data[i].forEach((item, index) => {
          item.index = index
          if (index === 0) {
            this.spanArr[i].tbArr.push(1);
          } else {
            // 判斷當(dāng)前對(duì)象的指定屬性值與上一個(gè)對(duì)象的指定屬性值是否相等
            if (item.hbxh === data[i][index - 1].hbxh) {
              this.spanArr[i].tbArr[contactDot] += 1;
              this.spanArr[i].tbArr.push(0);
            } else {
              this.spanArr[i].tbArr.push(1);
              contactDot = index;
            }
          }
        })
      }
      console.log('==========this.spanArr==========')
      console.log(this.spanArr)
      /* [
        [2, 0, 2, 0],
        [1, 2, 0, 1, 3, 0, 0]
      ] */
    },
    colSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 2) {
        const _row = this.spanArr[row.tbIndex].tbArr[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        return {
          // [0,0] 表示這一行不顯示, [2,1]表示行的合并數(shù)
          rowspan: _row,
          colspan: _col
        };
      }
    }
  }

}
</script>

有循環(huán)的需求時(shí),數(shù)據(jù)結(jié)構(gòu)會(huì)比較復(fù)雜,需要耐心理清數(shù)據(jù)之間的關(guān)系

效果:

合并的關(guān)鍵主要就是要準(zhǔn)確計(jì)算出每行記錄的合并數(shù),計(jì)算邏輯可能每個(gè)人會(huì)想得不太一樣,以上的栗子僅代表一種實(shí)現(xiàn)方式,不同方式的朋友可以留言討論

>>>>>>>>>今天又發(fā)現(xiàn)了一種新的邏輯>>>>>>>>>>>>>(2021-11-29)

<template>
  <div>
    <el-form
      ref="testForm"
      :model="testForm"
      style="height: 600px;"
    >
      <el-row
        v-for="(item, index) in testForm.tableList"
        :key="index"
        style="padding: 10px"
      >
        <el-table
          :data="item"
          :span-method="colSpanMethod2"
          border
          style="width: 100%; margin-bottom: 20px;"
        >
          <el-table-column prop="hbxh" label="合并序號(hào)" width="100" />
          <el-table-column prop="name" label="姓名" width="180" />
          <el-table-column prop="address" label="地址" />
        </el-table>
      </el-row>
    </el-form>
  </div>
</template>

<script>

export default {
  data() {
    return {
      spanArr: [], // 用于存放每一行記錄的合并數(shù)
      testForm: {
        tableList: [
          [
            {
              hbxh: '1',
              name: '王小虎',
              address: '上海市普陀區(qū)金沙江路 1518 弄',
              tbIndex: 0
            }, {
              hbxh: '1',
              name: '王小虎',
              address: '上海市普陀區(qū)金沙江路 1517 弄',
              tbIndex: 0
            }, {
              hbxh: '2',
              name: '王小虎',
              address: '上海市普陀區(qū)金沙江路 1519 弄',
              tbIndex: 0
            }, {
              hbxh: '2',
              name: '王小虎',
              address: '上海市普陀區(qū)金沙江路 1516 弄',
              tbIndex: 0
            }
          ],
          [
            {
              hbxh: '1',
              name: '王小虎',
              address: '上海市普陀區(qū)金沙江路 1518 弄',
              tbIndex: 1
            }, {
              hbxh: '2',
              name: '王小虎',
              address: '上海市普陀區(qū)金沙江路 1517 弄',
              tbIndex: 1
            }, {
              hbxh: '2',
              name: '王小虎',
              address: '上海市普陀區(qū)金沙江路 1519 弄',
              tbIndex: 1
            }, {
              hbxh: '3',
              name: '王小虎',
              address: '上海市普陀區(qū)金沙江路 1516 弄',
              tbIndex: 1
            }, {
              hbxh: '4',
              name: '王小虎',
              address: '上海市普陀區(qū)金沙江路 1516 弄',
              tbIndex: 1
            }, {
              hbxh: '4',
              name: '王小虎',
              address: '上海市普陀區(qū)金沙江路 1516 弄',
              tbIndex: 1
            }, {
              hbxh: '4',
              name: '王小虎',
              address: '上海市普陀區(qū)金沙江路 1516 弄',
              tbIndex: 1
            }
          ]
        ] // 測(cè)試數(shù)據(jù)
      },
    }
  },
  created() {
    this.getSpanArr2(this.testForm.tableList)
  },
  methods: {
    getSpanArr2(data) {
      for (let i = 0, l = data.length; i < l; i++) {
        let orderObj = {}
        data[i].forEach((item, index) => {
          // item.index = index
          if (orderObj[item.hbxh]) {
            orderObj[item.hbxh].push(index)
          } else {
            orderObj[item.hbxh] = []
            orderObj[item.hbxh].push(index)
          }
          this.spanArr[i] = {
            orderIndexArr: []
          }
          // 將數(shù)組長(zhǎng)度大于1的值 存儲(chǔ)到this.orderIndexArr(也就是需要合并的項(xiàng))
          Object.keys(orderObj).forEach((key) => {
            if (orderObj[key].length > 1) {
              this.spanArr[i].orderIndexArr.push(orderObj[key])
            }
          })
        })
      }
      console.log('==========this.spanArr==========')
      console.log(this.spanArr)
    },
    colSpanMethod2({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 2) {
        for (let i = 0, l = this.spanArr[row.tbIndex].orderIndexArr.length; i < l; i++) {
          let element = this.spanArr[row.tbIndex].orderIndexArr[i]
          for (let j = 0; j < element.length; j++) {
            let item = element[j]
            if (rowIndex === item) {
              if (j === 0) {
                return {
                  rowspan: element.length,
                  colspan: 1
                }
              } else {
                return {
                  rowspan: 0,
                  colspan: 0
                }
              }
            }
          }
        }
      }
    }
  }

}
</script>

效果:
同上

來(lái)看看前端打印的信息:

 到此這篇關(guān)于el-table 行合并的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)el-table 行合并內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論