There is a lot of content soon. Displaying all elements on one page is not useful. It has a negative effect on the layout and performance. Therefore, we divide the elements into sub-pages and add pagination or page numbering. With this, navigation through the pages is possible. Links are inserted for this purpose. Usually, these are located at the bottom of the page.

For impatient people: View the changed program code in the Diff View[^codeberg.org/astrid/j4examplecode/compare/t18...t19] and copy these changes into your development version.

Step by step

New files

No new files are added.

Modified files

administrator/components/com_foos/src/View/Foos/HtmlView.php

We do not have any special requests. To display the default pagination, more or less two lines are enough. In the view you call $this->pagination = $this->get('Pagination'); to set the variable $this->pagination.

administrator/components/com_foos/src/View/Foos/HtmlView.php

 	protected $items;

	protected $pagination;

 	/**
 	 * The model state
 	 *

 	public function display($tpl = null): void
 	{
 		$this->items = $this->get('Items');

		$this->pagination = $this->get('Pagination');
 		$this->filterForm = $this->get('FilterForm');
 		$this->activeFilters = $this->get('ActiveFilters');
 		$this->state = $this->get('State');

administrator/components/com_foos/tmpl/foos/default.php

In the template we use the getListFooter method of the variable $this->pagination. That was all!

administrator/components/com_foos/tmpl/foos/default.php

 						</tbody>
 					</table>

					<?php echo $this->pagination->getListFooter(); ?>

 				<?php endif; ?>
 				<input type="hidden" name="task" value="">
 				<input type="hidden" name="boxchecked" value="0">

In the global configuration you can set the number of elements that will be displayed by default. Normally this is set to 20 elements.

Joomla Pagination in global configuration

Do you feel that something is missing in this chapter? Are you wondering where all the calculations are that create the page links? Then take a look at the two files: libraries/src/Pagination/Pagination.php and libraries/src/MVC/Model/ListModel.php. Joomla does all the work for you if you use the default, so if specifically in our case the model extends the file libraries/src/MVC/Model/ListModel.php.

Test your Joomla component

  1. install your component in Joomla version 4 to test it:

Copy the files in the administrator folder to the administrator folder of your Joomla 4 installation.

A new installation is not necessary. Continue using the ones from the previous part.

  1. open the view of your component in the administration area and create so many items that they are no longer displayed on one page. In the lower part you will see a navigation to browse through the contents.

Joomla Pagination