一、 設(shè)置行背景顏色
1. 需求描述
后端返回表格數(shù)據(jù),有特定行數(shù)需要用顏色標(biāo)識。類似于以下需求:
2. 解決方式
方式 | 區(qū)別 |
---|---|
:row-class-name=“tableRowClassName” | 已返回類名的形式設(shè)置樣式,代碼整潔,但是會鼠標(biāo)高亮,導(dǎo)致背景顏色失效 |
:cell-style=“cellStyle” | 以返回樣式的形式設(shè)置,無鼠標(biāo)高亮問題 |
2.1 表格代碼
<el-table
:data="tableData"
style="width: 100%"
class="tableStyle"
:row-class-name="tableRowClassName"
:cell-style="cellStyle">
<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>
2.2 :row-class-name=“tableRowClassName” 方式
/**
* @description: 合計(jì)行處理: :row-class-name="tableRowClassName" 方式
* @param {*}row, rowIndex
* @return {*}
*/
tableRowClassName({row, rowIndex}) {
if (rowIndex === 1) {
return 'warning-row';
} else if (rowIndex === 3) {
return 'success-row';
}
return '';
}
2.3 :cell-style="cellStyle"方式
/**
* @description: 合計(jì)行處理: :cell-style="cellStyle"方式
* @param {*}row, rowIndex
* @return {*}
*/
cellStyle({ row, rowIndex }) {
if (rowIndex === 1) {
return 'background: #4D939F !important;color: #fff;'
} else if (rowIndex === 3) {
return 'background: #e6a23c !important;color: #fff;'
}
return '';
}
3. 樣式設(shè)置
3.1 row-class-name方式的樣式
<style lang='scss' scoped>
/deep/ .el-table .warning-row td {
background: #4D939F !important;
color: #fff;
}
/deep/ .el-table .fixedRow td {
background: #4D939F !important;
color: #fff;
position: sticky;
bottom: 0;
width: 100%;
}
<style>
4. 表頭設(shè)置顏色
4.1 第一種直接設(shè)置文章來源:http://www.zghlxwxcb.cn/news/detail-614892.html
<style>
.el-table th.red {
background-color: #FBBFBC;
color: #fff;
}
.el-table th.green {
background-color: #FEDDB6;
color: #fff;
}
</style>
4.2 第二種 設(shè)置類名 避免樣式污染 推薦第二種
注意,是.tableStyle.el-table 不是.tableStyle .el-table文章來源地址http://www.zghlxwxcb.cn/news/detail-614892.html
<style>
.tableStyle.el-table th.red {
background-color: #FBBFBC;
color: #fff;
}
.tableStyle.el-table th.green {
background-color: #FEDDB6;
color: #fff;
}
</style>
到了這里,關(guān)于el-table 設(shè)置行背景顏色 鼠標(biāo)移入高亮問題處理的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!