当前位置:   article > 正文

基于Vue 2.x版本Element中Table 表格背景色、表头等样式自定义_element table 表头颜色

element table 表头颜色

工作中有用到对这一块样式的自定义,记录一下,仅供参考!


目录

1、表头背景色的修改

1.1 行内样式修改

1.2 属性方法修改

1.3 表头字体颜色及其他属性修改

2、单元格背景色修改

2.1 行内样式修改

2.2属性方法修改

2.3 插槽方式修改

 3、某行内容过长对默认提示框架进行设置


1、表头背景色的修改

1.1 行内样式修改

核心代码:

<el-table :data="tableData" :header-cell-style="{ background: '#FF69B4 !important' ,color:'#ededed  !important'}" style="width: 100%">

1.2 属性方法修改

核心代码:

  <el-table :data="tableData"  :header-cell-style="rowClass" style="width: 100%">
  1. methods: {
  2. rowClass({row,column, rowIndex, columnIndex }) {
  3. if (rowIndex === 0) {
  4. if(columnIndex===0||columnIndex===1){
  5. return {background:'#98ff72 !important',color:'#ededed !important'};//不用这种写法不生效
  6. }else{
  7. return {background:'yellow !important'};
  8. }
  9. }
  10. return null;
  11. },
  12. }

1.3 表头字体颜色及其他属性修改

由于上面对于字体颜色处理没生效 ,要想字体颜色也自定义,需要加入样式如下:

  1. <style scoped>
  2. /*表格的表头颜色 */
  3. /deep/ .el-table__header-wrapper .cell {
  4. color: red;
  5. font-size: x-large;
  6. font-weight: bold;
  7. }
  8. </style>

2、单元格背景色修改

2.1 行内样式修改

核心代码:

:cell-style="{ background: '#AEFFE9 !important' ,color:'#ededed  !important'}"
<el-table :data="tableData" :cell-style="{ background: '#AEFFE9 !important' ,color:'#ededed  !important'}"  :header-cell-style="{ background: '#FF69B4 !important' ,color:'#ededed  !important'}" style="width: 100%">

2.2属性方法修改

核心代码:

  1. <el-table :data="tableData" :cell-style="cellStyle" :header-cell-style="{ background: '#FF69B4 !important' ,color:'#ededed !important'}" style="width: 100%">
  2. methods: {
  3. cellStyle({row, column, rowIndex, columnIndex}) {
  4. if (column.property === 'name') {
  5. switch (row.name) {
  6. case '张三':
  7. return {background: 'pink', color: '#FFFFFF'};//color未生效
  8. break;
  9. case '李四':
  10. return {background: '#98ff72', color: '#FFFFFF'};
  11. break;
  12. }
  13. }
  14. },
  15. }

2.3 插槽方式修改

上面方式对字体颜色不生效,可以用下面的方式处理 或者 某列加<span>对字体样式进行设置,

可以修改背景色、字体颜色、字体加粗、字号大小等,视需求而定。

 核心代码:

  1. <el-table-column prop="address" label="地址">
  2. <template slot-scope="scope">
  3. <div :style="{ background:'#4CFCFF !important',color: scope.row.address ? 'red' : 'black' ,fontWeight:'bold', fontFamily:'华文行楷'}">
  4. {{ scope.row.address }}
  5. </div>
  6. </template>
  7. </el-table-column>

 3、某行内容过长对默认提示框架进行设置

原始效果:

设置后效果:

核心代码:利用插槽对单元格提示进行改造

  1. <el-table-column prop="address" label="地址" show-overflow-tooltip>
  2. <!-- <template slot-scope="scope">
  3. <div :style="{ background:'#4CFCFF !important',color: scope.row.address ? 'red' : 'black' ,fontWeight:'bold', fontFamily:'华文行楷'}">
  4. {{ scope.row.address }}
  5. </div>
  6. </template>-->
  7. <template slot-scope="scope">
  8. <el-tooltip class="item" effect="dark" placement="top">
  9. <template slot="content">
  10. <textarea style="width:300px;height: 120px;font-size: 12px;background-color: #303133;color: white">{{ scope.row.address }}</textarea>
  11. </template>
  12. <span style="font-size: large">
  13. <div :style="{ background:'#4CFCFF !important',color: scope.row.address ? 'red' : 'black' ,fontWeight:'bold', fontFamily:'华文行楷'}">
  14. {{scope.row.address.length>50?scope.row.address.substr(0,50)+'...':scope.row.address}}
  15. </div>
  16. </span>
  17. </el-tooltip>
  18. </template>
  19. </el-table-column>

 设置弹框背景色等属性:

 以上罗列了各种情况,实际工作中,这种需要特殊处理的场景应该不多,根据需求使用不同的方式进行处理。


参考了下面的文章,但是其中对字体颜色的处理不生效,于是,加入自己的处理方式。

参考博文:https://blog.csdn.net/Dax1_/article/details/119739781


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

闽ICP备14008679号