public function __construct($config = array())
$config['filter_fields'] = array(
protected function getListQuery()
$db = $this->getDbo();
$query = $db->getQuery(true);
// Select the required fields from the table.
'list.select', 'DISTINCT a.*'
$query->from('`#__breed_breed` AS a');
$orderCol = $this->state->get('list.ordering');
$orderDirn = $this->state->get('list.direction');
if ($orderCol && $orderDirn)
$query->order($db->escape($orderCol . ' ' . $orderDirn));
Implementing Grid Sort: In the breeds model class we are overriding the default constructor and adding a list of fields that could be sort.
We are overriding the default JModelList constructor here which takes a configuration array as argument and in the contructor method, we are calling the filter_fields config array. In overriding the constructor we are first checking that whether the settings have been defined previously or not and if they are not, we are providing defaults values here. We are defining here the field names which can be used in the queries and if you are passing any other variables as the basis for filtering the list, they will not be accepted. In short you can take it as, when you click on any column heading for sorting, populateState method, which I have discussed in the previous lessons, picks the value, passed to it by the request and then it matches the list of fields defined in the filter_fields config array, in this constructor method.
Now the question is, why we are doing all this stuff and which problem, it is solving. The answer is, we are allowing the user to filter on the basis of only those fields which we want him to filter. So, in this way, no hacking code could be inserted in the filter column and that makes the process completely safe.
So the config array will contain the fields that are valid and then the parent constructor is called. Now lets talk about the populateState function that we have discussed while working on creating the grid sort feature at frontend. In the backend, populateState method is extensively used and ordering and direction are already being passed to this method, so we dont have to do anything here. As ordering is a major and most used function, so joomla includes this feature by default. We have to make some changes to our main query now, by adding code from line 45 to 51. It is getting the list ordering and list direction and assigning it to two variables, $orderCol and $orderDirn respectively. On line 48, first we check that we actually have a column which we can sort by. After that, we call the order function through query object and then pass the column name and direction.
public function display($tpl = null)
$this->pagination = $this->get('Pagination');
$this->state = $this->get('State');
We have added some new code to the view.html.php file, to support pagination and grid sorting in the list view of the component, we are developing. On line 12 and 14 of above mentioned code, we have created two protected properties, $pagination and $state and on lines 19 and 20, we are first assigning the state property, the values of the request variables from the state method and secondly, we are getting pagination so that we can get or call methods related to pagination, in the default.php layout file.
Now lets start working on the default.php file to implement grid sort and pagination feature to the list view. In this file code above, on line 6 and 7, we are getting the current state of ordering and direction requests and passing these values to $listOrder and $listDirn respectively. We are again using, php method chaining concept here and through $this specifier, We are getting ordering and direction vaues through state property, to which we have assigned values in view.html.php file. Keeping security in mind, you can also filter the results of these two properties through escape method, while working on the production environment.
The other header columns work in the same way. After that, the <tbody> section comes and I have explained it in detail, in the previous step, and so, if things are not fresh in your mind regarding that, you can refer to the previous step.
From line 80 to 86, we have added the pagination related code, to display pagination in the table footer section. In the colspan section, the code is calculating number of columns it has to cover, to construct a proper layout for the pagination buttons. And then on line 83, there comes our actual pagination code. The getListFooter() is a JPagination class method which generates the buttons and their links.
I basically work in the CMS, like Joomla and WordPress and in framework like Laravel and have keen interest in developing mobile apps by utilizing hybrid technology. I also have experience working in AWS technology. In terms of CMS, I give Joomla the most value because I found it so much user freindly and my clients feel so much easy to manage their project in it.
Enjoy best web development services at an affordable price. Looking forward to build a good relationship and serve you better...
My name is Nohman Habib and I am a web developer with over 10 years of experience, programming in Joomla, Wordpress, WHMCS, vTiger and Hybrid Apps. My plan to start codingace.com is to share my experience and expertise with others. Here my basic area of focus is to post tutorials primarily on Joomla development, HTML5, CSS3 and PHP.