We will see how to add pagination for our custom collection in this post. In our previous post, we saw how to display data at frontend. We saw how to display data in the list page and view page. In the list page, what if you have many records like maybe 100 or more. It’s good to show data using pagination and we did not use that. So in this post, we are going to see how to add pagination in the list page.


Pagination For Custom Collection In Magento 2

Pagination For Custom Collection In Magento 2


I am using the same module which we have created before. It is Vky_Test. You may want to check this for it. click here

To add pagination we have to make some changes in a Block TestListData.php and listdata.phtml file.
Why changes in TestListData.php and listdata.phtml? Because of the layout file test_index_listdata.xml of the list page.

File Vky/Test/view/frontend/layout/test_index_listdata.xml

You can see we are using class="Vky\Test\Block\TestListData" and template="Vky_Test::listdata.phtml"
That’s why we need to make changes in both files.

Right now our TestListData.php file is like below.Vky/Test/Block/TestListData.php

After changing our file should be like below Vky/Test/Block/TestListData.php

Now we just need to add below code in listdata.phtml

So our edited listdata.phtml Vky/Test/view/frontend/templates/listdata.phtml

It’s Done. Flush the cache and check you should have the pagination toolbar at your page.

Next: Magento 2 custom module image upload
(Check the post)


No Comments

Post A Comment