JPagination for Joomla 3.0. How it's working?

1,833 views
Skip to first unread message

Viper

unread,
Mar 6, 2013, 8:26:20 AM3/6/13
to joomla-...@googlegroups.com
Yesterday I started to write my own component. But there is a problem with understanding how pagination should work. Before Ш start a topic, I've read all the available topics in the JDM group about it.

Model
class OwlModelOwls extends JModelList {
    protected function populateState($ordering = 'ordering', $direction = 'ASC') {
        $app = JFactory::getApplication();
        $limit = $app->input->get('limit', 25, 'uint');
        $this->setState('limit', $limit);

        $limitstart = $app->input->get('limitstart', 0, 'uint');
        $this->setState('limitstart', $limitstart);
        $this->setState('ordering', $ordering);
    }

    protected function getListQuery() {
        $db = $this->getDBO();

        $query = $db->getQuery(true);
        $query->select("`m`.`id`, `m`.`parent_id`, `m`.`title`, `m`.`alias`, `m`.`introtext`, `m`.`desc`, DATE_FORMAT(`m`.`year`, '%Y') AS `year`, DATE_FORMAT(`m`.`created`, '%Y-%m-%d') AS `created`, DATE_FORMAT(`m`.`modified`, '%Y-%m-%d') AS `modified`, `g`.`filename`");
        $query->from($db->quoteName('#__m').' AS `m`');
        $query->leftJoin($db->quoteName('#__gallery').' AS `g` ON `g`.`is_m` = 1 AND `g`.`m_id` = `m`.`id` AND `g`.`is_poster` = 1 AND `g`.`is_poster_frontpage` = 1');
        $query->where('`language` IN ('.$db->quote(JFactory::getLanguage()->getTag()).','.$db->quote('*').')');
        $query->order('`ordering` DESC');

        return $query;
    }
}


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

    public function display($tpl = null) {
        $user = JFactory::getUser();
        $app = JFactory::getApplication();

        $state = $this->get('State');
        $items = $this->get('Items');
        $pagination = $this->get('Pagination');
        $params = $app->getParams('com_owl');

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

        $this->_prepareDocument($params);

        parent::display($tpl);
    }
}


Template
** foreach for list of items
    <div class="pagination">
        <p class="counter pull-right"><?php echo $this->pagination->getPagesCounter(); ?></p>
        <?php echo $this->pagination->getPagesLinks(); ?>
    </div>

And this is doesn't work. Paginator is not shown. Just blank <div> and <p>. If I do print_r() for $this->pagination I see

JPagination Object
(
    [limitstart] => 0
    [limit] => 2
    [total] => 2
    [prefix] => 
    [pagesStart] => 1
    [pagesStop] => 1
    [pagesCurrent] => 1
    [pagesTotal] => 1
    [viewall:protected] => 1
    [additionalUrlParams:protected] => Array
        (
        )
)

What's wrong and where's the mistake?

PS! In debugging I see 2 identical sql query.

Bakual

unread,
Mar 6, 2013, 12:09:02 PM3/6/13
to joomla-...@googlegroups.com
Do you have enough items? Pagination doesn't show if only one page is populated.
The Pagination object you posted shows only two items and one page.

Viper

unread,
Mar 6, 2013, 1:16:42 PM3/6/13
to joomla-...@googlegroups.com
Ok. I understand. But that if I set limit to one item per page? Ok, let's do this...
$this->setState('limit', 1);

And nothing changed. Even in JPagination array the limit value not changed.
Maybe I'm must write own getPagination() method in my model, which redefined pagination method from parent class?

Bakual

unread,
Mar 6, 2013, 2:36:35 PM3/6/13
to joomla-...@googlegroups.com
It would be
$this->setState('list.limit', 1);
 
But you don't have to set those values yourself. You can just use
parent::populateState('ordering', 'ASC'); // define your default ordering values here
 
This will take care of the ordering, dirextion and the pagination values. If you don't want to set any state values yourself, you can even delete your own populateState function and it will use the parent one instead.
 
For the $query->ordering you can use this insead, which will take care of the states:
$query->order($db->escape($this->getState('list.ordering', 'ordering')).' '.$db->escape($this->getState
('list.direction', 'ASC')));

Viper

unread,
Mar 7, 2013, 2:26:28 AM3/7/13
to joomla-...@googlegroups.com
Thank you Bakual.
But few small questions...

If I redefine the parent funtion like this
protected function populateState($ordering = 'ordering', $direction = 'DESC') {
    parent::populateState($ordering, $direction);
}


Where are from these values? From request, or from the default value?
And how can I show a pagination(items counter) if limit less than the default value?

And last question. Query duplicated. First just as it in query set, second - with limits. That is correct?

Bakual

unread,
Mar 7, 2013, 4:47:14 AM3/7/13
to joomla-...@googlegroups.com
I you define the function like this
protected function populateState($ordering = 'ordering', $direction = 'DESC') {
    parent::populateState($ordering, $direction);
}

then the default values ('ordering' and 'DESC') are used.

For the result count you can use
echo $this->pagination->getResultsCounter();
or
echo $this->pagination->total;

And yes, Joomla does the query twice, one time to get the total of items and a second time to get only the selected batch.

Nick Savov

unread,
Mar 7, 2013, 10:28:06 PM3/7/13
to joomla-...@googlegroups.com
Hi guys,

Sorry, I don�t mean to step on any toes, but this list is for discussion
of how to improve the CMS.

You should instead ask this question on the Joomla General Developer list.
Here's the direct link to it:
https://groups.google.com/forum/?fromgroups#!forum/joomla-dev-general

Hope this helps!

Kind regards,
Nick
Reply all
Reply to author
Forward
0 new messages