表格动态列展示

需求:动态列数据展示

<el-table
                ref="table"
                :data="tableData"
                :header-cell-style="{ background: '#F5F7FA', height: '30px' }"
                style="width: 100%; margin: 0 auto"
                height="100%"
                align="center"
                stripe
                row-key="id"
                border
                :row-class-name="tableRowClassName"
                @sort-change="sortChange"
                @cell-click="tabClick"
                @header-dragend="
                  () => {
                    $nextTick(() => {
                      this.$refs.table && this.$refs.table.doLayout() // 解决表格错位
                    })
                  }
                "
              >
                <el-table-column
                  v-for="(item, index) in viewColumns"
                  :key="index"
                  :fixed="item.fixed"
                  :prop="item.prop"
                  :align="item.align"
                  :label="item.label"
                  :min-width="item.width"
                  sortable="custom"
                  :show-overflow-tooltip="true"
                />
              </el-table>
 async getTabelData(data) {
      const params = {
        pageindex: this.page,
        pagesize: this.pageSize,
        sortfield: '',
        sorttype: 'asc',
      }
      const res = await datalistDetail(params)
      if (res && res.rows && res.rows.length > 0) {
//modelCount是后端返回的每一个列里面的动态数据
        if (String(this.page) === '1' && (JSON.stringify(res.rows[0].modelCount) !== '{}' || res.rows.modelCount !== undefined)) {
          this.oneList = JSON.parse(JSON.stringify(res.rows[0].modelCount)) // 获取静态列的数据
        }
        // 判断静态列有没有
        if (this.oneList) {
          let inSertColums = []
//viewColumnsOne 这个数据是静态的
          this.viewColumnsOne = [ 
            { prop: 'mishiDay', width: '120', align: 'left', label: '静态数据1', fixed: false },
            { prop: 'endWeek', width: '120', align: 'left', label: '静态数据2', fixed: false }
          ]
          for (const property in this.oneList) {
            inSertColums = [...inSertColums, { prop: property, width: '120', align: 'left', label: property, fixed: false }]
            res.rows.map((item) => {
              item[property] = item.modelCount[property]
            })
          }
          inSertColums.map((item, index) => {
            this.viewColumnsOne.splice(Number(14), 0, item)
          })
          const obj = {}
          this.viewColumns = this.viewColumnsOne.reduce((prev, cur) => {
            obj[cur.prop] ? '' : (obj[cur.prop] = true && prev.push(cur))
            return prev
          }, []) //动态数据拼接
          this.tableData = res.rows
          this.total = res.rowcount
        }
      } else {
        this.viewColumns = this.viewColumnsOne
        this.tableData = []
        this.total = 0
      }
      // datalistDetail(params).then((res) => {
      //   this.viewColumnsEnd = []
      //   this.tableData = res.rows || []
      //   this.total = res.rowcount || 0
      // })
      // .finally(() => {
      //   this.$nextTick(() => {
      //     this.$refs.table && this.$refs.table.doLayout() // 解决表格错位
      //   })
      // })
    },

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值