1、Element UI 引入(整体引入)
main.js
import Element from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
2、开始封装 iTable.vue 组件 (骨架)
<template>
<div class="table">
<el-table id="iTable"></el-table>
<el-pagination></el-pagination>
</div>
<template>
3、在页面中引用 iTable 组件,并且给 iTable 组件传值
<template>
<div class="table-page">
<!--region table 表格-->
<i-table
:list="list"
:total="total"
:otherHeight="otherHeight"
:options="options"
:pagination="pagination"
:columns="columns"
:operates="operates"
@handleSizeChange="handleSizeChange"
@handleIndexChange="handleIndexChange"
@handleSelectionChange="handleSelectionChange"
@handleFilter="handleFilter"
@handelAction="handelAction">
</i-table>
<!--endregion-->
</div>
</template>
<script>
import iTable from '../../components/Table/Index'
export default {
components: {iTable},
data () {
return {
total: 0,
list: [],
otherHeight: 208,
columns: [
{
prop: 'id',
label: '编号',
align: 'center',
width: 60
},
{
prop: 'title',
label: '标题',
align: 'center',
width: 400,
formatter: (row, column, cellValue) => {
return `<span style="white-space: nowrap;color: dodgerblue;">${row.title}</span>`
}
},
{
prop: 'state',
label: '状态',
align: 'center',
width: '160',
render: (h, params) => {
return h('el-tag', {
props: {type: params.row.state === 0 ? 'success' : params.row.state === 1 ? 'info' : 'danger'}
}, params.row.state === 0 ? '上架' : params.row.state === 1 ? '下架' : '审核中')
}
},
{
prop: 'author',
label: '作者',
align: 'center',
width: 120
},
{
prop: 'phone',
label: '联系方式',
align: 'center',
width: 160
},
{
prop: 'email',
label: '邮箱',
align: 'center',
width: 240
},
{
prop: 'createDate',
label: '发布时间',
align: 'center',
width: 180,
formatter: (row, column, cellValue) => {
return this.$utils.Common.dateFormat(row.createDate, 'YYYY年MM月DD日 hh:mm')
}
}
],
operates: {
width: 200,
fixed: 'right',
list: [
{
label: '编辑',
type: 'warning',
show: : (index, row) => {
retuen true
},
icon: 'el-icon-edit',
plain: true,
disabled: false,
method: (index, row) => {
this.handleEdit(index, row)
}
},
{
label: '删除',
type: 'danger',
icon: 'el-icon-delete',
show: true,
plain: false,
disabled: (index, row) => {
retuen false
},
method: (index, row) => {
this.handleDel(index, row)
}
}
]
},
pagination: {
pageIndex: 1,
pageSize: 20
},
options: {
stripe: true,
loading: false,
highlightCurrentRow: true,
mutiSelect: true
}
}
},
components: {
expandDom: {
props: {
column: {
required: true
},
row: {
required: true
}
},
render (h) {
return h('div', {}, ([this.column.render(this.row, this.column)]))
}
}
},
mounted () {
},
methods: {
handleSizeChange (pagination) {
this.pagination = pagination
},
handleIndexChange (pagination) {
this.pagination = pagination
},
handleSelectionChange (val) {
console.log('val:', val)
},
handleEdit (index, row) {
console.log(' index:', index)
console.log(' row:', row)
},
handleDel (index, row) {
console.log(' index:', index)
console.log(' row:', row)
}
}
}
</script>
<!--region 封装的分页 table-->
<!--region 封装的分页 table-->
<template>
<div class="table">
<el-table
id="iTable"
v-loading.iTable="options.loading"
:data="list"
:max-height="height"
:stripe="options.stripe"
ref="mutipleTable"
@selection-change="handleSelectionChange">
<!--region 选择框-->
<el-table-column v-if="options.mutiSelect" type="selection" style="width: 55px;">
</el-table-column>
<!--endregion-->
<!--region 数据列-->
<template v-for="(column, index) in columns">
<el-table-column :prop="column.prop"
:label="column.label"
:align="column.align"
:width="column.width">
<template slot-scope="scope">
<template v-if="!column.render">
<template v-if="column.formatter">
<span v-html="column.formatter(scope.row, column)"></span>
</template>
<template v-else>
<span>{{scope.row[column.prop]}}</span>
</template>
</template>
<template v-else>
<expand-dom :column="column" :row="scope.row" :render="column.render" :index="index"></expand-dom>
</template>
</template>
</el-table-column>
</template>
<!--endregion-->
<!--region 按钮操作组-->
<el-table-column ref="fixedColumn" label="操作" align="center" :width="operates.width" :fixed="operates.fixed"
v-if="operates.list.length > 0">
<template slot-scope="scope">
<div class="operate-group">
<template v-for="(btn, key) in operates.list">
<div class="item" v-if="!btn.show||&&btn.show(scope.$index,scope.row)">
<el-button :type="btn.type" size="mini" :icon="btn.icon" :disabled="btn.disabled||&&btn.disabled(scope.$index,scope.row)"
:plain="btn.plain" @click.native.prevent="btn.method(key,scope.row)">{{ btn.label }}
</el-button>
</div>
</template>
</div>
</template>
</el-table-column>
<!--endregion-->
</el-table>
<div style="height:12px"></div>
<!--region 分页-->
<el-pagination v-if="pagination" @size-change="handleSizeChange"
@current-change="handleIndexChange"
:page-size="tableCurrentPagination.pageSize"
:page-sizes="this.tableCurrentPagination.pageArray" :current-page="tableCurrentPagination.pageIndex"
layout="total,sizes, prev, pager, next,jumper"
:total="total"></el-pagination>
<!--endregion-->
</div>
</template>
<!--endregion-->
<script>
const _pageArray = [20, 50, 100]
export default {
props: {
list: {
type: Array,
default: []
},
columns: {
type: Array,
default: []
},
operates: {
type: Object,
default: {}
},
total: {
type: Number,
default: 0
},
pagination: {
type: Object,
default: null
},
otherHeight: {
type: Number,
default: 160
},
options: {
type: Object,
default: {
stripe: false,
loading: false,
highlightCurrentRow: false,
mutiSelect: false
}
}
},
components: {
expandDom: {
functional: true,
props: {
row: Object,
render: Function,
index: Number,
column: {
type: Object,
default: null
}
},
render: (h, ctx) => {
const params = {
row: ctx.props.row,
index: ctx.props.index
}
if (ctx.props.column) params.column = ctx.props.column
return ctx.props.render(h, params)
}
}
},
data () {
return {
pageIndex: 1,
tableCurrentPagination: {},
multipleSelection: []
}
},
created () {},
mounted () {
if (this.pagination && !this.pagination.pageSizes) {
this.pagination.pageArray = _pageArray
}
this.tableCurrentPagination = this.pagination || {
pageSize: this.total,
pageIndex: 1
}
},
computed: {
height () {
return this.$utils.Common.getWidthHeight().height - this.otherHeight
}
},
methods: {
handleSizeChange (size) {
if (this.pagination) {
this.tableCurrentPagination = {
pageIndex: 1,
pageSize: size
}
this.$emit('handleSizeChange', this.tableCurrentPagination)
}
},
handleIndexChange (currnet) {
if (this.pagination) {
this.tableCurrentPagination.pageIndex = currnet
this.$emit('handleIndexChange', this.tableCurrentPagination)
}
},
handleSelectionChange (val) {
this.multipleSelection = val
this.$emit('handleSelectionChange', val)
},
showfilterDataDialog () {
this.$emit('handleFilter')
},
showActionTableDialog () {
this.$emit('handelAction')
}
}
}
</script>
<style lang="less" rel="stylesheet/less">
@import "../../assets/styles/mixins";
.table {
height: 100%;
.el-pagination {
float: right;
margin: 20px;
}
.el-table__header-wrapper, .el-table__fixed-header-wrapper {
thead {
tr {
th {
color: #333333;
}
}
}
}
.el-table-column--selection .cell {
padding: 0;
text-align: center;
}
.el-table__fixed-right {
bottom: 0 !important;
right: 6px !important;
z-index: 1004;
}
.operate-group {
display: flex;
flex-wrap: wrap;
.item {
margin-top: 4px;
margin-bottom: 4px;
display: block;
flex: 0 0 50%;
}
}
.filter-data {
top: e("calc((100% - 100px) / 3)");
background-color: rgba(0, 0, 0, 0.7);
}
.table-action {
top: e("calc((100% - 100px) / 2)");
background-color: rgba(0, 0, 0, 0.7);
}
.fix-right {
position: absolute;
right: 0;
height: 100px;
color: #ffffff;
width: 30px;
display: block;
z-index: 1005;
writing-mode: vertical-rl;
text-align: center;
line-height: 28px;
border-bottom-left-radius: 6px;
border-top-left-radius: 6px;
cursor: pointer;
}
}
</style>