element-ui表头动态渲染

在这里插入图片描述

<template>
  <el-table :data="tableData" style="width: 100%" border>
    <el-table-column prop="CharacterPostCode" label="编码" width="180"> </el-table-column>
    <el-table-column prop="Remarks" label="备注" width="180"> </el-table-column>
    <el-table-column v-for="(item,index) in FBAColumns" :prop="item.prop" :label="item.label"> </el-table-column>
  </el-table>
</template>

<script>
export default {
  data() {
    return {
      /* tableData中的每一项RangePrice都要是一样的 */
      tableData: [
        {
          CharacterPostCode: 'BHX1',
          RangePrice: [{ Range: '0-20(CBM)', Price: 2050.0 }, { Range: '20以上(CBM)', Price: 2000.0 }],
          FbaServiceType: 1,
          Remarks: '优先级最低,不能匹配其它报价方案时执行'
        },
        {
          CharacterPostCode: 'BHX2',
          RangePrice: [{ Range: '0-20(CBM)', Price: 2050.0 }, { Range: '20以上(CBM)', Price: 2000.0 }],
          FbaServiceType: 1,
          Remarks: '优先级最低,不能匹配其它报价方案时执行'
        },
        {
          CharacterPostCode: 'BHX7',
          RangePrice: [{ Range: '0-20(CBM)', Price: 2050.0 }, { Range: '20以上(CBM)', Price: 2000.0 }],
          FbaServiceType: 1,
          Remarks: '优先级最低,不能匹配其它报价方案时执行'
        },
      ],
      FBAColumns: []
    }
  },
  mounted() {
      // 获取数组第一项
    const row = this.tableData[0].RangePrice
    this.FBAColumns = []

    // 填充表头
    row.map((obj, index) => {
      this.FBAColumns.push({
        key: parseInt(index + 1),
        label: obj.Range,
        prop: obj.Range,
        width: 200
      })
    })
    this.tableData.map(item => {
      item.RangePrice.map(ix => {
        item[ix.Range] = ix.Price
      })
    })
    console.log('row: ', row);
    console.log('this.FBAColumns: ', this.FBAColumns);
    console.log('this.tableData: ', this.tableData);
  }
}
</script>
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
您可以使用 ElementUI 表格的 `header-cell-style` 属性来改变表头单元格的样式。 具体实现方式如下: 1. 在表格的 `el-table-column` 组件中添加 `header-cell-style` 属性,并将其设置为一个对象。例如,以下代码将表头单元格的背景色设置为灰色: ```html <el-table-column prop="name" label="姓名" :header-cell-style="{ 'background-color': '#ccc' }"></el-table-column> ``` 2. 在 `header-cell-style` 对象中,可以设置 CSS 样式,例如 `background-color`、`color`、`font-size` 等。您可以根据具体的需求进行设置。 ```html <el-table-column prop="name" label="姓名" :header-cell-style="{ 'background-color': '#ccc', 'color': '#fff', 'font-size': '16px' }"></el-table-column> ``` 注意,您也可以在表格组件的 `header-cell-class-name` 属性中设置表头单元格的类名,然后在 CSS 样式表中定义相应的样式,以实现更加灵活的样式设置。例如: ```html <el-table :data="tableData" :header-cell-class-name="headerCellClassName"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> </el-table> ``` ```js methods: { headerCellClassName({ column }) { if (column.property === 'age') { return 'header-cell-age'; } } } ``` ```css .header-cell-age { background-color: #ccc; color: #fff; font-size: 16px; } ``` 在上面的代码中,我们使用 `header-cell-class-name` 属性来设置表头单元格的类名,然后在 CSS 样式表中定义了 `.header-cell-age` 类来设置表头单元格的样式。这种方式可以更加灵活地控制表头单元格的样式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值