Skip to content

Commit b9eedb8

Browse files
committed
feature: add pagination component (#1213)
1 parent a67b455 commit b9eedb8

File tree

5 files changed

+158
-38
lines changed

5 files changed

+158
-38
lines changed

src/components/Pagination/index.vue

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<template>
2+
<div :class="{'hidden':hidden}" class="pagination-container">
3+
<el-pagination
4+
:background="background"
5+
:current-page.sync="currentPage"
6+
:page-size.sync="pageSize"
7+
:layout="layout"
8+
:total="total"
9+
v-bind="$attrs"
10+
@size-change="handleSizeChange"
11+
@current-change="handleCurrentChange"/>
12+
</div>
13+
</template>
14+
15+
<script>
16+
import { scrollTo } from '@/utils/scrollTo'
17+
18+
export default {
19+
name: 'Pagination',
20+
props: {
21+
total: {
22+
required: true,
23+
type: Number
24+
},
25+
page: {
26+
type: Number,
27+
default: 1
28+
},
29+
limit: {
30+
type: Number,
31+
default: 20
32+
},
33+
pageSizes: {
34+
type: Array,
35+
default() {
36+
return [10, 20, 30, 50]
37+
}
38+
},
39+
layout: {
40+
type: String,
41+
default: 'total, sizes, prev, pager, next, jumper'
42+
},
43+
background: {
44+
type: Boolean,
45+
default: true
46+
},
47+
autoScroll: {
48+
type: Boolean,
49+
default: true
50+
},
51+
hidden: {
52+
type: Boolean,
53+
default: false
54+
}
55+
},
56+
computed: {
57+
currentPage: {
58+
get() {
59+
return this.page
60+
},
61+
set(val) {
62+
this.$emit('update:page', val)
63+
}
64+
},
65+
pageSize: {
66+
get() {
67+
return this.limit
68+
},
69+
set(val) {
70+
this.$emit('update:limit', val)
71+
}
72+
}
73+
},
74+
methods: {
75+
handleSizeChange(val) {
76+
this.$emit('pagination', { page: this.currentPage, limit: val })
77+
if (this.autoScroll) {
78+
scrollTo(0, 800)
79+
}
80+
},
81+
handleCurrentChange(val) {
82+
this.$emit('pagination', { page: val, limit: this.pageSize })
83+
if (this.autoScroll) {
84+
scrollTo(0, 800)
85+
}
86+
}
87+
}
88+
}
89+
</script>
90+
91+
<style scoped>
92+
.pagination-container {
93+
background: #fff;
94+
padding: 32px 16px;
95+
}
96+
.pagination-container.hidden {
97+
display: none;
98+
}
99+
</style>

src/utils/index.js

-11
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,6 @@ export function objectMerge(target, source) {
164164
return target
165165
}
166166

167-
export function scrollTo(element, to, duration) {
168-
if (duration <= 0) return
169-
const difference = to - element.scrollTop
170-
const perTick = (difference / duration) * 10
171-
setTimeout(() => {
172-
element.scrollTop = element.scrollTop + perTick
173-
if (element.scrollTop === to) return
174-
scrollTo(element, to, duration - 10)
175-
}, 10)
176-
}
177-
178167
export function toggleClass(element, className) {
179168
if (!element || !className) {
180169
return

src/utils/scrollTo.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
Math.easeInOutQuad = function(t, b, c, d) {
2+
t /= d / 2
3+
if (t < 1) {
4+
return c / 2 * t * t + b
5+
}
6+
t--
7+
return -c / 2 * (t * (t - 2) - 1) + b
8+
}
9+
10+
// requestAnimationFrame for Smart Animating http://goo.gl/sx5sts
11+
var requestAnimFrame = (function() {
12+
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function(callback) { window.setTimeout(callback, 1000 / 60) }
13+
})()
14+
15+
// because it's so fucking difficult to detect the scrolling element, just move them all
16+
function move(amount) {
17+
document.documentElement.scrollTop = amount
18+
document.body.parentNode.scrollTop = amount
19+
document.body.scrollTop = amount
20+
}
21+
22+
function position() {
23+
return document.documentElement.scrollTop || document.body.parentNode.scrollTop || document.body.scrollTop
24+
}
25+
26+
export function scrollTo(to, duration, callback) {
27+
const start = position()
28+
const change = to - start
29+
const increment = 20
30+
let currentTime = 0
31+
duration = (typeof (duration) === 'undefined') ? 500 : duration
32+
var animateScroll = function() {
33+
// increment the time
34+
currentTime += increment
35+
// find the value with the quadratic in-out easing function
36+
var val = Math.easeInOutQuad(currentTime, start, change, duration)
37+
// move the document.body
38+
move(val)
39+
// do the animation unless its over
40+
if (currentTime < duration) {
41+
requestAnimFrame(animateScroll)
42+
} else {
43+
if (callback && typeof (callback) === 'function') {
44+
// the animation is done so lets callback
45+
callback()
46+
}
47+
}
48+
}
49+
animateScroll()
50+
}

src/views/example/list.vue

+3-11
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,18 @@
5050
</el-table-column>
5151
</el-table>
5252

53-
<div class="pagination-container">
54-
<el-pagination
55-
:current-page="listQuery.page"
56-
:page-sizes="[10,20,30, 50]"
57-
:page-size="listQuery.limit"
58-
:total="total"
59-
background
60-
layout="total, sizes, prev, pager, next, jumper"
61-
@size-change="handleSizeChange"
62-
@current-change="handleCurrentChange"/>
63-
</div>
53+
<pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
6454

6555
</div>
6656
</template>
6757

6858
<script>
6959
import { fetchList } from '@/api/article'
60+
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
7061
7162
export default {
7263
name: 'ArticleList',
64+
components: { Pagination },
7365
filters: {
7466
statusFilter(status) {
7567
const statusMap = {

src/views/table/complexTable.vue

+6-16
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@
8080
</el-table-column>
8181
</el-table>
8282

83-
<div class="pagination-container">
84-
<el-pagination v-show="total>0" :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" :total="total" background layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
85-
</div>
83+
<pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
8684

8785
<el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
8886
<el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="70px" style="width: 400px; margin-left:50px;">
@@ -130,8 +128,9 @@
130128

131129
<script>
132130
import { fetchList, fetchPv, createArticle, updateArticle } from '@/api/article'
133-
import waves from '@/directive/waves' // 水波纹指令
131+
import waves from '@/directive/waves' // Waves directive
134132
import { parseTime } from '@/utils'
133+
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
135134
136135
const calendarTypeOptions = [
137136
{ key: 'CN', display_name: 'China' },
@@ -148,9 +147,8 @@ const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
148147
149148
export default {
150149
name: 'ComplexTable',
151-
directives: {
152-
waves
153-
},
150+
components: { Pagination },
151+
directives: { waves },
154152
filters: {
155153
statusFilter(status) {
156154
const statusMap = {
@@ -168,7 +166,7 @@ export default {
168166
return {
169167
tableKey: 0,
170168
list: null,
171-
total: null,
169+
total: 0,
172170
listLoading: true,
173171
listQuery: {
174172
page: 1,
@@ -228,14 +226,6 @@ export default {
228226
this.listQuery.page = 1
229227
this.getList()
230228
},
231-
handleSizeChange(val) {
232-
this.listQuery.limit = val
233-
this.getList()
234-
},
235-
handleCurrentChange(val) {
236-
this.listQuery.page = val
237-
this.getList()
238-
},
239229
handleModifyStatus(row, status) {
240230
this.$message({
241231
message: '操作成功',

0 commit comments

Comments
 (0)