美文网首页
el-table 合并相同内容单元格

el-table 合并相同内容单元格

作者: 驴不与红鲤鱼 | 来源:发表于2020-09-15 08:26 被阅读0次
    微信截图_20200915081644.png
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
      <title></title>
      <style>
        
      </style>
    </head>
    <body>
      <div id="app">
        <el-table
          :span-method="arraySpanMethod"
          :data="tableData"
          border
          style="width: 100%">
          <el-table-column
            prop="date"
            label="日期"
            width="180">
          </el-table-column>
          <el-table-column
            prop="name"
            label="姓名"
            width="180">
          </el-table-column>
          <el-table-column
            prop="address"
            label="地址">
          </el-table-column>
        </el-table>
      </div>
      <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script><!-- 引入样式 -->
      <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    
      <!-- 引入组件库 -->
      <script src="https://unpkg.com/element-ui/lib/index.js"></script>
      <script>
        var app = new Vue({
          el: '#app',
          data: {
            tableData: [{
              date: '2016-05-02',
              name: '王小龙',
              address: '上海市普陀区金沙江路 1 弄'
            }, {
              date: '2016-05-02',
              name: '王小龙',
              address: '上海市普陀区金沙江路 2 弄'
            }, {
              date: '2016-05-02',
              name: '王小虎',
              address: '上海市普陀区金沙江路 3 弄'
            }, {
              date: '2016-05-02',
              name: '王小虎',
              address: '上海市普陀区金沙江路 5 弄'
            }, {
              date: '2016-05-02',
              name: '王小虎',
              address: '上海市普陀区金沙江路 4 弄'
            }],
            mergeObj: {}
          },
          methods: {
            arraySpanMethod ({ row, column, rowIndex, columnIndex }) {
              if (this.mergeObj[column.property][rowIndex]) {
                return {
                  rowspan:this.mergeObj[column.property][rowIndex],
                  colspan:1
                }
              } else {
                return {
                  rowspan: 0,
                  colspan: 0
                }
              }
            },
            
          },
          mounted () {
            let numerical = 0;
            for (let key in this.tableData[0]) {
              this.mergeObj[key] = []
              this.tableData.forEach((item,index) => {
                if (index === 0) {
                  this.mergeObj[key].push(1)
                } else {
                  if (item[key] === this.tableData[index-1][key]) {
                    this.mergeObj[key][numerical] += 1;
                    this.mergeObj[key].push(0)
                  } else {
                    numerical = index
                    this.mergeObj[key].push(1)
                  }
                }
              })
            }
          }
        })
      </script>
    </body>
    </html>
    

    相关文章

      网友评论

          本文标题:el-table 合并相同内容单元格

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