欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Element-UI中关于table表格的那些骚操作(小结)

程序员文章站 2022-11-16 08:07:51
最近的项目中使用到element-ui组件库,由于做的是后台管理系统,所以经常需要操作表格,编辑样式的过程中遇到一些问题,官网针对table给出了很多的api,自己可以自定...

最近的项目中使用到element-ui组件库,由于做的是后台管理系统,所以经常需要操作表格,编辑样式的过程中遇到一些问题,官网针对table给出了很多的api,自己可以自定义,基本能满足产品需求,但是没有给出具体的案例,网上的资料也比较简略,这里简单整理下一些常用的操作,如果有类似的功能可以做一个参考。

具体的使用方法还是建议仔细阅读官网-table章节:

https://element.eleme.cn/#/zh-cn/component/table#table-column-scoped-slot

该项目demo已上传github,欢迎大家下载:

# 克隆到本地
git clone git@github.com:hanxueqing/element-table.git

# 安装依赖
npm install

# 开启本地服务器localhost
npm run dev

项目地址:

https://github.com/hanxueqing/element-table

自定义列的内容

需求:在表格最后一栏添加操作按钮

Element-UI中关于table表格的那些骚操作(小结)

通过slot-scope="scope"添加操作按钮,这是专门为我们提供的插槽,方便自定义添加不同的内容。

   <template slot-scope="scope">
    <el-button size="mini" type="primary">编辑</el-button>
    <el-button size="mini" type="danger">删除</el-button>
   </template>
   </el-table-column>

Element-UI中关于table表格的那些骚操作(小结) 

scope.$index 获取当前行下标

添加进来的操作按钮可以通过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>

根据下标可以对指定某一行进行操作

Element-UI中关于table表格的那些骚操作(小结)

scope.row 获取当前属性值

通过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>

点击按钮获得当前行的name属性值

Element-UI中关于table表格的那些骚操作(小结)

可以通过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

Element-UI中关于table表格的那些骚操作(小结)

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:

Element-UI中关于table表格的那些骚操作(小结)

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

Element-UI中关于table表格的那些骚操作(小结)

header-cell-class-name:

Element-UI中关于table表格的那些骚操作(小结)

header-row-style

说明:表头行的 style 的回调方法,也可以使用一个固定的 object 为所有表头行设置一样的 style。

类型:function({row, rowindex})/object

使用方式与header-cell-style类似

设置行样式

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

Element-UI中关于table表格的那些骚操作(小结)

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'
  }

点击按钮操作当前行

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

Element-UI中关于table表格的那些骚操作(小结)

通过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)'
  }
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。