美文网首页
element-ui表格单元格合并

element-ui表格单元格合并

作者: 梦里梦不到的梦_b5c8 | 来源:发表于2019-12-05 11:26 被阅读0次
<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="地区">
    </el-table-column>
    <el-table-column
      prop="type"
      label="类型">
    </el-table-column>
    <el-table-column
      prop="company.name"
      label="企业名称">
    </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: '外贸',
          company: [
            { name: '深圳外贸公司1' },
            { name: '深圳外贸公司2' },
            { name: '深圳外贸公司3' },
            { name: '深圳外贸公司4' }
          ]
        },
        {
          id: 2,
          region: '北京',
          type: '金融',
          company: [
            { name: '北京金融公司1' },
            { name: '北京金融公司2' },
            { name: '北京金融公司3' }
          ]
        },
        {
          id: 2,
          region: '上海',
          type: '金融',
          company: [{ name: '上海金融公司1' }, { name: '上海金融公司2' }]
        }
      ],
      tableData: [], //表格数据
      arr: [] //合并参数
    }
  },
  created() {
    this.initData(this.table)
  },
  methods: {
    // 初始化数据
    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 // 合并的行数
        })
      })
    },

    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>

相关文章

网友评论

      本文标题:element-ui表格单元格合并

      本文链接:https://www.haomeiwen.com/subject/bkrdgctx.html