今天实习要完成一个数据表格的小案例,要求是能根据数据的严重程度,在页面上显示不同的样式。
我是使用element-ui
中的表格组件对数据进行处理,在处理的过程中就掉进坑里。el-table
组件结合插槽和数据格式化(formatter
)时,是数据格式化是不会生效的。
解决方法:只能自己封装el-table
中formatter
方法
<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="警告级别">
<template slot-scope="scope">
<div class="cell" v-html="formatter(scope.row.level)">div>
template>
el-table-column>
<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 ` ${content} `
} else if (content == "严重") {
return ` ${content} `
} else {
return ` ${content} `;
}
}
}
};
script>