Pagination in Joomla 3

2,817 views
Skip to first unread message

M. Asif

unread,
Sep 10, 2013, 2:51:41 PM9/10/13
to joomla-de...@googlegroups.com
Is Pagination in Joomla 3 needs different code from 2.5? I have seen examples of 2.5 but they don't work in 3. For example setState in the model require JRegistry object which I don't know how to provide it.

Can any one help in this regard?

Thanks

Viper

unread,
Sep 10, 2013, 3:45:11 PM9/10/13
to joomla-de...@googlegroups.com
If you are using JModelList you do not need setState(). But if you want to change some variables for Pagination object you can do it in populateState().

PS! If you want to create JRegistry object(not only for pagination) do
$object = new JRegistry;
$object->set('variable', 'value');

M. Asif

unread,
Sep 10, 2013, 3:56:00 PM9/10/13
to joomla-de...@googlegroups.com
I have done this successfully now. I am not sure if this is the correct way to do it or not.

I was actually trying to follow lendr tutorials. But he has not provided working code in the master repository of git for the example.

I have put these two lines in my model which is extended from JModelBase

$this->limit = $this->_app->getUserStateFromRequest('limit', 'limit', 10, 'int');
$this->limitstart = JRequest::getVar('limitstart', 0, '', 'int');

and my getPagination() method in the model class is as below

function getPagination(){
    // Lets load the content if it doesn't already exist
    if ($this->_pagination == null || empty($this->_pagination)){
      $this->_pagination = new JPagination($this->getTotal(), $this->limitstart, $this->limit );
      
    }
    
    return $this->_pagination;
  }


After doing this i only have put $this->pagination = $model->getPagination() in my view which is extended from JViewHTML

And then in the template I have put $this->pagination->getListFooter();

Now its working correctly for me.

But I am not sure how the state can be used in this case, as it looks like its altogether missing from my implementation. Will see how i can achieve that too.

Thanks for you reply Viper.

Viper

unread,
Sep 10, 2013, 4:48:13 PM9/10/13
to joomla-de...@googlegroups.com
Why you use JModelBase? JModelList do all dirty works.

Model
class ABCModelAbc extends JModelList {
    protected function populateState($ordering = null, $direction = null) {
        $app = JFactory::getApplication();
        $params = $app->getParams('com_my');

        parent::populateState($params->get('sort_field'), strtoupper($params->get('sort_ord')));
    }

    protected function getListQuery() {
        $db = $this->getDBO();
        $query = $db->getQuery(true);
        $query->select("...")->from("")->where("");

        $orderCol = $this->state->get('list.ordering', 'ordering');
        $orderDirn = $this->state->get('list.direction', 'desc');
        $query->order($db->escape($orderCol.' '.$orderDirn));

        return $query;
}

View
class ABCViewAbc extends JViewLegacy {
    protected $state = null;
    protected $items = null;
    protected $pagination = null;

    public function display($tpl = null) {
        $state = $this->get('State');
        $items = $this->get('Items');
        $pagination = $this->get('Pagination');

        $this->items = &$items;
        $this->pagination = &$pagination;

        parent::display($tpl);
    }
}

Template
<div class="pagination bottom">
    <?php echo $this->pagination->getPagesLinks(); ?><br />
    <?php echo $this->pagination->getResultsCounter(); ?>
</div>

That's all.

M. Asif

unread,
Sep 10, 2013, 7:54:12 PM9/10/13
to joomla-de...@googlegroups.com
Ok thanks for this. But JModelList is extended from JModelLegacy so I thought of not using any legacy class to make native Joomla 3 component.

The word legacy does not sound good actually.

Why not the most current options be used?

Bakual

unread,
Sep 11, 2013, 10:36:22 AM9/11/13
to joomla-de...@googlegroups.com
Legacy is what is used in core for almost everything. It can't be that bad :)
I'm not sure what the plans are here. But I promise that they will stick around for a long time ;)
Reply all
Reply to author
Forward
0 new messages