J4.x

Joomla 4 Tips and Tricks: Number of Records

From Joomla! Documentation

Introduction[edit]

List pages that return many results usually have a pagination widget at the foot of the page but it is not obvious exactly how many records are available. The solution described here comes from a component that has more than a dozen list views and involves placing a custom information button in the toolbar.

The HtmlView.php file[edit]

The end of this file is a good place for a custom information button, just before the code for the Help button. By that stage the pagination will have been set. So the code looks roughly like this:

	public function display($tpl = null)
	{
		....
		$this->pagination    = $this->get('Pagination');
		....
		$this->addToolbar();
		....
	}
	protected function addToolbar()
	{
		....
		$nRecords = $this->pagination->total;
		$toolbar->standardButton('nrecords')
		->icon('fa fa-info-circle')
		->text($nRecords . ' Records')
		->task('')
		->onclick('return false')
		->listCheck(false);
		....
	}

Where ... indicate the other lines found in this file. There is a snag: the info icon has primary button colours. And as this code is to be used many times it is best to put it into a helper.

Calling and using a helper[edit]

This code goes in every page that needs a record count in place the standardButton code shown above:

		$nRecords = $this->pagination->total;
		$toolbar->customHtml(ButtonsHelper::getRecordCount($nRecords));

And the ButtonsHelper file has lots of buttons in it. This is the one used here:

class ButtonsHelper {
	...
	public static function getRecordCount($nRecords) {
		$html = '
		<joomla-toolbar-button id="toolbar-record-count" task="">
		<button class="btn btn-info">
		<span class="fa fa-info-circle" aria-hidden="true"></span>
		' . $nRecords . ' Records</button>
		</joomla-toolbar-button>
		';
		return $html;
	}
	...
}

And of course the word Records should be replaced by a string constant.

Result[edit]

Number of Records Toolbar button

Handy to have this at the top of the page!

Parent Links[edit]

Back to J4.x:Tips and Tricks for Joomla 4 Developers