elementUI tag标签和表格样式设置

1、在vue表格中使用

   <template slot-scope="scope">
          <template v-if="column.prop==='status'">
            <el-tag :type="formatTagType(scope.row.status)">{{ statusArr.labelByValue[scope.row.siteStatus] }}</el-tag>
          </template>
   </template>

2.dict.js 文件中


import { dictGetLabelByValue, dictGetValueByLabel } from '@/api/dictDetail'
let that
export default {
  data() {
    return {
      dictStatus: [],
      // 字典转换相关数据
      statusArr: {
        valueByLabel: '',
        labelByValue: ''
      }
    }
  },
  created() {
    //  所有字典
    this.getNeedDicts()
  },
  beforeCreate() { // 全局this
    that = this
  },

  filters: {
    // 时间过滤器
    filterTime: function(time) {
      if (!time) {
        return ''
      }
      const date = new Date(time)
      const dateNumFun = num => (num < 10 ? `0${num}` : num)
      const [Y, M, D, h, m, s] = [
        // es6 解构赋值
        date.getFullYear(),
        dateNumFun(date.getMonth() + 1),
        dateNumFun(date.getDate()),
        dateNumFun(date.getHours()),
        dateNumFun(date.getMinutes()),
        dateNumFun(date.getSeconds())
      ]
      return `${Y}-${M}-${D} ${h}:${m}:${s}` 
    },
    // 状态过滤器
    filterApprovalStatus: function(value) {
      const label = that.statusArr.labelByValue[value]
      if (typeof label !== 'string') {
        return ''
      }
      return label
    }

  },
  methods: {
    //tag标签获取类型
    formatTagType(state) {
      if (state === 0) return 'info'
      else if (state === 1 || state === 3) return 'success'
      else if (state === 2 || state === 4) return 'danger'
      else return 'success'
    },

    async  getNeedDicts() {
      const _this = this
      // 拿到状态字典
      dictGetValueByLabel('status').then(function(result) {
        _this.statusArr.valueByLabel = result
      })
      dictGetLabelByValue('status').then(function(result) {
        _this.statusArr.labelByValue = result
      })
    }

  }
}

3、表格表头样式和斑马纹样式设置
在表头添加如下代码

 <el-table
      v-loading="loading"
      id="drag-table"
      ref="table"
      :data="data"
      :height="height"
      :header-cell-style="tableHeaderColor"
      :row-class-name="tableRowClassName"
      resizable
      border
      style="width: 100%"
      @row-click="clickRow"
      @row-dblclick="edit"
      @selection-change = "selectData">

js代码如下

  // 斑马纹表格样式
    tableRowClassName({ row, rowIndex }) {
      let color = ''
      if (rowIndex % 2 === 0) {
      //class名    设置相应样式
          color = 'success-row'
       } else {
        color = 'warning-row'
      }
     if (row.isShow) {
        color = 'table-row'
      }
      //选择行样式
      for (let i = 0; i < this.multipleSelection.length; i++) {
        if (row === this.multipleSelection[i]) {
          color = 'select-row'
        }
      }
      return color
    },
    // 更改表头样式
    tableHeaderColor({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return 'background-color: #373F52;color: white;font-weight: 700;'
      }
    },

在这里插入图片描述

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值