浅谈Vue element-ui表格那些实用操作

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

具体的使用方法还是建议仔细阅读官网-table章节:
https://element.eleme.cn/#/zh-CN/component/table#table-column-scoped-slot

自定义列的内容
需求:在表格最后一栏添加操作按钮

通过slot-scope="scope"添加操作按钮,这是专门为我们提供的插槽,方便自定义添加不同的内容。
点击事件其中的参数就是单行的数据

 <template slot-scope="scope">
    <el-button size="mini" @click="edit(scope.row)" type="primary">编辑</el-button>
    <el-button size="mini" @click="del(scope.row)" type="danger">删除</el-button>
   </template>
   </el-table-column>

设置表头样式
需求:将表头样式改为背景色蓝色,字体颜色白色,字重400
header-cell-class-name
说明:表头单元格的 className 的回调方法,也可以使用字符串为所有表头单元格设置一个固定的 className。
类型:Function({row, column, rowIndex, columnIndex})/String
函数形式:将headerStyle方法传递给header-cell-class-name

<el-table 
   :data="tableData" 
   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" 
   class="table" 
   stripe 
   border 
   header-cell-class-name="tableStyle"
  >

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

<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)'
  }
  }
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值