mirror of
https://github.com/typicode/json-server.git
synced 2025-07-29 21:23:41 +08:00
Update pagination (#346)
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
module.exports = {
|
||||
toNative: toNative
|
||||
toNative: toNative,
|
||||
getPage: getPage
|
||||
}
|
||||
|
||||
// Turns string to native.
|
||||
@ -22,3 +23,30 @@ function toNative (value) {
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
function getPage (array, page, perPage) {
|
||||
var obj = {}
|
||||
var start = (page - 1) * perPage
|
||||
var end = page * perPage
|
||||
|
||||
obj.items = array.slice(start, end)
|
||||
if (obj.items.length === 0) {
|
||||
return obj
|
||||
}
|
||||
|
||||
if (page > 1) {
|
||||
obj.prev = page - 1
|
||||
}
|
||||
|
||||
if (end < array.length) {
|
||||
obj.next = page + 1
|
||||
}
|
||||
|
||||
if (obj.items.length !== array.length) {
|
||||
obj.current = page
|
||||
obj.first = 1
|
||||
obj.last = Math.ceil(array.length / perPage)
|
||||
}
|
||||
|
||||
return obj
|
||||
}
|
Reference in New Issue
Block a user