VUE3+TS+ElementPlus二次封装分页
南江烂柯人 JAVA劝退师

编写组件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

<template>
<div :class="{ hidden: hidden }" class="pagination-container">
<el-pagination
v-model:current-page.sync="paginationData.current"
v-model:page-size="paginationData.size"
:style="{ float: props.isRight ? 'right' : '' }"
:total="total"
:small="small"
:background="background"
:layout="layout"
:page-sizes="pageSizes"
v-bind="$attrs"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
</div>
</template>

<script setup lang="ts">
import {reactive} from 'vue'

const props = defineProps({
// 总数
total: {
required: true,
type: Number,
},
// 页码
current: {
type: Number,
default: 1,
},
// 每页数量
size: {
type: Number,
default: 10,
},
// 每页显示数量
pageSizes: {
type: Array,
default() {
return [10, 20, 50, 100, 200]
},
},
// 显示组件布局
layout: {
type: String,
default: 'total, sizes, prev, pager, next, jumper',
},
small: {
type: Boolean,
default: false,
},
// 是否为分页按钮添加背景色
background: {
type: Boolean,
default: true,
},
autoScroll: {
type: Boolean,
default: true,
},
// 显示与否
hidden: {
type: Boolean,
default: false,
},
// 是否居右
isRight: {
type: Boolean,
default: false,
},
})

const emit = defineEmits(['update:current', 'update:size', 'pagination'])

const paginationData = reactive({
current: props.current,
size: props.size,
})

const handleSizeChange = (val: number) => {
emit('update:size', val)
emit('pagination', paginationData)
}

const handleCurrentChange = (val: number) => {
emit('update:current', val)
emit('pagination', paginationData)
}
</script>

<style scoped lang="scss">
.pagination-container {
overflow-x: auto;
margin-top: 20px;
}

.pagination-container.hidden {
display: none;
}
</style>

使用组件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

<template>
<Pagination
@pagination="handleChangePage"
:total="state.tableData.total"
:current="state.tableData.queryForm.current"
:size="state.tableData.queryForm.size"
/>
</template>

<script setup lang="ts">
import {reactive} from 'vue';

const state = reactive({
tableData: {
total: 0,
data: [],
queryForm: {
current: 1,
size: 10,
},
},
});

// 分页操作
const handleChangePage = (item: { current: number; size: number }) => {
state.tableData.queryForm.current = item.current;
state.tableData.queryForm.size = item.size;
};
</script>
  • 本文标题:VUE3+TS+ElementPlus二次封装分页
  • 本文作者:南江烂柯人
  • 创建时间:2023-02-20 09:46:44
  • 本文链接:https://www.mattjia.com/2023/02/20/web/VUE3+TS+ElementPlus二次封装分页/
  • 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
 评论