原因:element ui 的表格组件默认是不带分页的功能,但是实际工作中表格总是搭配分页功能一起出现的。封装在一起使用方便不需要每次都复制粘贴一大堆。
可以根据自己的需求更改,自己根据设计图定义样式(目前是超过一页才显示分页)
<template>
<div class="common-table-paging">
<el-table
v-loading="loading"
class="common-table"
style="width: 100%"
:data="tableData"
v-bind="$attrs"
v-on="$listeners"
>
<template v-for="(item, index) in columns">
<slot v-if="item.slot" :name="item.slot" />
<el-table-column
v-else-if="!item.children"
:key="index"
:type="item.type"
:prop="item.prop"
:sortable="item.sortable"
:label="item.label"
:width="item.width"
:min-width="item.minWidth"
/>
<el-table-column v-else :key="index" :label="item.label">
<el-table-column
v-for="(childItem, childIndex) in item.children"
:key="'child' + childIndex"
:prop="childItem.prop"
:label="childItem.label"
:width="childItem.width"
:min-width="childItem.minWidth"
:formatter="childItem.formatter"
/>
</el-table-column>
</template>
<template slot="empty">
<slot name="empty" />
</template>
</el-table>
<div v-if="pagination.total > pagination.pageSize" style="height: 60px" />
<el-pagination
style="margin: 19px 0 0"
class="common-pagination"
:current-page="pagination.currentPage"
:page-sizes="[pagination.pageSize]"
:page-size="pagination.pageSize"
layout="slot, prev, pager, next"
:total="pagination.total"
:hide-on-single-page="true"
@current-change="handleCurrentChange"
>
<div v-show="totalPage > 1" class="el-pagination-jump">
<div>第</div>
<div class="el-pagination-editor">
<input v-model="currentPageTemp" autocomplete="off" class="el-inpu-inner"></div>
页/共{{ totalPage }}页
<div
class="jumperBtn"
@click="jumpToPage"
>跳转</div>
</div>
</el-pagination>
</div>
</template>
<script>
export default {
name: 'CommonTable',
props: {
tableData: {
type: Array,
default: () => []
},
columns: {
type: Array,
default: () => []
},
loading: {
type: Boolean,
default: true
},
pagination: {
type: Object,
default: () => ({})
},
size: {
type: String,
default: ''
}
},
data() {
return {
currentPageTemp: 1 // 缓存要跳转的页面
}
},
computed: {
totalPage() {
if (this.pagination.total && this.pagination.pageSize) {
return Math.ceil(this.pagination.total / this.pagination.pageSize)
}
return 0
}
},
watch: {
'pagination.currentPage': function(newVal) {
this.currentPageTemp = newVal
}
},
methods: {
// 跳转到指定的页面
jumpToPage() {
if (Number(this.currentPageTemp) > this.totalPage) {
this.currentPageTemp = this.totalPage
}
if (isNaN(Number(this.currentPageTemp))) {
this.currentPageTemp = 1
}
this.$emit('currentPageChange', Number(this.currentPageTemp))
},
handleCurrentChange(val) {
this.$emit('currentPageChange', val)
}
}
}
</script>
?使用:
<CommonTable
:table-data="tableData"
:pagination="pagination"
:loading="loading"
:columns="columns"
@currentPageChange="handleCurrentPageChange"
>
<el-table-column slot="type" label="测试" min-width="140">
<template slot-scope="scope">
<span v-if="scope.row.type === 1">已开启</span>
<span v-else-if="scope.rowtype === 2">未开启</span>
<span v-else>--</span>
</template>
</el-table-column>
<el-table-column slot="operation" label="操作" min-width="200px">
<template slot-scope="scope">
<el-link
v-hide-env="['cloud']"
:underline="false"
:style="{color: '#46D9FD', marginRight: '18px'}"
@click="handleEdit(scope.$index, scope.row,true)"
>查看</el-link>
<el-link
v-hide-env="['cloud']"
:underline="false"
:style="{color: '#46D9FD', marginRight: '18px'}"
@click="handleEdit(scope.$index, scope.row)"
>编辑</el-link>
</template>
</el-table-column>
</CommonTable>
data:
pagination: {
pageSize: 15,
currentPage: 1,
total: 0,
},
columns: [
{
type: 'index',
label: '序号',
width: '80px'
},
{
label: '测试',
slot: 'type'
},
{
label: '测试列',
prop: 'test-a'
},
{
label: '操作',
slot: 'operation'
}
]
handleCurrentPageChange(val) {
this.pagination.currentPage = val
getDataList()
},
|