表格的自定义模板和展开行
1、自定义模板:
(1)在<el-table-column>
标记对内部使用<template>
标记对。
(2)在自定义模板中使用 作用域插槽:
//重点:可以实现展开行,sc功能强大,可以获取任意行和列的数据
<el-table-column>
<template slot-scope=“sc”>
//sc.row - 任意行的所有列分量
//sc.column - 本列的列属性
//sc.$index - 任意行的索引值
</template>
</el-table-column>
例:在表格中添加一列用来显示出生年份。
{{new Date().getFullYear()-sc.row.age}}
2、展开行:
(1)新增一列并设置type属性取值为expand。
(2)<el-table>
的default-expand-all属性:设置是否所有行默认均
处于展开状态。
(3)可以在具备type=“expand”的列内部使用<template>
。
(4)如何指定特定行默认处于展开状态:
A .为<el-table>
设置row-key属性:为表格数据设置一个主关
键字,主关键字的取值不能重复。
该属性的取值是一个函数。
<el-table :row-key=“rowKey”></el-table>
rowKey(row){ return row.no;} //将no属性作为主关键字
B .使用`<el-table>`的expand-row-keys属性:取值为数组。
数组元素的取值时row-key属性所设置的主关键字的取值。
<el-table :row-key=“rowKey”
:expand-row-keys=“[‘10001’,‘10006’]”>
(5)<el-table>
的expand-change事件:
当行的展开状态发生变化时触发该事件。
3、<el-table-column>
的type属性:
取值:selection - 出现选中列的复选框。
expand - 出现展开列的箭头。
index - 显示每一行的索引值。
当type=“index”时,可以设置<el-table-column>
的index属性。
(1)index属性绑定一个数值,则索引值从该数值开始计数。
(2)index属性绑定一个函数,则索引值按照函数的返回值公
式进行计算。
<el-table-column type=“index” :index=“getNewIndex”>
getNewIndex(index){
//index - 从0开始的索引值
return (index+1)*2; //在表格中显示偶数索引值
}