Skip to content

Commit edbfd5c

Browse files
Merge pull request #261 from fratzinger/patch-3
Update FeathersVuexFind.ts - pagination
2 parents 240af6d + 69051a6 commit edbfd5c

File tree

2 files changed

+78
-5
lines changed

2 files changed

+78
-5
lines changed

docs/components.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,65 @@ export default {
315315
</script>
316316
```
317317

318+
### server-side pagination
319+
320+
When you want to use server-side pagination you need to pass the ids from the server to vuex. It can be done by a combination of `query`, `fetchQuery` and `editScope` as described below. The `fetchQuery`-prop is only computed after items from the server arrived. The ids for the `find` getter as well as the total amount of available values `total` are extracted by the `edit-scope` function and stored in `data`:
321+
322+
```html
323+
<template>
324+
<FeathersVuexFind
325+
:service="service"
326+
:query="internalQuery"
327+
:fetch-query="fetchQuery"
328+
:edit-scope="getPaginationInfo"
329+
>
330+
<div slot-scope="{ items: todos }">
331+
{{todos}}
332+
</div>
333+
</FeathersVuexFind>
334+
</template>
335+
336+
<script>
337+
export default {
338+
data() {
339+
return {
340+
service: 'users',
341+
ids: [],
342+
query: {
343+
isComplete: true
344+
},
345+
total: 0,
346+
limit: 10,
347+
skip: 0
348+
};
349+
},
350+
computed: {
351+
internalQuery() {
352+
const { idField } = this.$store.state[this.service];
353+
return {
354+
[idField]: {
355+
$in: this.ids
356+
}
357+
};
358+
},
359+
fetchQuery() {
360+
return Object.assign({}, this.query, { $limit: this.limit, $skip: this.skip });
361+
}
362+
},
363+
methods: {
364+
getPaginationInfo (scope) {
365+
const { queryInfo, pageInfo } = scope;
366+
367+
this.total = queryInfo.total;
368+
if (pageInfo && pageInfo.ids) {
369+
this.ids = pageInfo.ids;
370+
}
371+
}
372+
}
373+
}
374+
</script>
375+
```
376+
318377
### Query when certain conditions are met
319378

320379
Sometimes you only want to query the API server when certain conditions are met. This example shows how to query the API server when the `userSearch` has as least three characters. This property does not affect the internal `find` getter, so the `items` will still update when the `userSearch` property has fewer than three characters, just no API request will be made. The `isFindPending` attribute is used to indicate when data is being loaded from the server.

src/FeathersVuexFind.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { randomString } from './utils'
1+
import { randomString, getQueryInfo } from './utils'
2+
import _get from 'lodash/get'
23

34
export default {
45
props: {
@@ -50,7 +51,9 @@ export default {
5051
}
5152
},
5253
data: () => ({
53-
isFindPending: false
54+
isFindPending: false,
55+
queryId: null,
56+
pageId: null
5457
}),
5558
computed: {
5659
items() {
@@ -62,9 +65,17 @@ export default {
6265
pagination() {
6366
return this.$store.state[this.service].pagination[this.qid]
6467
},
68+
queryInfo() {
69+
if (this.pagination == null || this.queryId == null) return {}
70+
return _get(this.pagination, `[${this.queryId}]`) || {}
71+
},
72+
pageInfo() {
73+
if (this.pagination == null || this.queryId == null || this.pageId == null) return {}
74+
return _get(this.pagination, `[${this.queryId}][${this.pageId}]`) || {}
75+
},
6576
scope() {
66-
const { items, isFindPending, pagination } = this
67-
const defaultScope = { isFindPending, pagination, items }
77+
const { items, isFindPending, pagination, queryInfo, pageInfo } = this
78+
const defaultScope = { isFindPending, pagination, items, queryInfo, pageInfo }
6879

6980
return this.editScope(defaultScope) || defaultScope
7081
}
@@ -85,8 +96,11 @@ export default {
8596

8697
return this.$store
8798
.dispatch(`${this.service}/find`, params)
88-
.then(() => {
99+
.then((response) => {
89100
this.isFindPending = false
101+
const { queryId, pageId } = getQueryInfo(params, response)
102+
this.queryId = queryId
103+
this.pageId = pageId
90104
})
91105
}
92106
}

0 commit comments

Comments
 (0)