表格效果
组件的创建
<template>
<div id="Wrap">
<!-- 表格 -->
<el-table
:data="tableData"
:border="hasBorder"
:header-cell-style="{ background: '#DDDDDD', color: '#606266' }"
>
<!-- 序号 -->
<el-table-column
v-if="hasIndex"
type="index"
label="序号"
header-align="center"
align="center"
width="80"
>
</el-table-column>
<!-- 其他 -->
<template v-for="item in tableColumnOptions">
<!-- 插槽列 -->
<el-table-column
v-if="item.columnType"
:key="item.label"
:prop="item.prop"
:label="item.label"
:width="item.width"
:header-align="item.headerAlign || 'center'"
:align="item.align || 'center'"
>
<template slot-scope="{ row }">
<slot :name="item.soltName" :data="row"></slot>
</template>
</el-table-column>
<!-- 非插槽列 -->
<el-table-column
v-else
:key="item.label"
:prop="item.prop"
:label="item.label"
:width="item.width"
:header-align="item.headerAlign || 'center'"
:align="item.align || 'center'"
>
</el-table-column>
</template>
</el-table>
<!-- 分页器 -->
<div class="pagination">
<el-pagination
:hide-on-single-page="isShowPagination"
:page-sizes="[10, 20, 50, 100]"
:current-page="currentPage"
:page-size="pageSize"
:total="tableDataTotal"
layout="total, sizes, prev, pager, next, jumper"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
>
</el-pagination>
</div>
</div>
</template>
<script type="text/ecmascript-6">
export default {
name: 'BaseTable',
props: {
tableData: {
type: Array,
default() {
return []
}
},
tableColumnOptions: {
type: Array,
require: true,
default() {
return []
}
},
tableDataTotal: {
type: Number,
default() {
return 0
}
},
hasIndex: {
type: Boolean,
default() {
return true
}
},
hasBorder: {
type: Boolean,
default() {
return true
}
}
},
data() {
return {
currentPage: 1,
pageSize: 10
}
},
computed: {
isShowPagination() {
const isShow = this.tableDataTotal === 0
return isShow
}
},
methods: {
handleCurrentChange(val) {
const params = {
currentPage: val,
pageSize: this.pageSize
}
this.$emit('update', params)
},
handleSizeChange(val) {
const params = {
currentPage: this.currentPage,
pageSize: val
}
this.$emit('update', params)
}
}
}
</script>
<style lang="scss" scoped>
#Wrap {
width: 100%;
box-sizing: border-box;
padding-left: 10px;
padding-right: 10px;
.pagination {
display: flex;
justify-content: flex-end;
padding: 10px 10px 10px 0;
}
}
</style>
组件的使用 (引入和注册步骤省略没写)
<template>
<div>
<!--
hasIndex :是否显示索引(默认显示)
hasBorder :是否显示边框(默认显示)
tableData :表格数据
tableDataTotal :表格数据总量
tableColumnOptions :表格列项
-->
<table-base
:has-index="false"
:has-border="true"
:table-data="tableData"
:table-data-total="20"
:table-column-options="tableColumnOptions"
@update="handleUpdate"
>
<!-- 类型插槽 -->
<template #type="{ data }">
<el-tag :type="data.type ? 'success' : 'danger'">
{{ data.type ? '通过' : '驳回' }}
</el-tag>
</template>
<!-- 操作插槽 -->
<template #operation="{ data }">
<el-button type="success" size="mini">编辑</el-button>
<el-button type="danger" size="mini" @click="handleDelete(data)">删除</el-button>
</template>
</table-base>
</div>
</template>
<script type="text/ecmascript-6">
import TableBase from '@/components/TableBase'
export default {
name: 'ComponentTable',
components: {
TableBase
},
data() {
return {
tableData: [
{ id: 1233, name: '阿松大', type: 1, desc: '1231345434' },
{ id: 6570, name: '投影机', type: 0, desc: '1234144564' },
{ id: 7897, name: '阿什顿', type: 0, desc: '7895745675' },
{ id: 3969, name: '富贵花', type: 1, desc: '9578567455' },
{ id: 3457, name: '法国海', type: 1, desc: '3465678567' },
{ id: 6570, name: '投影机', type: 0, desc: '1234144564' },
{ id: 7897, name: '阿什顿', type: 0, desc: '7895745675' },
{ id: 3969, name: '富贵花', type: 1, desc: '9578567455' }
],
tableColumnOptions: [
{ label: 'ID', prop: 'id' },
{ label: '名称', prop: 'name' },
{ label: '类型', prop: 'type', columnType: true, soltName: 'type' },
{ label: '描述', prop: 'desc', width: '300' },
{ label: '操作', prop: 'operation', width: '300', columnType: true, soltName: 'operation' }
]
}
},
methods: {
handleUpdate(params) {
console.log(params)
},
handleDelete(item) {
console.log(item)
}
}
}
</script>
<style lang="sass" scoped></style>
如有错误,欢迎批评指正