Skip to content

Commit aab64bc

Browse files
author
Joeri
committed
Fix add multiple pagination instances possible
1 parent a13dcaa commit aab64bc

6 files changed

Lines changed: 42 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## [1.0.4] - 2024-11-25
2+
3+
Fixed multiple pagination instances possible \
4+
Update README
5+
16
## [1.0.4] - 2024-04-23
27

38
Fixed table style 'tight' \

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,15 @@ class LatestOrders extends TableCard
232232
}
233233
```
234234
![Nova Table Card](https://github.com/Whitespacecode/nova-table-card/blob/master/examplePaginate.png)
235+
236+
### Multiple TableCards with pagination per page
237+
Sometimes you may need to render multiple `TableCard` instances with pagination on the same page or dashboard. However, those paginator instances use the `page` query string to track the current page and the paginators will conflict. <br/>To resolve this, you can customize the query string the name of the query string used by each paginator by passing a third argument to the `paginate` method. <br/>
238+
Official documentation in the [Laravel Pagination Docs](https://laravel.com/docs/11.x/pagination#multiple-paginator-instances-per-page).
239+
240+
```php
241+
// Set a unique query key for this paginator
242+
$this->queryKey('orders');
243+
244+
// Pass the query key to paginate
245+
$orders = Orders::paginate(5, ['*'], 'orders');
246+
```

dist/js/card.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/js/components/Card.vue

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@
4747
</a>
4848
</div>
4949

50-
<Pagination v-if="paginator" v-model="paginator" @update-rows="update"></Pagination>
50+
<Pagination
51+
v-if="paginator"
52+
v-model="paginator"
53+
:query-key="queryKey"
54+
@update-rows="update"
55+
></Pagination>
5156
</div>
5257
</template>
5358

@@ -71,6 +76,7 @@ export default {
7176
header: [],
7277
title: '',
7378
viewAll: null,
79+
queryKey: '',
7480
paginator: null,
7581
}
7682
},
@@ -97,21 +103,22 @@ export default {
97103
},
98104
viewAllBottomPosition() {
99105
return this.viewAll && this.viewAll.position && this.viewAll.position === 'bottom';
100-
}
106+
},
101107
},
102108
methods: {
103109
update(event){
104110
this.rows = event;
105111
}
106112
},
107113
created () {
108-
const {header, rows, title, paginator, viewAll} = this.card;
114+
const {header, rows, title, paginator, viewAll, queryKey} = this.card;
109115
110116
this.header = header;
111117
this.title = title;
112118
this.rows = rows;
113119
this.paginator = paginator;
114120
this.viewAll = viewAll;
121+
this.queryKey = queryKey;
115122
},
116123
}
117124
</script>

resources/js/components/Pagination.vue

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default {
2929
name: 'Pagination',
3030
props: {
3131
modelValue: Object,
32+
queryKey: '',
3233
},
3334
mixins: [
3435
Paginatable,
@@ -72,11 +73,13 @@ export default {
7273
},
7374
methods: {
7475
selectPage(page) {
76+
const queryKey = this.queryKey;
77+
7578
this.currentPage = page;
7679
7780
Nova.request().get(this.path, {
7881
params: {
79-
page: this.currentPage,
82+
[queryKey || 'page']: page
8083
}
8184
}).then(response => {
8285
let cards = response.data;
@@ -85,7 +88,9 @@ export default {
8588
cards = response.data.cards;
8689
}
8790
88-
const paginationCard = cards.find(card => card.component === 'nova-table-card');
91+
const paginationCard = cards.find(card =>
92+
card.queryKey === queryKey || (queryKey === '' && card.component === 'nova-table-card')
93+
);
8994
9095
this.$emit('update:modelValue', paginationCard.paginator);
9196
this.$emit('updateRows', paginationCard.rows);

src/TableCard.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class TableCard extends Card
2929
* @param string $title
3030
* @param array $viewAll
3131
*/
32-
public function __construct(array $header = [], array $data = [], string $title = '', array $viewAll = [])
32+
public function __construct(array $header = [], array $data = [], string $title = '', array $viewAll = [], string $queryKey = '')
3333
{
3434
parent::__construct();
3535

@@ -40,6 +40,7 @@ public function __construct(array $header = [], array $data = [], string $title
4040
'rows' => $data,
4141
'title' => $title,
4242
'viewAll' => $viewAll,
43+
'queryKey' => $queryKey,
4344
'showBorders' => false,
4445
]);
4546
}
@@ -64,6 +65,11 @@ public function viewAll(array $viewAll)
6465
return $this->withMeta(['viewAll' => $viewAll]);
6566
}
6667

68+
public function queryKey(string $queryKey)
69+
{
70+
return $this->withMeta(['queryKey' => $queryKey]);
71+
}
72+
6773
public function paginator(LengthAwarePaginator $paginator)
6874
{
6975
return $this->withMeta([

0 commit comments

Comments
 (0)