// 二次封装table组件
<template>
<div class="lby_table">
<el-table
:data="dataSource"
:border="border"
v-loading="tableLoading"
@selection-change="handleSelectionChange"
>
<el-table-column
v-if="isSelection"
type="selection"
width="55"
/>
<template v-for="column in columns">
<el-table-column
v-if="column.slot"
:key="column.prop"
:label="column.label"
:prop="column.prop"
:min-width="column.width"
:width="column.width"
>
<template slot-scope="scope">
<slot
:name="column.slot"
:row="scope.row"
:column="column"
></slot>
</template>
</el-table-column>
<el-table-column
v-else
:key="column.prop"
:label="column.label"
:prop="column.prop"
:min-width="column.width"
:width="column.width"
:class-name="column.className"
>
</el-table-column>
</template>
</el-table>
<el-pagination
v-if="pagination"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page.sync="pagination.pageIndex"
:page-sizes="[10, 20, 50, 100]"
:page-size="pagination.pageSize"
:total="total"
layout="total, sizes, prev, pager, next, jumper"
></el-pagination>
</div>
</template>
export default {
name: "CustomTable",
props: {
columns: {
type: Array,
required: true,
default: () => [],
},
dataSource: {
type: Array,
required: true,
default: () => [],
},
pagination: {
type: Object,
default: () => {
return {
pageIndex: 1,
pageSize: 10
};
},
},
total: {
type: Number,
default: 0
},
border: {
type: Boolean,
default: false
},
tableLoading: {
type: Boolean,
default: false
},
isSelection: {
type: Boolean,
default: false
}
},
methods: {
handleSizeChange (pageSize) {
this.$emit("onSizeChange", pageSize);
},
handleCurrentChange (pageIndex) {
this.$emit("onCurrentChange", pageIndex);
},
// 复选框选中
handleSelectionChange (selection) {
this.$emit('onSelection', selection)
}
},
};
<style lang="scss" scoped>
.lby_table {
width: 100%;
border-radius: 4px;
background: #ffffff;
height: 0;
flex: 1;
display: flex;
flex-direction: column;
::v-deep .el-table {
height: calc(100% - 52px);
.el-table__body-wrapper {
height: calc(100% - 44px);
overflow-y: auto;
}
thead {
height: 40px;
border-radius: 4px;
font-weight: 400;
color: rgba(28, 31, 35, 0.6);
tr {
th {
border-bottom: 0;
font-size: 14px;
color: rgba(28, 31, 35, 0.6);
font-weight: 400;
background: #ffffff;
}
}
}
tbody {
th {
border-bottom: 0;
font-size: 14px;
color: rgba(28, 31, 35, 0.8);
font-weight: 400;
}
.el-table__cell {
border-bottom: 0;
}
.abnormal {
color: rgba(227, 79, 72, 1);
}
}
.cell {
line-height: 24px;
padding-left: 16px;
}
}
::v-deep .el-pagination {
padding: 12px 32px;
text-align: right;
box-shadow: 0px 4px 14px 0px rgba(0, 0, 0, 0.1);
}
}
</style>
// 调用
<template>
<CustomTable
:total='total'
:columns='columns'
:pagination='page'
:isSelection='true'
:dataSource='dataSource'
:tableLoading="tableLoading"
@onSelection='handleSelection'
@onSizeChange='handleSizeChange'
@onCurrentChange='handleCurrentChange'
>
<template v-slot:userCount="{ row }">
<div>{{row.userCount}}人</div>
</template>
<template v-slot:status="{ row }">
<dict-tag
:options="options"
:value="row.status"
/>
</template>
<template v-slot:operate="{ row }">
<el-button
size="mini"
type="text"
icon="el-icon-view"
@click="handleView(row)"
>查看</el-button>
<el-button
size="mini"
type="text"
icon="el-icon-edit"
@click="handleUpdate(row)"
>编辑</el-button>
<el-button
size="mini"
type="text"
icon="el-icon-delete"
@click="handleDelete(row)"
>删除</el-button>
</template>
</CustomTable>
</template>
import { columns } from './config.js'
export default {
// ........
data () {
// 表头
columns,
total: 0,
page: {
pageIndex: 1,
pageSize: 10,
}
dataSource: [],
tableLoading: true,
},
},
mothods: {
handleSizeChange (pageSize) {
this.page.pageSize = pageSize
// 请求列表
this.xxx()
},
handleCurrentChange (pageIndex) {
this.page.pageIndex = pageIndex
// 请求列表
this.xxx()
},
// 省略............................
}
}
// config.js 配置文件
export const columns = [
{
prop: 'userName',
label: '用户名称',
},
{
prop: 'userCount',
label: '人员数量',
slot: 'userCount',
},
{
prop: 'status',
label: '状态',
slot: 'status',
},
{
prop: 'createTime',
label: '创建时间'
},
{
prop: 'operate',
label: '操作',
slot: 'operate'
}
]
基于element-ui table组件二次封装
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 搜索了一下网上关于如何在storybook中引入基于element-ui二次封装后的组件的资料几乎没用或者比较老旧...
- 安装依赖 yarn global add @vue/cli vue create qiyun-el-ui vue ...
- 麻烦点 在使用element-ui的el-table组件时,需要手工写入大量el-table-column,费时费...
- Table组件 因为正常需求来说表格都会带翻页,所以我就直接在一个组件里面做好了 JS 和 CSS 使用组件方法 ...
- 使用vue+elementui封装table。组件中提供了含有分页的表格,支持filter,支持render,支持...