vue前端界面 el-table将接口传入的数据转换成想要的文字显示

本文总结了在Vue中进行数字类型转换和时间戳转换的方法,包括使用三元运算符、定义数据映射和 formatter 函数。示例展示了如何在表格组件中根据状态值显示不同文字,以及如何将时间戳转换为易读的时间格式。提供了实用的代码片段和时间处理函数。


前言

总结记录一下vue中的前端界面显示的技巧


一、数字类型转换

1. 在三元运算符中加入判断,以及嵌套实现

<el-table-column label="状态" prop="status" align="center" width="80px">
        <template slot-scope="scope">
          {{ scope.row.status== 1 ? '待确认' : scope.row.type == 2 ?'已确认': '' }} 
        </template>
</el-table-column>

2. 在data中定义一个map存储根据map的值来对应转换的数据

<el-table-column label="状态" prop="status" align="center" width="80px">
        <template slot-scope="scope">
          {{ testEnum[scope.row.status] }}
        </template>
</el-table-column>
data() {
    return {
        testEnum: {
          1: '待确认',
          2: '已确认'
      }
    }
  },

3. 使用 :formatter 来进行数据转换。

<el-table-column label="状态" prop="status" align="center" width="80px" :formatter="formatStatus">
</el-table-column>
formatStatus(row) {
                return row.status=== "1" ? "待确认" :
                       row.status=== "2" ? "已确认" : "";
            },

二、时间戳转换

<el-table-column label="创建时间" prop="createTime" :formatter="timestampToTime" align="center" width="140" />
// 时间戳转化为时间
timestampToTime(row, column) {
        var date = new Date(row.createTime) // 时间戳为10位需*1000,时间戳为13位的话不需乘1000
        var Y = date.getFullYear() + '-'
        var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
        var D = date.getDate() + ' '
        var h = date.getHours() + ':'
        var m = date.getMinutes() + ':'
        var s = date.getSeconds()
        return Y + M + D + h + m + s
    },

参考网址:
https://www.jianshu.com/p/62a90e3fec64


总结

目前vue前端界面转换就是这些了,以后会慢慢补充的

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值