element-ui表格單元格合并

<template>
  <div class="hello">
    <el-table
    :data="tableData"
    :span-method="arraySpanMethod"
    border
    style="width: 100%">
    <el-table-column
      prop="id"
      label="ID"
      width="180">
    </el-table-column>
    <el-table-column
      prop="region"
      label="地區(qū)">
    </el-table-column>
    <el-table-column
      prop="type"
      label="類型">
    </el-table-column>
    <el-table-column
      prop="company.name"
      label="企業(yè)名稱">
    </el-table-column>
  </el-table>

  </div>
</template>

<script>
// import VueGridLayout from 'vue-grid-layout'
export default {
  name: 'HelloWorld',
  data() {
    return {
      table: [
        {
          id: 1,
          region: '深圳',
          type: '外貿(mào)',
          company: [
            { name: '深圳外貿(mào)公司1' },
            { name: '深圳外貿(mào)公司2' },
            { name: '深圳外貿(mào)公司3' },
            { name: '深圳外貿(mào)公司4' }
          ]
        },
        {
          id: 2,
          region: '北京',
          type: '金融',
          company: [
            { name: '北京金融公司1' },
            { name: '北京金融公司2' },
            { name: '北京金融公司3' }
          ]
        },
        {
          id: 2,
          region: '上海',
          type: '金融',
          company: [{ name: '上海金融公司1' }, { name: '上海金融公司2' }]
        }
      ],
      tableData: [], //表格數(shù)據(jù)
      arr: [] //合并參數(shù)
    }
  },
  created() {
    this.initData(this.table)
  },
  methods: {
    // 初始化數(shù)據(jù)
    initData(data) {
      console.log(11, data)
      this.tableData = []
      const tempdata = data.filter(item => item.company)
      let s = 0
      tempdata.forEach((item, i, data) => {
        item.company.forEach(e => {
          this.tableData.push({
            id: item.id,
            region: item.region,
            type: item.type,
            company: e
          })
        })
        if (this.arr.length) {
          s = this.arr[this.arr.length - 1].row + data[i - 1].company.length
        }
        this.arr.push({
          row: s, // 合并的起始行
          index: item.company.length // 合并的行數(shù)
        })
      })
    },

    arraySpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0 || columnIndex === 1 || columnIndex === 2) {
        let obj = []
        this.arr.forEach(item => {
          if (rowIndex === item.row) {
            obj = [item.index, 1]
          }
        })
        return obj
      }
    }
  }
}
</script>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容