改变el-table样式

这篇文章介绍了如何在Vue.js的El-Table组件中自定义样式和交互行为。包括改变鼠标悬停表格行的背景颜色、固定列的特殊处理、表头背景的修改以及行背景的动态调整。方法涉及到CSS选择器、row-class-name和cell-mouse事件处理。
摘要由CSDN通过智能技术生成

1、改变鼠标进入表格悬浮时颜色

/deep/ .el-table__body tr:hover>td{
  background-color: #39fcff;
}

一般情况下这个方法可以解决问题,如果不想影响所有表格给el-table 一个class名。

2、固定了右列并且表格单独有特殊色处理

a.有使用到特殊行颜色不同的处理,这个挺容易实现的(ps:如有不懂可以看一下官方文档-带状态表格)
/ / html

//html 
<el-table :row-class-name="tableRovClassName"></el-table>

// js
methods: {
    tableRowC1assName({row,rowIndex}){
        if (row.deployStatus ==O){
            return "warning-row"
        }else {
            return '';
        }
    }
}

b.灵活使用ecell-mouse-enter和ecell-mouse-leave(ps:如有不懂可以看一下官方文档-事件处理)

// html
<el-table
: row-class-name="tableRowClassName"
:ce1l-mouse-enter="mouseEnter"
cell-mouse-leave="mouseLeave"></el-table>
// js
methods: {
    mouseEnter(row,column,cell,event){
        官网查方法
    }
}

 //css
.table-hide-color>td{
    background: #fafafa!important;
}
.site-table .el-table_ body tr.hover-row>td{
    background : #fff;
}

3、表头背景修改

使用 :header-cell-style="headerStyle" 

headerStyle({row,column,rowIndex, columnIndex}){
      let cellStyle1;
      let cellStyle2;
      let cellStyle3;
      cellStyle1= "color: #fff;background:#027db4"
      if(columnIndex >= 0 && columnIndex < 14 && rowIndex===0){
        return cellStyle1;
      }
    },

4、修改每表格一行背景

使用  :row-class-name="tableRowClassName"

tableRowClassName({row, rowIndex}) {
          if (rowIndex === 0) {
              return 'warning-row';
          } else if (rowIndex === 1) {
              return 'color: #fff;background:#027db4';
          }
          return '';
      },

//css
/deep/ .el-table .warning-row {
      background-color: #027DB4;
      color: #fff;}

/deep/ .el-table .success-row {
        background: #f0f9eb;
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Element UI的`el-table`组件中,你可以使用多种方法来改变单元格(cell)的样式。以下是一些常见的方法: 1. **内联样式**:可以直接在`<el-table-column>`的`cell-style`属性中设置CSS样式,例如: ```html <el-table-column prop="name" label="姓名" cell-style="background-color: #f2f2f2; color: #333;"> ``` 这将为每一行的该列设置特定的背景颜色和文本颜色。 2. **数据绑定样式**:如果你想要根据数据动态改变样式,可以在`render-cell`函数中获取当前行的数据并应用样式,例如: ```javascript <template> <el-table-column prop="name" label="姓名" :cell-render="customRender" ></el-table-column> </template> <script> export default { methods: { customRender(h, { row, column, $index }) { return h('span', { style: { backgroundColor: row.color }, // 假设color属性存在于row对象 'data-index': $index, }, row.name); }, }, }; </script> ``` 这里,`customRender`函数会获取`color`属性并应用到元素上。 3. **自定义组件**:如果需要更复杂的样式或交互,可以创建一个自定义的组件并在`cell`属性中使用。 4. **CSS类名**:你也可以为单元格添加类名,然后在外部样式表中定义规则。 相关问题--: 1. 如何在Element UI的`el-table`中使用CSS类名改变单元格样式? 2. 如何在`el-table`中根据数据动态渲染并应用样式? 3. `el-table-column`的`cell-render`属性如何帮助定制单元格样式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值