使用vue+elementui封装table。
组件中提供了含有分页的表格,支持filter,支持render,支持按钮列(按钮可自定义不展示条件),展开行,合并单元格,支持点击行自定义事件,支持单元格内容超出宽度自动显示...。
使用的方式有两种:
①使用slot插槽自定义表格内容
②使用data和prop将表格内容传递到表格组件
下面给出合并单元格的代码,完整代码见:传送门
// 初始化需要合并的单元格
initMergeRow: function() {
if (this.mergeRowKeys.length === 0) {
return
}
this.mergeRowKeys.forEach((key) => {
const rowAndColumn = []
let pos = 0
for (var i = 0; i < this.data.length; i++) {
if (i === 0) {
// 如果是第一条记录(即索引是0的时候),向数组中加入1
rowAndColumn.push(1)
pos = 0
} else {
if (this.data[i][key] === this.data[i - 1][key]) {
// 如果属性值相等就累加,并且push 0
rowAndColumn[pos] += 1
rowAndColumn.push(0)
} else {
// 不相等push 1
rowAndColumn.push(1)
pos = i
}
}
}
this.multiColumnCount[key] = rowAndColumn
})
},
// 合并单元格函数
mergeRow: function({ row, column, rowIndex, columnIndex }) {
// 行合并
if (this.mergeRowKeys.includes(column.property)) {
if (this.multiColumnCount[column.property][rowIndex]) {
const rowNum = this.multiColumnCount[column.property][rowIndex]
return {
rowspan: rowNum,
colspan: rowNum > 0 ? 1 : 0
}
} else {
return {
rowspan: 0,
colspan: 0
}
}
}
// 列合并
if (this.mergeColIndex && this.mergeColIndex.length > 0) {
var num = 1
// 当前列的index在columnList中对应的index值,如果表格还需要展示复选框,则需要将index 再 减去1
const notOperationIndex = this.columnList[this.columnList.length - 1].operations && this.columnList[this.columnList.length - 1].operations.length > 0 ? this.columnList.length - 2 : this.columnList.length - 1
const endIndex = this.mergeColIndex.length > 1 ? this.mergeColIndex[1] : notOperationIndex
// 如果当前列不在columnList中或者当前列超出了需合并列的index,则不进行任何操作
if (columnIndex < 0 || columnIndex > endIndex) {
return [1, 1]
}
// 如果当前列与前一列值相同,则进行合并,将单元格隐藏
if (columnIndex > 0 && row[this.columnList[columnIndex].prop] === row[this.columnList[columnIndex - 1].prop]) {
return [0, 0]
}
for (let i = columnIndex + 1; i <= endIndex; i++) {
// 若值与前一单元格值相等,则需合并单元格的值++
if (row[this.columnList[i].prop] === row[this.columnList[i - 1].prop]) {
num++
} else {
// 如果值不相等则返回合并单元格范围
return [1, num]
}
}
return [1, num]
}
},
使用方式
table属性
参数 |
说明 |
类型 |
可选值 |
默认值 |
data |
表格当前展示的数据 |
Array |
—— |
[] |
rowKey |
行数据的 Key,用来优化 Table 的渲染;在使用 reserve-selection 功能与显示树形数据时,该属性是必填的。类型为 String 时,支持多层访问:user.info.id,但不支持 user.info[0].id,此种情况请使用 Function。 |
Function(row)/String |
—— |
—— |
columnList |
表格列属性。详细属性见下方columnList 属性详述 |
Array |
—— |
[] |
pageable |
分页信息,若不使用分页组件,可不传此值。具体参数详见下方pageable 属性详述 |
Object |
—— |
null |
height |
表格高度 |
String/Number |
—— |
—— |
showPoper |
当内容超出单元格长度时是否自动截取并展示提示框 |
Boolean |
—— |
true |
reserveSelection |
分页是否保留勾选,在存在单选或者多选列时可生效 |
Boolean |
—— |
true |
poperDelay |
提示信息展示延迟时间ms |
Number |
—— |
1000 |
cellClassName |
单元格的 className 的回调方法,也可以使用字符串为所有单元格设置一个固定的 className。 |
String/Function({row, column, rowIndex, columnIndex}) |
—— |
—— |
headerCellClassName |
表头行的 className 的回调方法,也可以使用字符串为所有表头行设置一个固定的 className。 |
String/Function({row, column, rowIndex, columnIndex}) |
—— |
—— |
dictList |
若改行含有字典code转name的情况可以将字典列表({code: "xxx", label: "YYY"})传入此参数,并在columnList中标明type为dict,可自动进行转换;若此值不传且存在type为dict的列则会从store中获取dictList;若都不存在则直接展示code。 |
Array |
—— |
[] |
tableStyle |
表格样式,将其放在table的style上 |
String |
—— |
null |
mergeRowKeys |
要进行相同数据行合并的字段键值,如第一行和第二行的dict值一样想要进行合并,则可以将dict字段传入此字段['dict'] |
Array |
—— |
[] |
mergeColIndex |
要进行列合并的起始和结束列index,第一个元素为起始index,第二个元素为结束index,若不传第二个元素,则结束为最后一列 |
Array |
—— |
[] |
columnList 属性详述
参数 |
说明 |
类型 |
可选值 |
默认值 |
prop |
该列对应的后端属性 |
String |
—— |
—— |
label |
该列的表头展示名称 |
String |
—— |
—— |
width |
该列所占宽度(如传'200' 则表示列宽200px),不传则按照内容分配宽度 |
String |
—— |
—— |
headerAlign |
该列表头对齐方式 |
String |
center/left/right |
center |
align |
该列内容对齐方式 |
String |
center/left/right |
center |
type |
该列的特殊类型,checkbox为复选框,radio为单选框,index为序号列,expand为展开行标识,money为金额列(会自动展示为千分符格式),dict为字典列(会自动根据code转化为label。字典从dictList中筛选,详情见上方table属性dictList参数),operation为操作列,normal为普通列,默认值 |
String |
checkbox/radio/index/expand/money/dict/operation/normal |
normal |
clickEvent |
点击单元格的回调函数,参数为行数据 |
Function(row) |
—— |
—— |
operaions |
操作列按钮,具体参数见下方operations 属性详述 |
Array |
—— |
—— |
render |
属性值的render渲染函数, 参数为行数据row, 返回需要展示的内容 |
Function(row) |
—— |
—— |
filter |
属性值的过滤器名称,即vue自带过滤器, 参数为行数据row,以及传入的过滤器参数filterParams |
String |
—— |
—— |
filterParams |
过滤器参数 |
Object |
—— |
—— |
pageable 属性详述
参数 |
说明 |
类型 |
可选值 |
默认值 |
page |
页码,从1开始 |
Number |
—— |
1 |
size |
页容量 |
Number |
—— |
20 |
total |
总条数 |
Number |
—— |
0 |
operations 属性详述
参数 |
说明 |
类型 |
可选值 |
默认值 |
type |
按钮类型,more更多类型,默认展示更多图标,鼠标悬停展示更多里面的按钮 |
String |
success/warn/danger/primary/more |
primary |
label |
按钮展示名称 |
String |
—— |
—— |
key |
按钮key值 |
String |
—— |
—— |
notShow |
按钮过滤条件,Object为一个条件:{value: ['XX', 'YY'], prop: 'ZZ'},表示当满足条件该行中属性ZZ存在于value列表中时,不展示该按钮;
Array为多个过滤条件,多条件连接方式为下一个属性notShowJoinType ,如: [{value: ['XX', 'YY'], prop: 'ZZ'}, {value: ['AA'], prop: 'CC'}] 表示当满足条件该行中属性ZZ存在于value列表,并且/或者属性CC存在于['AA', 'BB']中时,不展示该按钮;
类型为Boolean时,按照此Boolean值进行判断;
类型为Function时,按照Function返回值进行判断,返回类型为Boolean |
Object/Array/Boolean/Function |
—— |
—— |
notShowJoinType |
按钮过滤条件连接方式 |
String |
or/and |
and |
buttonIcon |
按钮图标 |
String |
参照element-UI的图标库 |
—— |
trigger |
更多按钮列表展示方式 |
String |
hover/click |
hover |
childOperations |
子按钮,更多按钮展开时展示的按钮列表Array[Object],每个元素属性同operations |
Array |
—— |
—— |
作用域插槽
名称 |
说明 |
expand |
展开行作用域插槽,用于定义行展开之后的显示内容,使用示例见展开行表格示例, 提供参数为该行数据row |
cell |
单元格的作用域插槽,用于定义单元格内容,提供参数为该行数据row, 该列属性prop |
网友评论