在表格中显示字典的内容(根据后端返回的数据)vue3

进入页面,调接口,后端返回数据,indexType为0或者1,要用这个数据显示字典的内容

用插槽拿到数据

写一个函数,在模板中使用

const { proxy } = getCurrentInstance();
// 字典-指标类型
const { index_type } = proxy.useDict("index_type");
// 写方法处理指标类型的显示-注意:如果这个函数最后返回的是undefined,undefined在模板上是不显示的
const handleIndexType = (data) => {
  return index_type.value.find((item) => {
    return item.value === data;
  })?.label;
};

 

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Vue 的计算属性来根据不同的状态值判断使用不同的数据字典,并根据字典内容动态显示表格内容。具体步骤如下: 1. 在 Vue 组件定义一个对象或数组,表示状态值与对应的数据字典的映射关系。例如: ``` const statusDict = { 1: { text: '待审核', color: 'orange' }, 2: { text: '已审核', color: 'green' }, 3: { text: '审核不通过', color: 'red' } }; ``` 2. 在 Vue 组件定义一个计算属性 `tableData`,根据状态值从数据字典获取对应的文本和颜色,并将其作为表格的一列数据。例如: ``` <template> <table> <thead> <tr> <th>编号</th> <th>名称</th> <th>状态</th> </tr> </thead> <tbody> <tr v-for="(item, index) in tableData" :key="index"> <td>{{ item.id }}</td> <td>{{ item.name }}</td> <td :style="{ color: item.status.color }">{{ item.status.text }}</td> </tr> </tbody> </table> </template> <script> const statusDict = { 1: { text: '待审核', color: 'orange' }, 2: { text: '已审核', color: 'green' }, 3: { text: '审核不通过', color: 'red' } }; export default { data() { return { data: [ { id: 1, name: '商品1', status: 1 }, { id: 2, name: '商品2', status: 2 }, { id: 3, name: '商品3', status: 3 } ] }; }, computed: { tableData() { return this.data.map(item => { return { id: item.id, name: item.name, status: statusDict[item.status] }; }); } } }; </script> ``` 在上述代码,通过计算属性 `tableData`,将原始数据的状态值转换成对应的文本和颜色,并将其作为表格的一列数据显示出来。其,`:style="{ color: item.status.color }"` 控制状态列的颜色,`item.status.text` 控制状态列的文本内容。 需要注意的是,在实际开发数据字典和状态值可能是从后端接口动态获取的,需要根据实际情况进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值