在element-UI table中通过slot-scope=“scope“实现获取值并将金额和时间格式化方法

在element-UI table中实现金额和时间格式化:

有时候element-UI table中,我们需要将后端给的金额或时间需要额外处理来展示,比如将金额60000.06处理成60,000.00这种格式,再比如将时间格式化,怎么来实现呢:

                <el-table-column prop="loanAmt" label="放款金额">
                  <template slot-scope="scope">
                    <span>{{scope.row.loanAmt | formatStatus}}</span>
                  </template>
                </el-table-column>
                <el-table-column prop="loanDate" label="放款日期">
                  <template slot-scope="scope">
                    <span>{{scope.row.loanDate | formatterDate}}</span>
                  </template>
                </el-table-column>

首先,我们看看slot-scope="scope":

slot-scope="scope" 来取得作用域插槽:data绑定的数据,scope可以随便替换其他名称,只是定义对象来代表取得的data数据

然后,<span>{{scope.row.loanAmt | formatStatus}}</span>这里scope.row.loanAmt就是一个金额,然后通过filters(过滤器)去处理格式化:

  filters: {
    formatStatus: function(val) {
      return toThousandslsFilter(val, 2);
    },
    formatterDate: function(val) {
      if (val) {
        let txt = val.slice(0, 10);
        return txt.slice(0, 4) + "-" + txt.slice(4, 6) + "-" + txt.slice(6, 8);
      } else {
        return "";
      }
    },
  }

ok,再看看toThousandslsFilter是怎么处理的:

toThousandslsFilter(num, digits) {
  num = (num + '').replace(/\,/g, '')
  return (+num || 0).toFixed(digits).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}

通过这个案例主要说了两点:

1、在el-table中怎么获取值

2、filters(过滤器)怎么来用

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值