Issues

ZF-4936: multiple paginator instances with the same view script

Description

You currently have to create a new view script for every paginator instance you want to use on a single page.

This is caused by the paginator view script setting the url page parameter. If each paginator instance uses the same view script then when you paginate one you will paginate all.

Here is the code to fix this.

I would like to commit it to the trunk but I wanted someone else to review this first.


class Paginator extends Zend_Paginator
{
    public $pageName = 'page';
    
    public function getPages($scrollingStyle = null)
    {
        $pages = parent::getPages($scrollingStyle);
        
        $pages->pageName = $this->pageName;
        
        return $pages;
        
    }
    
    public function setPageName($pageName)
    {
        $this->pageName = $pageName;
    }
}

<?php if (isset($this->previous)): ?> 
  < Previous | 
<?php else: ?> 
  < Previous | 
<?php endif; ?> 

Comments

{quote} This is caused by the paginator view script setting the url page parameter. If each paginator instance uses the same view script then when you paginate one you will paginate all. {quote}

Right. and this intentional; say you have two pagination controls, one at the top of the results and another at the bottom. Both advance the same.

I'm curious how in your scenario you would have two pagination controls controlling different data but with identical URLs. Or maybe I'm misunderstanding.

I have a page to view a user profile at /user/view/id/1

On this page I have multiple sets of data like Comments, Articles, Pictures and Friends.

In my UserController I have code to create an sql statement for each data set. For each sql statement I create a new instance of Zend_Paginator using the modifications above. I give each pagination instance a unique page name.

I can choose to paginate Articles to the next page at /user/view/id/1/articlePage/2. This will paginate the article data set only.

I can then choose to paginate pictures to the last page at /user/view/id/1/articlePage/2/picturePage/999. This keeps the articles on page 2 and the pictures on page 999.

The class Zend_View_Helper_Url handles all of this for me. I can use the same view script for each pagination instance giving my site a more consistent look and feel.

You can also do it by:

in Controller:


$sth = getPages()->pageName = 'page2';

where $sth is instance of Zend_Paginator

Now you have $this->pageName in you paginator view, you can only add ie.:


<?php if (empty($this->pageName)) $this->pageName = 'page' ?>

and as above:


<?php if (isset($this->previous)): ?> 
  < Previous | 
<?php else: ?> 
  < Previous | 
<?php endif; ?>

{quote}in Controller:



I assume you meant

which actually works quite well.

The correct items are subtracted from the set, but for some reason the pagination controls seem to disagree. As yet I am unable to convince them it is not first page any more....


Employees
Last name   First name  Type        Location
Test        Twentyeight     Non Admin   Oldenburg   delete
Test        Sixtynine   Non Admin   Oldenburg   delete
Test    Thirty  Non Admin   Oldenburg   delete
add
1 - 10 of 33 First | < Previous | Next > | Last

Those are the final 3 records (after having clicked on last) but the paginator controls still think the first 10 are shown, so first and previous are disabled, next and last are enabled. Will continue to look into it.

And I found where I went wrong, getPages() uses the currentpagenumber, which I set after calling the getPages function....... Now it works brilliantly, thanks!