Vue数据格式化,:formatter的使用

在Vue中使用ElementUi表格,对数据进行格式化处理
1、后端给我返回的格式如下,我要根据他提供的数据,将其转变为文字:
在这里插入图片描述
2、我们可以在需要处理行里,加入:formatter方法,具体代码如下:

      <el-table-column prop="protocolType" label="协议类型" :formatter="formatProtocolType" >
      </el-table-column>

3、在js中进行格式处理
既可以使用if判断,也可使用switch case判断,亦或使用三元表达式

  • switch case:
  // 处理协议类型
    formatProtocolType(row) {
      let type = row.protocolType;
      switch (type) {
        case 1:
          type = "普通";
          break;
        case 2:
          type = "移动NB协议";
          break;
        case 3:
          type = "LORA";
          break;
        case 4:
          type = "电信NB协议";
          break;
        case 5:
          type = "loraWan";
          break;
        case 9:
          type = "其他";
          break;
      }

      return type;
    },
  • 三元表达式
    // 处理通信状态
    formatCommunicationStatus(row) {
      let type = row.communicationStatus;
      return type == 1 ? "正常" : "离线";
    },
  • if判断
    // 集中器类型
    formatType(row) {
      let type = row.type;
      if (type == "") {
        type = "";
      } else if (type == "1") {
        type = "虚拟";
      } else if (type == "2") {
        type = "真实";
      }
      return type;
    },
  • 12
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值