Skip to content
This repository was archived by the owner on Jun 27, 2024. It is now read-only.

Commit 5fc2c49

Browse files
committed
Support for JSON resource pagination
1 parent 5850f24 commit 5fc2c49

File tree

3 files changed

+110
-17
lines changed

3 files changed

+110
-17
lines changed

__tests__/Pagination.spec.js

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ describe('Pagination.vue', () => {
88
const component = mount(Pagination, {
99
propsData: {
1010
meta: {
11-
total: 0
11+
total: 0,
12+
to: 0,
13+
from: 0,
1214
},
1315
}
1416
});
@@ -24,12 +26,68 @@ describe('Pagination.vue', () => {
2426
const component = mount(Pagination, {
2527
propsData: {
2628
meta: {
27-
total: 0
29+
total: 0,
30+
to: 0,
31+
from: 0,
2832
},
2933
}
3034
});
3135

3236
expect(component.html()).toContain("Geen resultaten gevonden");
3337
});
3438

39+
it('works with a json eloquent collection', () => {
40+
const component = mount(Pagination, {
41+
propsData: {
42+
meta: {
43+
current_page: 7,
44+
data: [],
45+
first_page_url: "http://project.test/resource?page=1",
46+
from: 61,
47+
last_page: 10,
48+
last_page_url: "http://project.test/resource?page=10",
49+
links: Array[12],
50+
next_page_url: "http://project.test/resource?page=8",
51+
path: "http://project.test/resource",
52+
per_page: 10,
53+
prev_page_url: "http://project.test/resource?page=6",
54+
to: 70,
55+
total: 0, // for test only
56+
},
57+
}
58+
});
59+
60+
expect(component.vm.previousPageUrl).toContain("http://project.test/resource?page=6");
61+
expect(component.vm.nextPageUrl).toContain("http://project.test/resource?page=8");
62+
});
63+
64+
it('works with a json resource collection', () => {
65+
const component = mount(Pagination, {
66+
propsData: {
67+
meta: {
68+
data: [],
69+
links: {
70+
first: "http://project.test/resource?page=1",
71+
last: "http://project.test/resource?page=10",
72+
next: "http://project.test/resource?page=7",
73+
prev: "http://project.test/resource?page=5",
74+
},
75+
meta: {
76+
current_page: 6,
77+
from: 51,
78+
last_page: 10,
79+
links: [],
80+
path: "http://shop.test/admin/customers",
81+
per_page: 10,
82+
to: 60,
83+
total: 0, // for test only
84+
},
85+
},
86+
}
87+
});
88+
89+
expect(component.vm.previousPageUrl).toContain("http://project.test/resource?page=5");
90+
expect(component.vm.nextPageUrl).toContain("http://project.test/resource?page=7");
91+
});
92+
3593
})

js/Components/Pagination.vue

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,38 @@ const Pagination = {
1111
translations() {
1212
return Pagination.defaultTranslations;
1313
},
14+
15+
pagination() {
16+
if ("total" in this.meta && "to" in this.meta && "from" in this.meta) {
17+
return this.meta;
18+
}
19+
20+
if ("meta" in this.meta) {
21+
return this.meta.meta;
22+
}
23+
24+
return {};
25+
},
26+
27+
previousPageUrl() {
28+
if ("prev_page_url" in this.pagination) {
29+
return this.pagination.prev_page_url;
30+
}
31+
32+
if ("links" in this.meta) {
33+
return this.meta.links.prev;
34+
}
35+
},
36+
37+
nextPageUrl() {
38+
if ("next_page_url" in this.pagination) {
39+
return this.pagination.next_page_url;
40+
}
41+
42+
if ("links" in this.meta) {
43+
return this.meta.links.next;
44+
}
45+
},
1446
},
1547
1648
defaultTranslations: {

js/Tailwind2/Pagination.vue

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,30 @@
33
class="bg-white px-4 py-3 flex items-center justify-between border-t border-gray-200 sm:px-6"
44
v-if="meta"
55
>
6-
<p v-if="meta.total < 1">{{ translations.no_results_found }}</p>
7-
<div v-if="meta.total > 0" class="flex-1 flex justify-between sm:hidden">
6+
<p v-if="pagination.total < 1">{{ translations.no_results_found }}</p>
7+
<div v-if="pagination.total > 0" class="flex-1 flex justify-between sm:hidden">
88
<component
9-
:is="meta.prev_page_url ? 'inertia-link' : 'div'"
10-
:href="meta.prev_page_url"
9+
:is="previousPageUrl ? 'inertia-link' : 'div'"
10+
:href="previousPageUrl"
1111
class="relative inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-md text-gray-700 bg-white hover:text-gray-500"
1212
>{{ translations.previous }}</component>
1313
<component
14-
:is="meta.next_page_url ? 'inertia-link' : 'div'"
15-
:href="meta.next_page_url"
14+
:is="nextPageUrl ? 'inertia-link' : 'div'"
15+
:href="nextPageUrl"
1616
class="ml-3 relative inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-md text-gray-700 bg-white hover:text-gray-500"
1717
>{{ translations.next }}</component>
1818
</div>
19-
<div v-if="meta.total > 0" class="hidden sm:flex-1 sm:flex sm:items-center sm:justify-between">
19+
<div
20+
v-if="pagination.total > 0"
21+
class="hidden sm:flex-1 sm:flex sm:items-center sm:justify-between"
22+
>
2023
<div>
2124
<p class="hidden lg:block text-sm text-gray-700">
22-
<span class="font-medium">{{ meta.from }}</span>
25+
<span class="font-medium">{{ pagination.from }}</span>
2326
{{ translations.to }}
24-
<span class="font-medium">{{ meta.to }}</span>
27+
<span class="font-medium">{{ pagination.to }}</span>
2528
{{ translations.of }}
26-
<span class="font-medium">{{ meta.total }}</span>
29+
<span class="font-medium">{{ pagination.total }}</span>
2730
{{ translations.results }}
2831
</p>
2932
</div>
@@ -33,8 +36,8 @@
3336
aria-label="Pagination"
3437
>
3538
<component
36-
:is="meta.prev_page_url ? 'inertia-link' : 'div'"
37-
:href="meta.prev_page_url"
39+
:is="previousPageUrl ? 'inertia-link' : 'div'"
40+
:href="previousPageUrl"
3841
class="relative inline-flex items-center px-2 py-2 rounded-l-md border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50"
3942
>
4043
<span class="sr-only">{{ translations.previous }}</span>
@@ -52,7 +55,7 @@
5255
</svg>
5356
</component>
5457

55-
<div v-for="(link, key) in meta.links" :key="key">
58+
<div v-for="(link, key) in pagination.links" :key="key">
5659
<slot name="link">
5760
<component
5861
v-if="!isNaN(link.label) || link.label === '...'"
@@ -65,8 +68,8 @@
6568
</div>
6669

6770
<component
68-
:is="meta.next_page_url ? 'inertia-link' : 'div'"
69-
:href="meta.next_page_url"
71+
:is="nextPageUrl ? 'inertia-link' : 'div'"
72+
:href="nextPageUrl"
7073
class="relative inline-flex items-center px-2 py-2 rounded-r-md border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50"
7174
>
7275
<span class="sr-only">{{ translations.next }}</span>

0 commit comments

Comments
 (0)