vue 根据不同的状态显示不同的颜色

场景:
根据不同的状态显示不同的背景色,共有以下几种状态
a. 草稿
b. 待开始
c. 进行中
d. 已暂停
e. 已结束

实现效果:
在这里插入图片描述

代码实现:

<template>
  <div>
    <ElTable :data="tableData">
      <ElTableColumn
        label="状态"
        width="115">
        <template slot-scope="scope">
          <p class="cm-status" :style="{'background': caseStatusColorFilter(scope.row.activityStatus)}">{{statusText(scope.row.activityStatus)}}</p>
        </template>
      </ElTableColumn>
    </ElTable>
  </div>
</template>
<script>
export default {
  data () {
    return {
      // 状态
      caseStatusMap: [
        {
          code: 1,
          name: '草稿',
          bgc: '#EFEFEF'
        },
        {
          code: 2,
          name: '待开始',
          bgc: '#E6F2FF'
        },
        {
          code: 3,
          name: '进行中',
          bgc: '#EDFAE1'
        },
        {
          code: 4,
          name: '已暂停',
          bgc: ' #FFEBD3'
        },
        {
          code: 5,
          name: '已结束',
          bgc: '#EFEFEF'
        }],

      tableData: [
        {
          id: 0,
          activityStatus: 1
        },
        {
          id: 1,
          activityStatus: 2
        },
        {
          id: 2,
          activityStatus: 3
        },
        {
          id: 3,
          activityStatus: 4
        }
      ]
    }
  },
  methods: {
    // 颜色过滤
    caseStatusColorFilter (val) {
      let col = null
      this.caseStatusMap.forEach(arg => {
        if (arg.code === val) {
          col = arg.bgc
        }
      })
      return col
    },

    statusText (status) {
      switch (status) {
        case 1: return '草稿'
        case 2: return '待开始'
        case 3: return '进行中'
        case 4: return '已暂停'
        case 5: return '已结束'
      }
    }
  }
}
</script>
<style scoped>
.cm-status {
  display: inline-block;
  border-radius: 2px;
  padding: 2px 8px;
  font-size: 12px;
  line-height: 20px;
}
</style>

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值