用于展示多条结构类似的数据,可对数据进行排序、筛选、对比或其他自定义操作。
当el-table
元素中注入data
对象数组后,在el-table-column
中用prop
属性来对应对象中的键名即可填入数据,用label
属性来定义表格的列名。可以使用width
属性来定义列宽。
<el-table :data="table2data" style="width: 100%;">
<el-table-column prop="id" label="编号" width="180"></el-table-column>
<el-table-column prop="name" label="名称" width="180"></el-table-column>
<el-table-column prop="age" label="年龄"></el-table-column>
</el-table>
table2data: [
{ id: 1, name: "zhangsan",age: 30},
{ id: 2, name: "lisi", age: 40}
]
stripe
属性可以创建带斑马纹的表格。它接受一个Boolean
,默认为false
,设置为true
即为启用。
可以通过指定 Table 组件的 row-class-name
属性来为 Table 中的某一行添加 class,表明该行处于某种状态。
可将表格内容 highlight 显示,方便区分「成功、信息、警告、危险」等内容。
<el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName">
<el-table-column
prop="date"
label="日期"
width="180">
</el-table-column>
<el-table-column
prop="name"
label="姓名"
width="180">
</el-table-column>
<el-table-column
prop="address"
label="地址">
</el-table-column>
</el-table>
<style>
.el-table .warning-row {
background: oldlace;
}
.el-table .success-row {
background: #f0f9eb;
}
</style>
methods: {
tableRowClassName({row, rowIndex}) {
if (rowIndex === 1) {
return 'warning-row';
} else if (rowIndex === 3) {
return 'success-row';
}
return '';
}
},
通过设置max-height
属性为 Table 指定最大高度。此时若表格所需的高度大于最大高度,则会显示一个滚动条。
多级表头:只需要在 el-table-column 里面嵌套 el-table-column,就可以实现多级表头。
单选:Table 组件提供了单选的支持,只需要配置highlight-current-row
属性即可实现单选。之后由current-change
事件来管理选中时触发的事件,它会传入currentRow
,oldCurrentRow
。如果需要显示索引,可以增加一列el-table-column
,设置type
属性为index
即可显示从 1 开始的索引号。
<template>
<div>
<el-table
:data="tableData"
max-height="250"
highlight-current-row
ref="singleTable"
@current-change="handleCurrentChange"
border
style="width: 100%">
<el-table-column
type="selection"
label="编号"
width="50">
</el-table-column>
<el-table-column
fixed
prop="date"
label="日期"
width="150">
</el-table-column>
<el-table-column label="配送信息">
<el-table-column
prop="name"
label="姓名"
width="120">
</el-table-column>
<el-table-column label="地址">
<el-table-column
prop="province"
label="省份"
width="120">
</el-table-column>
<el-table-column
prop="city"
label="市区"
width="120">
</el-table-column>
<el-table-column
prop="address"
label="地址"
width="300">
</el-table-column>
<el-table-column
prop="zip"
label="邮编"
width="120">
</el-table-column>
</el-table-column>
<el-table-column
fixed="right"
label="操作"
width="100">
<template slot-scope="scope">
<el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
<el-button type="text" size="small">编辑</el-button>
</template>
</el-table-column>
</el-table-column>
</el-table>
<div style="margin-top: 20px">
<el-button @click="setCurrent(tableData[1])">选中第二行</el-button>
<el-button @click="setCurrent()">取消选择</el-button>
</div>
<!--<el-table :data="table2data" style="width: 100%;" stripe >
<el-table-column prop="id" label="编号" width="180"></el-table-column>
<el-table-column prop="name" label="名称" width="180"></el-table-column>
<el-table-column prop="age" label="年龄"></el-table-column>
</el-table>-->
</div>
</template>
<style>
.el-table .warning-row {
background: oldlace;
}
.el-table .success-row {
background: #f0f9eb;
}
</style>
<script>
export default {
methods: {
handleClick(row) {
console.log(row);
},
setCurrent(row) {
this.$refs.singleTable.setCurrentRow(row);
console.log(this.$refs.singleTable.setCurrentRow(row));
},
handleCurrentChange(val) {
this.currentRow = val;
console.log(this.currentRow);
}
},
data() {
return {
currentRow: null,
tableData: [{
date: '2016-05-02',
name: '王小虎',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333
}, {
date: '2016-05-04',
name: '王小虎',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1517 弄',
zip: 200333
}, {
date: '2016-05-01',
name: '王小虎',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1519 弄',
zip: 200333
},{
date: '2016-05-02',
name: '王小虎',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333
}, {
date: '2016-05-02',
name: '王小虎',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333
}, {
date: '2016-05-02',
name: '王小虎',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333
}, {
date: '2016-05-02',
name: '王小虎',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333
}, {
date: '2016-05-02',
name: '王小虎',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1518 弄',
zip: 200333
}, {
date: '2016-05-03',
name: '王小虎',
province: '上海',
city: '普陀区',
address: '上海市普陀区金沙江路 1516 弄',
zip: 200333
}]
}
}
}
</script>