<template> <div id="app"> <el-table :data="tableData" border :header-cell-style="{ background: '#fafafa', color: '#333', fontWeight: '600', fontSize: '14px', }" style="width: 541px" :row-style="TableRowStyle" > <el-table-column prop="name" label="姓名" width="180"> </el-table-column> <el-table-column prop="age" label="年齡" width="180"> </el-table-column> <el-table-column prop="school" label="是否上學" width="180"> </el-table-column> </el-table> </div> </template> <script> export default { name: "app", data() { return { tableData: [ { name: "孫悟空", age: 500, school: "上學中", }, { name: "豬八戒", age: 88, school: "已輟學", }, { name: "沙僧", age: 1000, school: "已輟學", }, { name: "唐僧", age: 99999, school: "上學中", }, ], }; }, methods: { // 當狀態為 已輟學 的狀態,加上背景色 TableRowStyle({ row, rowIndex }) { // 注意,這里返回的是一個對象 let rowBackground = {}; if (row.school.includes("輟學") ) { rowBackground.background = "pink"; return rowBackground; } }, }, }; </script>
還有
cell-style的設置舉例
<el-table :data="auditList" border highlight-current-row style="width: 100%" :cell-style="addClass"> ... </el-teble>
addClass({row,column,rowIndex,columnIndex}) {
var bgColor = '';
if(columnIndex == 2) {
if(row.kkk == 'Yes') {
return 'background:#baecc6';
}
}