效果:
表格展开行官网使用:
通过设置 type="expand" 和 Scoped slot
可以开启展开行功能,el-table-column
的模板会被渲染成为展开行的内容,展开行可访问的属性与使用自定义列模板时的 Scoped slot
相同。
但是这种方法是有局限性,只是点击箭头展开,点击行没反应,这里做优化点击行也可以进行展开。
关键点(以下属性和事件缺一不可):
:row-key="getRowKeys"
:expand-row-keys="expands"
@row-click="clickRowHandle"
需要默认展开行只需要在expands存入需要展开行的id就可以了
html
<el-table :data="tableData":row-key="getRowKeys":expand-row-keys="expands"@row-click="clickRowHandle"border><el-table-column type="expand"><template slot-scope="scope"><div class="expand-txt"><span>编号:</span> {{ scope.row.code }}</div><div class="expand-txt"><span>批次:</span> {{ scope.row.batch }}</div><div class="expand-txt"><span>名称:</span> {{ scope.row.name }}</div><div class="expand-txt"><span>规格:</span> {{ scope.row.specifications }}</div><div class="expand-txt"><span>参数:</span> {{ scope.row.parameter }}</div></template></el-table-column><el-table-column prop="code"label="编号" width="120px"></el-table-column><el-table-column prop="batch"label="批次"></el-table-column><el-table-column prop="name"label="姓名"></el-table-column><el-table-column prop="specifications"label="规格"></el-table-column><el-table-column prop="parameter"label="参数"></el-table-column></el-table>
script:
<script>
export default {data () {return {tableData: [{id: 1,code: '2016-05-01',batch: 1,name: '王小虎',specifications: 'A',parameter: '上海市普陀区金沙江路 1518 弄'}, {id: 2,code: '2016-05-02',batch: 1,name: '王小虎',specifications: 'A',parameter: '上海市普陀区金沙江路 1517 弄'}, {id: 3,code: '2016-05-03',batch: 1,name: '王小虎',specifications: 'A',parameter: '上海市普陀区金沙江路 1519 弄'}, {id: 4,code: '2016-05-04',batch: 1,name: '王小虎',specifications: 'A',parameter: '上海市普陀区金沙江路 1516 弄'}],// 获取row的key值getRowKeys (row) {// console.log(row);return row.id;},expands: [],}},components: {},mounted () {// 在初始化的时候展开第一行都可以了this.expands.push(this.tableData[0].id);},methods: {clickRowHandle (row) { // , column, event// console.log(row, column, event);if (this.expands.includes(row.id)) {this.expands = this.expands.filter(val => val !== row.id)console.log(1, this.expands);} else {this.expands.push(row.id)console.log(2, this.expands);}}}
}
</script>