在 element-ui 中,可以使用 formatter 属性来格式化表格中的数据。如果要实现动态的 formatter,可以在表格中使用 render 函数来实现。

具体步骤如下:

  1. 在表格中定义 render 函数,并将 formatter 属性设置为 null:
<el-table-column prop="price" label="价格" :formatter="null" :render-header="renderHeader"></el-table-column>
  1. 在 render 函数中,根据数据动态生成 formatter,并返回格式化后的数据:
<el-table-column prop="price" label="价格" :formatter="null" :render-header="renderHeader">
  <template slot-scope="scope">
    {{ formatNumber(scope.row.price, scope.row.currency) }}
  </template>
</el-table-column>

...

methods: {
  formatNumber(price, currency) {
    let formatter = null
    if (currency === 'CNY') {
      formatter = val => `¥ ${val}`
    } else if (currency === 'USD') {
      formatter = val => `$ ${val}`
    }
    return formatter ? formatter(price) : price
  }
}

在上面的例子中,根据不同的货币类型动态生成了不同的 formatter,用于格式化价格数据。


原文地址: https://www.cveoy.top/t/topic/bssi 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录