Element UI踩坑记 —— el-table中 formatter 的属性居然失效了

姜玉泽
2023-12-01

一、问题引入

今天实习要完成一个数据表格的小案例,要求是能根据数据的严重程度,在页面上显示不同的样式。

我是使用element-ui中的表格组件对数据进行处理,在处理的过程中就掉进坑里。el-table 组件结合插槽和数据格式化(formatter)时,是数据格式化是不会生效的。

解决方法:只能自己封装el-tableformatter方法

二、案例

<template>
  <div class="app">
    <el-card class="box-card" shadow="always">
      <div slot="header">
        <span class="title">异常警告信息</span>
      </div>
      <el-table :data="tableData" style="width: 100%" size="small" height="275" :fit="false">
      
        <el-table-column prop="id" label="序号"> </el-table-column>
        
        <el-table-column prop="level" label="警告级别"> 
           <!--
               插槽 
               scope 代表的是 tableData 数据
               scope.row 代表这一行的数据         
           -->
          <template slot-scope="scope">
            <div class="cell" v-html="formatter(scope.row.level)"></div>
          </template>
        </el-table-column>
        
        <!--:show-overflow-tooltip='true' 溢出隐藏 -->
        <el-table-column prop="info" label="信息" width="78" :show-overflow-tooltip='true'> </el-table-column>
         
        <el-table-column prop="time" label="时间" width="168">
        
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>
<script>
export default {
  name: "chartnine",
  data() {
    return {
      tableData: [
        {
          id: 1,
          level: "严重",
          info: "中病毒啦啦啦啦!!!",
          time: "2022-01-13 19:54:30",
        },
        {
          id: 2,
          level: "严重",
          info: "Wift断啦!!!",
          time: "2022-01-13 19:54:30",
        },
        {
          id: 3,
          level: "重要",
          info: "Wift断啦!!!",
          time: "2022-01-13 19:54:30",
        },
        {
          id: 4,
          level: "重要",
          info: "Wift断啦!!!",
          time: "2022-01-13 19:54:30",
        },
        {
          id: 5,
          level: "严重",
          info: "Wift断啦!!!",
          time: "2022-01-13 19:54:30",
        },
        {
          id: 6,
          level: "严重",
          info: "Wift断啦!!!",
          time: "2022-01-13 19:54:30",
        },
        {
          id: 7,
          level: "重要",
          info: "Wift断啦!!!",
          time: "2022-01-13 19:54:30",
        },
        {
          id: 8,
          level: "普通",
          info: "Wift断啦!!!",
          time: "2022-01-13 19:54:30",
        },
        {
          id: 9,
          level: "重要",
          info: "Wift断啦!!!",
          time: "2022-01-13 19:54:30",
        },
        {
          id: 10,
          level: "严重",
          info: "Wift断啦!!!",
          time: "2022-01-13 19:54:30",
        },
      ],
    };
  },
  methods: {
    // 通过重要程度设置相关样式
    formatter(content) {
      if(content == "重要") {
         return `<span style="color: rgb(37, 87, 150);"> ${content} </span> `
      } else if (content == "严重") {
        return `<span style="color: rgb(246, 23, 38);"> ${content} </span>`
      } else {
        return `<span> ${content} </span>`;
      }
     
    }
  }
};
</script>
 类似资料: