J4.x

Joomla 4 Tips and Tricks: Number of Records

From Joomla! Documentation

Documentation all together tranparent small.png
Under Construction

This article or section is in the process of an expansion or major restructuring. You are welcome to assist in its construction by editing it as well. If this article or section has not been edited in several days, please remove this template.
This article was last edited by Ceford (talk| contribs) 18 months ago. (Purge)

Introduction[edit]

List pages that return many results usually have a pagination widget at the foot of the page. It is often useful to know how many records are available but this is not obvious. 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 Record should be replaced by a string constant.

Result[edit]

Number of Records Toolbar button

Handy to have this at the top of the page!