vue3表格组件formatter

有时候在网页上显示表格数据时,表格的某些列值只有有限数目(例如,启用/停用),这时候后端常常使用不同的数据值表示不同状态,前端怎么将这些数据值转化为相应的列值呢?

我们可以采用vue3表格组件formatter实现这一点。

例如:

后端返回的数据格式:

java 复制代码
tableData:[
  {
    "id": "1",
    "recCreateTime": "2024-08-01 09:27:47",
    "name": "张三",
    "sex": 0,
    "status":1
  },
  {
    "id": "2",
    "recCreateTime": "2024-08-01 09:28:10",
    "name": "李四",
    "sex": 0,
    "status":0
  },
  {
    "id": "3",
    "recCreateTime": "2024-08-01 09:28:41",
    "name": "王五",
    "sex": 1,
    "status":1
  },
  {
    "id": "4",
    "recCreateTime": "2024-08-01 09:30:03",
    "name": "刘六",
    "sex": 0,
    "status":1
  }
]

前端可以这样处理:

html 复制代码
<el-table :data="tableData" style="width: 100%" :height="screenHeight * 0.8">
	  <el-table-column label="序号" width="50px" align="center">
	    <template slot-scope="scope">
	      {{ scope.$index + 1 }}
	    </template>
	  </el-table-column>
	  <el-table-column :label="$t('用户名')" prop="name" align="center"/>
	  <el-table-column :label="$t('创建日期')" prop="recCreateTime" align="center"/>
	  <el-table-column :label="$t('性别')" prop="sex" align="center" :formatter="sextypeFormatter"/>
	  <el-table-column :label="$t('状态')" prop="status" align="center" :formatter="statustypeFormatter"/>
</el-table>

export default {
	methods: {
	    sextypeFormatter: function (row) {
	        if(row.sex == 0){
	          return "男"
	        }
	        else if(row.sex == 1){
	          return "女"
	        }
	    },
	    statustypeFormatter: function (row) {
	        if(row.status == 0){
	          return "未启用"
	        }
	        else{
	          return "已启用"
	        }
	    }
}
相关推荐
pan_junbiao5 小时前
Vue组件:使用$emit()方法监听子组件事件
前端·javascript·vue.js
正在绘制中6 小时前
如何部署Vue+Springboot项目
前端·vue.js·spring boot
Loong_DQX6 小时前
【前端】vue+html+js 实现table表格展示,以及分页按钮添加
前端·javascript·vue.js
程序员皮皮林7 小时前
【Java毕业设计】基于SpringBoot+Vue+uniapp的农产品商城系统
vue.js·spring boot·uni-app
Rverdoser7 小时前
Vue3 中的响应式系统:深入理解 Proxy API
前端·javascript·vue.js
Alan-Xia7 小时前
Vue响应式进阶常用API之effectScope、getCurrentScope、onScopeDispose学习
前端·vue.js·学习
SnowMan19937 小时前
使用Nginx部署前端Vue项目的详细指南
前端·vue.js·nginx
涵一8 小时前
动态获取git版本号
vue
怪咖码农8 小时前
vue3获取视频时长、码率、格式等视频详细信息
前端·vue·音视频
GISer_Jing9 小时前
大厂前端常见的笔试题目
javascript·vue.js·uni-app