pagination with livewire is the same with laravel. you just need to use WithPagination.
https://laravel-livewire.com/docs/2.x/pagination#paginating-data
How to Paginate an Array in Livewire?
Hey guys I have an array of products which are fetched dynamically and displayed in a table format, How can I add pagination to the array?
@AungHtetPaing__ Hey! I tried using this, This doesn't work on an array.
Have you tried to use database pagination instead of an array?
@jlrdw I have two tables in view, Table 1 and Table 2. Table 2 has all the products which comes from the DB. Now I have a functionality which on the click of the row button that product is added in the Table 1 which is an array. There can be more than 100 products so I wanted to paginated that.
Like @jlrdw writes, you should handle the pagination in the database, but if that isn't and option, you need to handle it client side with javascript. It's not as hard as it may sound.
You need to keep track of this
- Items per page
- Total number of items
- Total number of pages
- Current page
Let's say that you want ten per page and you have 100 records.
- Items per page: 10
- Total number of items: 100
- Total number of pages: 100/10
- Current page: 0
Then you can use this calculation to know which items to display.
current page + number of items = last item to display
last item to display - items per page = first item to display
@ap1234 surely you only need to split the array into chunks then keep a page counter which you can increment or decrement to move through 'pages'
Make your array a collection, chunk it, then select the correct chunk in your render method
I faked a Sushi model out of my array and was to able to fully utilize Livewire pagination functionality.
Please or to participate in this conversation.