element-ui,table表格根据数据设置箭头和文字颜色


今天朋友发来张ui图是这样的:

 

 

 后面哪一列要根据数据大于或小于0来显示对应文字的颜色,箭头颜色和,箭头指向

于是我就自己做了个demo来实现这个功能

建表格这些就跳过了,直入主题:

首先我们先搞这个箭头,饿了么的table “slot-scope”,这个属性是允许我们在表格前面加上自己想要的东西

     <el-table-column label="同比">
        <template slot-scope="scope">
          <i class="el-icon-top" style="margin-left: 10px"
           ></i>
          <span
            style="margin-left: 10px"
            >{{ scope.row.data }}</span
          >
        </template>
      </el-table-column>

加上去后,我们得到了这个效果:

 

 

 我们看到,这里只有上箭头,下箭头怎么搞呢,用v-if就可以实现了,

我们再把下箭头给加上:

    <el-table-column label="同比">
        <template slot-scope="scope">
          <i class="el-icon-top" 
            style="margin-left: 10px"
           ></i>
          <i class="el-icon-bottom" style="margin-left: 10px"
           ></i>
          <span
            style="margin-left: 10px"
            >{{ scope.row.data }}</span
          >
        </template>
      </el-table-column>

效果如下:

 

 

 这时候我们使用v-if来让他根据数据只显示一个箭头:

   <el-table-column label="同比">
        <template slot-scope="scope">
          <i class="el-icon-top" v-if="scope.row.data > 0" style="margin-left: 10px"
           ></i>
          <i class="el-icon-bottom" v-else-if="scope.row.data < 0" style="margin-left: 10px"
           ></i>
          <span
            style="margin-left: 10px"
            >{{ scope.row.data }}</span
          >
        </template>
      </el-table-column>

效果如下:

 

 

 此时箭头已经可以根据数据作出正确的指向了

加上文字颜色:

   <el-table-column label="同比">
        <template slot-scope="scope">
          <i class="el-icon-top" v-if="scope.row.data > 0"
            style="margin-left: 10px"
            ></i>
          <i class="el-icon-bottom" v-else-if="scope.row.data < 0"
          style="margin-left: 10px"
            ></i>
          <span
            style="margin-left: 10px" class="scop"
            >{{ scope.row.data }}</span
          >
        </template>
      </el-table-column>
<style> .scop{ color: green; } </style>

效果:

   <el-table-column label="同比">
        <template slot-scope="scope">
          <i class="el-icon-top" v-if="scope.row.data > 0"
            style="margin-left: 10px"
            :class="{
              red: scope.row.data > 0
            }"></i>
          <i class="el-icon-bottom" v-else-if="scope.row.data < 0"
          style="margin-left: 10px"
            :class="{
              red: scope.row.data > 0
            }"></i>
          <span
            style="margin-left: 10px"
            :class="{
              red: scope.row.data > 0
            }"
            class="scop"
            >{{ scope.row.data }}</span
          >
        </template>
      </el-table-column>

 

 

 只有绿色的,红色的我们要用到 :class 来解决, 在span 定义 :class  style里定义一个类名为red,大于0的时候用 :class ,小于0的时候用 class

   <el-table-column label="同比">
        <template slot-scope="scope">
          <i class="el-icon-top" v-if="scope.row.data > 0"
            style="margin-left: 10px"
            ></i>
          <i class="el-icon-bottom" v-else-if="scope.row.data < 0"
          style="margin-left: 10px"
            ></i>
          <span
            style="margin-left: 10px" :class="{ red: scope.row.data > 0 }" class="scop"
            >{{ scope.row.data }}</span
          >
        </template>
      </el-table-column>

<style>
.scop{
  color: green;
}
.red {
  color: #f00;
}
</style>
 

效果:

 

 

 这样文字颜色就搞定了

接下来的箭头也是一个道理:

<el-table-column label="同比">
        <template slot-scope="scope">
          <i class="el-icon-top" v-if="scope.row.data > 0" style="margin-left: 10px" :class="{ red: scope.row.data > 0 }"></i>
          <i class="el-icon-bottom" v-else-if="scope.row.data < 0" style="margin-left: 10px" :class="{ red: scope.row.data > 0 }"></i>
          <span
            style="margin-left: 10px"
            :class="{
              red: scope.row.data > 0
            }"
            class="scop"
            >{{ scope.row.data }}</span
          >
        </template>
      </el-table-column>


<style>
.el-icon-bottom{
  color: green;
}
.el-icon-top{
  color: green;
}
.scop{
  color: green;
}
.red {
  color: #f00;
}
</style>
 

效果:

 

 

完整代码:

<template>
  <div>
    <el-table border :data="tableData" style="width: 100%">
      <el-table-column prop="date" label="日期" width="180"> </el-table-column>
      <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
      <el-table-column prop="address" label="地址"> </el-table-column>
      <el-table-column label="同比">
        <template slot-scope="scope">
          <i class="el-icon-top" v-if="scope.row.data > 0"
            style="margin-left: 10px"
            :class="{
              red: scope.row.data > 0
            }"></i>
          <i class="el-icon-bottom" v-else-if="scope.row.data < 0"
          style="margin-left: 10px"
            :class="{
              red: scope.row.data > 0
            }"></i>
          <span
            style="margin-left: 10px"
            :class="{
              red: scope.row.data > 0
            }"
            class="scop"
            >{{ scope.row.data }}</span
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        {
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄',
          data: -1
        },
        {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄',
          data: 6
        },
        {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄',
          data: -5
        },
        {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄',
          data: -2
        }
      ]
    };
  }
};
</script>

<style>
.el-icon-bottom{
  color: green;
}
.el-icon-top{
  color: green;
}
.scop{
  color: green;
}
.red {
  color: #f00;
}
</style>

 

 

纯属小白学习笔记,有不对的还望各位大佬指正!!!


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM