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