当前位置:   article > 正文

【Vue】在el-table的el-table-column中,如何控制单行、单列、以及根据内容单独设置样式。例如:修改文字颜色、背景颜色_el-table-column 单行

el-table-column 单行

用cell-style表属性来实现。在官网中是这样表述这个属性的。

 

 在el-table中用v-bind绑定此属性。(v-bind的简写是:)

  1. <el-table
  2. :data="options"
  3. :cell-style="cell"
  4. >
  5. <el-table-column prop="id" label="id" width="50px"></el-table-column>
  6. <el-table-column prop="label" label="时间" width="200px"></el-table-column>
  7. </el-table>

data中的options数据为:

  1. data() {
  2. return {
  3. options: [
  4. { id: 1, label: "inner" },
  5. { id: 2, label: "webapi" },
  6. { id: 3, label: "inner-cron" }
  7. ],
  8. };
  9. },

此时页面显示为:

 

 

在methods中声明cellStyle方法。让我们打印出各个参数看一下代表了什么。

  1. cell({ row, column, rowIndex, columnIndex }) {
  2. console.log(row);
  3. console.log(column);
  4. console.log(rowIndex);
  5. console.log(columnIndex);
  6. },

控制台打印如下: 

 

 其实很好理解,row是行,控制台第一行打印的是数组中第一个对象。column是列,是el-table-column。rowIndex是行的索引,columnIndex是列索引。

如果我们想更改第一行的字体颜色是绿色。可以这么写:

  1. cell({ row, column, rowIndex, columnIndex }) {
  2. if(rowIndex === 0){
  3. return "color:green"
  4. }
  5. },

页面效果为:

如果想要第一列的背景颜色是红色。那么:

  1. cell({ row, column, rowIndex, columnIndex }) {
  2. if(columnIndex === 0){
  3. return "background-color : red"
  4. }
  5. if(rowIndex === 0){
  6. return "color:green"
  7. }
  8. },

 页面显示为:

 若想要label为inner-cron的字体加粗。那么:

  1. cell({ row, column, rowIndex, columnIndex }) {
  2. if (columnIndex === 0) {
  3. return "background-color : red";
  4. }
  5. if (rowIndex === 0) {
  6. return "color:green";
  7. }
  8. if (row.label === "inner-cron") {
  9. return "font-weight : bold";
  10. }
  11. },

页面显示为:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/103276
推荐阅读
相关标签
  

闽ICP备14008679号