Element-UI table表格 自定义样式

Element-UI中关于table表格的样式操作

自定义列的内容:

  • 需求:在表格最后一栏添加操作按钮
    在这里插入图片描述
    通过slot-scope="scope"添加操作按钮,这是专门为我们提供的插槽,方便自定义添加不同的内容。
<el-table-column>
        <template slot-scope="scope">
          <el-button size="mini" type="primary">编辑</el-button>
          <el-button size="mini" type="danger">删除</el-button>
        </template>
</el-table-column>

在这里插入图片描述
scope.$index 获取当前行下标

<el-table-column label="操作" width="160">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" plain @click ="showIndex(scope.$index)">点击显示当前行下标</el-button>
            </template>
</el-table-column>

**根据下标可以对指定某一行进行操作
在这里插入图片描述

通过scope.row.属性名可以获取当前行对应的属性值

<el-table-column label="操作" width="160">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" plain @click = "showName(scope.row.name)">点击获取姓名属性</el-button>
            </template>
 </el-table-column>


可以通过scope.row.属性名和三目运算符给特殊的属性值设定样式

<el-table-column prop="name" :label="langConfig.table.name[lang]" width="200">
            <template slot-scope="scope">
              <div :class="scope.row.name === '王大虎' ? 'specialColor':''">{{scope.row.name}}</div>
            </template>
</el-table-column>

编写specialColor样式,将字体颜色设置为红色

.specialColor{
    color:red;
  }

设置表头样式

  • 需求:将表头样式改为背景色蓝色,字体颜色白色,字重400
    在这里插入图片描述
    header-cell-class-name
  • 说明:表头单元格的 className 的回调方法,也可以使用字符串为所有表头单元格设置一个固定的 className。
  • 类型:Function({row, column, rowIndex, columnIndex})/String
  • 函数形式:将headerStyle方法传递给header-cell-class-name
<el-table 
     :data="tableData[lang]" 
      class="table" 
      stripe 
      border 
      :header-cell-class-name="headerStyle"
 >

编写headerStyle,返回class名称tableStyle

headerStyle ({row, column, rowIndex, columnIndex}) {
        return 'tableStyle'
      }

在style中编写tableStyle样式

<style lang = "scss">
  .tableStyle{
    background-color: #1989fa!important;
    color:#fff;
    font-weight:400;
  }
</style>

字符串形式:直接将tableStyle名称赋值给header-cell-class-name

<el-table
          :data="tableData[lang]"
          class="table"
          stripe
          border
          header-cell-class-name="tableStyle"
        >

header-cell-style

  • 说明:表头单元格的 style 的回调方法,也可以使用一个固定的 Object 为所有表头单元格设置一样的 Style。
  • 类型:Function({row, column, rowIndex, columnIndex})/Object
  • 函数形式:将tableHeaderStyle方法传递给header-cell-style
<el-table 
          :data="tableData[lang]" 
          class="table" 
          stripe 
          border 
          :header-cell-style='tableHeaderStyle'
        >

编写tableHeaderStyle方法,返回样式

tableHeaderStyle ({row, column, rowIndex, columnIndex}) {
        return 'background-color:#1989fa;color:#fff;font-weight:400;'
      }

对象形式:直接在对象中编写样式

<el-table 
          :data="tableData[lang]" 
          class="table" 
          stripe 
          border 
          :header-cell-style="{
            'background-color': '#1989fa',
            'color': '#fff',
            'font-weight': '400'
        }">

header-row-class-name

  • 说明:表头行的className 的回调方法,也可以使用字符串为所有表头行设置一个固定的 className。
  • 类型:Function({row, rowIndex})/String
  • 使用方式与header-cell-class-name类似

注意:header-row-class-name与header-cell-class-name的区别:

header-row-class-name是添加在tr上面的,header-cell-class-name是添加在th上面的。

header-row-class-name:

在这里插入图片描述
所以想让添加在tr上的样式显示,需要关闭element-ui中原本的th的样式,否则会被覆盖!(例如背景色)

在这里插入图片描述
header-cell-class-name:

在这里插入图片描述

header-row-style

  • 说明:表头行的 style 的回调方法,也可以使用一个固定的 Object 为所有表头行设置一样的 Style。
  • 类型:Function({row, rowIndex})/Object
  • 使用方式与header-cell-style类似

设置行样式

需求:将表格中行的背景色设置为浅蓝色

在这里插入图片描述
row-class-name

  • 说明:行的 className 的回调方法,也可以使用字符串为所有行设置一个固定的 className。
  • 类型:Function({row, rowIndex})/String
  • 使用方式与header-cell-class-name类似

row-style

  • 说明:行的 style 的回调方法,也可以使用一个固定的 Object 为所有行设置一样的 Style。
  • 类型:Function({row, rowIndex})/Object
  • 使用方式与header-cell-style类似

函数形式:将tableRowStyle方法传给row-style

<el-table
          :data="tableData[lang]"
          class="table"
          border
          :row-style="tableRowStyle"
        >

编写tableRowStyle方法,返回样式

// 修改table tr行的背景色

tableRowStyle ({ row, rowIndex }) {
     return 'background-color:#ecf5ff'
   }

点击按钮操作当前行

需求:点击操作栏的按钮,切换按钮状态,并且将当前行置灰

在这里插入图片描述

通过slot-scope添加按钮

<el-table-column label="操作" width="160">
            <template slot-scope="scope">
              <el-button size="mini" type="danger" plain v-if = "scope.row.buttonVisible" @click = "changeTable(scope.row.buttonVisible,scope.$index)">禁用该行</el-button>
              <el-button size="mini" type="primary" plain v-else @click = "changeTable(scope.row.buttonVisible,scope.$index)">启用该行</el-button>
            </template>
 </el-table-column>

②在每一个data中添加buttonVisible字段,使用v-if/v-else指令实现按钮的显示与隐藏

{
          date: '2016-05-10',
          name: '王大虎',
          address: '上海市普陀区金沙江路 1518 弄',
          zip: 200333,
          buttonVisible: true
 }

③编写changeTable方法,点击按钮的时候更改buttonVisible的值

changeTable (buttonVisible, index) {
        this.tableData[index].buttonVisible = !buttonVisible
      }

④给el-table添加row-style,并且将tableRowStyle方法传递给row-style

<el-table
          :data="tableData"
          class="table"
          border
          :row-style="tableRowStyle"
        >

⑤编写tableRowStyle方法,根据每一行buttonVisible的值设置背景色

// 修改table tr行的背景色
      tableRowStyle ({ row, rowIndex }) {
        if (this.tableData[rowIndex].buttonVisible === false) {
          return 'background-color: rgba(243,243,243,1)'
        }
      }

原文作者:https://www.cnblogs.com/airen123/p/11889062.html

### element-ui Table 表头自定义方法 在element-ui中,`el-table`组件提供了丰富的功能来定制化表格的外观和行为。对于表头部分,可以通过多种方式实现自定义。 #### 方法一:通过具名插槽(Scoped Slot) 可以利用vue.js提供的slot特性来自定义每一列的内容,包括表头。具体来说,在模板内部使用`<template>`标签配合特定的名字作为插槽名称,比如`header`用于指定该栏目的头部内容[^3]: ```html <template> <div class="container"> <el-table :data="tableData" border style="width: 100%"> <!-- 定义名为'customHeader'的插槽 --> <el-table-column prop="date" label="日期" width="180"> <template slot="header" slot-scope="scope"> 我是自定义的日期标题 </template> </el-table-column> ... </el-table> </div> </template> ``` 这种方法允许开发者完全控制表头内的HTML结构以及样式应用。 #### 方法二:多级表头设置 当涉及到更复杂的场景如创建分组或多层嵌套式的表头时,则需借助于`children`属性构建子栏目,并且可以在最外层设定父级别的label值表示整个区域的大标题[^2]: ```html <el-table :data="tableData" style="width: 100%"> <el-table-column label="基本信息"> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> </el-table-column> ... </el-table> ``` 此方案适用于展示具有逻辑关联的数据集合,使得信息层次更加清晰明了。 #### 方法三:动态调整布局 如果希望进一步增强用户体验,还可以考虑加入交互性的设计,例如支持用户手动改变各字段宽度或者重新排列次序等功能。这通常涉及监听某些事件并相应地修改DOM节点的位置或尺寸[^1]。 ```javascript // 假设已经获取到了目标表格实例 const table = document.querySelector('table'); // 添加新的表头元素到现有列表最后面 table.appendChild(newHeaderElement); ``` 上述代码片段展示了如何向现有的表格对象追加额外的表头项,实际项目里可能还需要结合业务逻辑处理更多细节问题。 综上所述,element-ui不仅内置了许多实用的功能帮助快速搭建美观易用的表格界面,同时也给予了足够的灵活性让用户能够根据自己的需求来进行个性化定制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值