# el-table-plus

背景:使用el-table时,总要书写过多的template模板代码,使得页面html代码过多。从应用层看element组件库,虽然api设计的十分灵活,但使用起来十分繁琐。所以需要对el-table进行包装,简化相关配置。

应用层api设计比较好的,推荐antd-design-vue table,el-table-plus api设计就参考了antd-design-vue table组件,底层渲染依然沿用element-ui table组件。同时全属性/事件支持原element table组件,不会破坏原有的api(无侵入);同时支持slot/jsx/h函数三种方式自定义渲染列数据,实现自定义业务逻辑渲染;同时集成常用的pagination组件以及扩展api,帮助便捷处理常用业务。

# 特点

  1. 简单的使用方式,只需要data和column两个属性即可支持
  2. 全继承,element-ui全属性/事件继承
  3. 高扩展,支持slot/jsx/h函数三种方式自定义渲染列数据
  4. 更便捷,扩展scroll事件、pagination组件、fn快捷格式化

# 安装和引入

安装

yarn add @springleo/el-table-plus

引入

该组件依赖element-ui的el-table组件,需要自行引入。

import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
Vue.use(ElementUI);

import ElTablePlus from '@springleo/el-table-plus'
Vue.use(ElTablePlus)

# 基本用法

datacolumns 两个属性

# 属性事件全继承elementui

自动集成el-table 和el-table-column所有属性和事件。el-table属性包括stripe、border等;el-table-column属性包括fixed、align、show-overflow-tooltip等。

更多可看element官方属性和事件列表

# 格式化列

element-ui字段列中有 formatter - Function(row, column, cellValue, index) 属性支持格式化,这里el-table-plus兼容该写法。

同时为简便函数参数,同时提供fn - (cellValue, row, column, index)属性

# 自定义列模板

支持jsx/slot/h函数三种方式的自定义渲染。

熟悉slot语法,可以在scopedSlots.customRender中设置slot名称,然后在template模板中书写slot。

熟悉jsx语法,可直接customRender函数中返回JSX(注意此时需要babel支持,vue-cli3自带),或者直接书写h函数创建最终VNode。

# 远程排序

使用sortable: 'custom',搭配sort-change事件,进行远程排序。

使用default-sort设置默认排序方向。

# 展开行

当行内容过多并且不想显示横向滚动条时,可以使用 Table 展开行功能。

# 自定义表头

跟element一致,支持自定义列的内容以及自定义表头的内容,分别对应customRendercustomTitle。参数顺序和element一致,分别是 { row, column, $index }和 { column, $index }

# scroll事件

# 集成pagination

分页器,参考配置项或 pagination文档,设为 false 时不展示和进行分页

# API

# el-table-plus 属性

支持el-table上所有原有属性,同时扩展以下api。

参数 类型 默认值 说明
loading Boolean false 动效loading
data Array [] 列表数据
columns Array [] column item配置列表,详细见如下columns Attrs
pagination Object 翻页器配置,默认未设置,不显示翻页器。相关api可查看el-pagination
total Number 0 翻页器条数总数

# el-table-plus 事件

支持el-table上所有原有事件,同时扩展以下api。

事件名称 说明 Description
scroll table滚动条事件 e
page-change 翻页器改变事件 { pageSize, currentPage }

# columns 属性

支持el-table-column所有原有属性Scoped Slot,同时扩展以下api:

Attr Type Default Description
label String 列名称
prop String 列数据字段,支持多层对象嵌套,如user.email.prefix
fn Function 格式化列。函数参数(value, row, column, $index)
hidden Boolean 是否隐藏该列。建议是一个computed,使得可以响应式显示隐藏
customRender Function 自定义列数据渲染。函数参数(value, row, column, $index, h),支持jsx和h函数
customTitle Function 自定义列头部渲染。函数参数(column, $index, h),支持jsx和h函数
scopedSlots Object 使用slot方式自定义渲染,替换customRender/customTitle函数。比如:{ customRender: 'slotName1', customTitle: 'slotName2' }