elementUI-table利用index给前三设置不同的颜色及样式

9 篇文章 0 订阅

vue组件部分
使用elementUI的table,利用index给前三设置不同的颜色及样式
关键要素:scope.$index+1

<template>
  <div class="thematicResources">
      <el-table ref="singleTable" :data="deptData" highlight-current-row :show-header="false" style="width: 100%">
        <el-table-column type="index" width="50"> 
            <template slot-scope="scope">
                <span v-if="scope.$index+1 ==1" class="no1">{{scope.$index+1}}</span>
                <span v-else-if="scope.$index+1 ==2" class="no2">{{scope.$index+1}} </span>
                <span v-else-if="scope.$index+1 ==3" class="no3">{{scope.$index+1}} </span>
                <span v-else class="no">{{scope.$index+1}} </span>
            </template>
        </el-table-column>
        <el-table-column prop="name" width="200" label="部门" >  </el-table-column>
        <el-table-column  prop="value" label="数量"> 
            <template scope="scope">
                <span v-if="scope.$index+1 ==1" style="color:#FF545B">{{ scope.row.value }}</span>
                <span v-else-if="scope.$index+1 ==2" style="color: #FCBC24">{{ scope.row.value }}</span>
                <span v-else-if="scope.$index+1 ==3" style="color: #0AF9D8">{{ scope.row.value }}</span>
                <span v-else style="color: #36BAFF">{{ scope.row.value }}</span>
          </template>
        </el-table-column>
      </el-table>
  </div>
</template>

js部分

<script>
export default {
  components: {},
  data() {
    return {
      deptData: [],
    };
  },
  computed: {
  },
  mounted() {
    this.initTableData()
  },
  beforeDestroy() {},
  methods: {
    //数组排序
    sortData(arr){
      for(let i=0;i<arr.length-1;i++){
        for(let j=0;j<arr.length-1-i;j++){
          if(arr[j].value < arr[j+1].value){
            var temp=arr[j];
            arr[j]=arr[j+1];
            arr[j+1]=temp;
          }
        }
      }
      return arr
    },
    //获取表格数据
    initTableData(){
      this.deptData=[];
        let alldata=this.sortData(ThematicResourcesConfig.departmentalData);
        //引入数据文件,ThematicResourcesConfig.departmentalData本地数据
        console.log(alldata)
        for(let i=0;i<alldata.length;i++){
          this.deptData.push(alldata[i])
          if(i==9){//top10
            break
          }
        }
    },
  }
}
</script>

css部分
yellow-bg.png这些即为不同的背景图片

.no1{
    display: inline-block;
    text-align: center;
    background-image: url("../../../../../static/img/lz-data-monitor/red-bg.png");
    background-repeat: no-repeat;
    background-size: 100% 100%;
    width: 20px;
    height: 20px;
    line-height: 20px;
  }
  .no2{
    display: inline-block;
    text-align: center;
    background-image: url("../../../../../static/img/lz-data-monitor/yellow-bg.png");
    background-repeat: no-repeat;
    background-size: 100% 100%;
    width: 20px;
    height: 20px;
    line-height: 20px;
  }
  .no3{
    display: inline-block;
    text-align: center;
    background-image: url("../../../../../static/img/lz-data-monitor/green-bg.png");
    background-repeat: no-repeat;
    background-size: 100% 100%;
    width: 20px;
    height: 20px;
    line-height: 20px;
  }
  .no{
    display: inline-block;
    text-align: center;
    background-color: #007EFF;
    border-radius: 5px;
    width: 20px;
    height: 20px;
    line-height: 20px;
  }

数据文件部分,可以从后台接口获取,此处为了测试制造的静态数据

var ThematicResourcesConfig = ThematicResourcesConfig || {};
(function () {
  var ThematicResourcesConfigColor = ["#8e71c7", "#13CE66", "#F36868", "#8e71c7", "#13CE66", "#F36868"]
  ThematicResourcesConfig = {
    departmentalData: [{
        value: 14,
        name: "住建委"
      },
      {
        value: 14,
        name: "卫计委"
      },
      {
        value: 16,
        name: "园林局"
      },
      {
        value: 639,
        name: "自然资源与规划局"
      }, {
        value: 25,
        name: "地震局"
      },
      {
        value: 39,
        name: "统计局"
      },
      {
        value: 13,
        name: "卫生局"
      },
      {
        value: 14,
        name: "教育局"
      }, {
        value: 20,
        name: "交通局"
      },
      {
        value: 13,
        name: "旅游局"
      },
      {
        value: 3,
        name: "文物局"
      },
      {
        value: 5,
        name: "公安局"
      },
      {
        value: 107,
        name: "执法局"
      },
      {
        value: 24,
        name: "环保局"
      },
      {
        value: 89,
        name: "发改委"
      }
    ],
    
  }
 
})()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值