※前提条件:本文基于 Vue 2.0 创作
prop 属性对应对象中的键名,用来填充数据;label 属性定义表格的列名;width 属性定义表格的列宽。
<template> <el-table :data="tableData" class="data_table"> <el-table-column prop="number" label="编号" width="180"> </el-table-column> <el-table-column prop="name" label="姓名" width="180"> </el-table-column> <el-table-column prop="info" label="描述"> </el-table-column> </el-table> </template>
data () { return { tableData: [{ number: '2016001', name: '小明', info: '这是第一条数据' }, { number: '2016002', name: '小明', info: '这是第二条数据' }, { number: '2016003', name: '小明', info: '这是第三条数据' }, { number: '2016004', name: '小明', info: '这是第四条数据' }, { number: '2016005', name: '小明', info: '这是第五条数据' }] } }
<el-table-column prop="endTime" label="时间" width="90" :formatter="formatDate"> </el-table-column>
methods: { formatDate(row, column) { // 获取单元格数据 let data = row[column.property] if(data == null) { return null } let dt = new Date(data) return dt.getFullYear() + '/' + (dt.getMonth() + 1) + '/' + dt.getDate() }, }
element-ui 中 el-table-column 里面设置 type="expand" 可设置 table 全部展开,或者展开有数据的部分。如果想让全部展开的话添加 efault-expand-all="true"
<el-table :default-expand-all="true"> <el-table-column type="expand"> ...
需要在table绑定两个属性
expand-row-key="expand" 和 row-key="method" method 里面设置需要展开的行
<el-table-column prop="note" label="备注"> <template slot-scope="scope"> <span style="display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-align: left"> {{ scope.row.note }} </span> </template> </el-table-column>
全部展开/折叠
//按钮 <el-button size="mini" @click="btnExpand" style="margin-top: 8px">{{ this.isExpand ? '折叠' : '展开' }}</el-button> <el-table ref="ctab" >... 省略 ... </el-table> data() { return { isExpand: false, } method(){ btnExpand() { this.actionUnfoldFunc(!this.isExpand); }, //btnExpand actionUnfoldFunc(isAss) { this.handleExpand(isAss); }, handleExpand(isAss) { // 此判断是否为展开状态或折叠状态--若展开折叠是同一个按钮则无需传参和加此if if (this.isExpand == isAss) { return; } this.isExpand = !this.isExpand; // this.data为返回的二级数据data this.$nextTick(() => { this.forArr(this.data, this.isExpand); }); }, //handleExpand // 遍历 forArr(arr, isExpand) { arr.forEach((i) => { // toggleRowExpansion见element文档 this.$refs.ctab.toggleRowExpansion(i, isExpand); if (i.children) { this.forArr(i.children, isExpand); } }); }, //forArr }
使用 v-for
简单的
<el-table style="width: 100%" border :data="tableData"> <template v-for="(item,index) in DataList"> <el-table-column :prop="item.column_name" :label="item.column_comment" :key="index" v-if="item.column_name != 'id'"></el-table-column> </template> </el-table>
复杂的,里面带一个 el-input 控件的
<template v-for="(pt, index) in list"> <el-table-column :prop="pt.Name" :label="pt.Name" :key="index"> <template slot-scope="scope"> <el-input v-model="scope.row[pt.Value * 1]" placeholder="请输入" /> </template> </el-table-column> </template>
配合 highlight-current-row 和 setCurrentRow 使用可以将点击的行设置为高亮
<el-table ref="aaTable" @row-click="rowClick" v-loading="loading" highlight-current-row :data="dataList"> </el-table> method:{ rowClick(row){ this.$refs.aaTable.setCurrentRow(row); console.log(row) }, }
<template slot="empty" v-if="slot.row.children.length == 0"> <span>empty</span> </template>
コメント: