[SOLVED] Pagination in CGridview
Rate Topic:

In standard Yii CRUD result, we will have the admin page already paginating. My concern is, for example we are on page 5, then view one of the record, then I have a redirect link to the admin page, if I click on this link, it will bring me back to admin page with page 1. How could I go to page 5?

In my case, and to solve that issue, I will overwrite the back to admin link and include the page it was and for that, you will also have to get the value of the page CGridView was. That means, you'll also have to include your edit buttons with the value of page parameter (normally is: Modelname_page).

I have successfully done that for my specific CMS that has nothing to do with the one that comes by default with Yii. And I created my own grid and pagination widgets. I guess that you dont want that.

In my case, and to solve that issue, I will overwrite the back to admin link and include the page it was and for that, you will also have to get the value of the page CGridView was. That means, you'll also have to include your edit buttons with the value of page parameter (normally is: Modelname_page).

I have successfully done that for my specific CMS that has nothing to do with the one that comes by default with Yii. And I created my own grid and pagination widgets. I guess that you dont want that.

Hi Antonio Ramirez,
Could you explain more that? It's a very important topic. I think many persons need that stuff.
Thanks!

This is very helpful. But, one thing that I need to mention after studying your code, this is only work if in the CGridView I set the "ajaxUpdate"=>false. Otherwise, I cannot get the Modelname_page parameter.

Another question, how can I get the parameter if I use ajax in the CGridView?

Thanks a lot Antonio, you are the best! One of the most helpful person in yii's forum.

Another question, how can I get the parameter if I use ajax in the CGridView?

Hi Daniel,
It is weird as when doing any calls it gets the value from the HREF property of the links (remember than on the first page you wont see the parameter as a page is not set, the parameter starts from page 1, that is second page)

The only one who does an ajax call by default and is 'monitored' by CGridView Javascript functions is the delete. Check the following code:

// code extracted from init() function
// it does show that if no click event
// is placed, the link will fire by default
// as it should be, therefore the solution
// provided in the blog is correct
foreach($this->buttons as $id=>$button)
{
if(strpos($this->template,'{'.$id.'}')===false)
unset($this->buttons[$id]);
else if(isset($button['click'])) // see how it looks for the click event?
{
if(!isset($button['options']['class']))
$this->buttons[$id]['options']['class']=$id;
if(strpos($button['click'],'js:')!==0)
$this->buttons[$id]['click']='js:'.$button['click'];
}
}
//-------------------------------
// code extracted from initDefaultButtons
// check this code here, see that is the delete
// function the only one monitored by CGridView?
$this->buttons['delete']['click']=<<<EOD
function() {
$confirmation
$.fn.yiiGridView.update('{$this->grid->id}', {
type:'POST',
url:$(this).attr('href'),$csrf
success:function() {
$.fn.yiiGridView.update('{$this->grid->id}');
}
});
return false;
}
EOD;
//---------------------------
// code extracted from the rendered Javascript
// look that is the delete the only one 'live' monitored
jQuery('#news-grid a.delete').live('click',function() {
if(!confirm('Are you sure you want to delete this item?')) return false;
....

This is done this way as by default Yii, jumps to an edition and view Views of the Models.

Make sure you are on page number 2 to check for the parameter. It should be ok my friend. If not, drop the code in here and we will see how we can to the right answer.

Thank you very much for your kind words Daniel. There are lots of good people here ready to collaborate with others to get to the best solution that truly helped me through my learning experience, there are so many I cannot even name them all. Respect for all of them.

These are my steps:
1. load the admin which contains CGridView.
2. From pagination click on the page 3.
3. If I am not using "ajaxUpdate"=>false, the url in address bar is still the same as http://localhost/mas...r=account/admin

Do you have firebug on firefox or developer tools chrome? if so, open your firebug and go to the XHR requests. Filter the requests made on the console, as when you do AJAX calls, your address bar remains the same link as it was but on your AJAX calls you see the actual links.

If you set the ajaxUpdate= false, then no AJAX is performed and you will see the real link on the address bar.

Let me know if you have problems setting firebug for firefox. I think that is why is causing confusion.

Do you have firebug on firefox or developer tools chrome? if so, open your firebug and go to the XHR requests. Filter the requests made on the console, as when you do AJAX calls, your address bar remains the same link as it was but on your AJAX calls you see the actual links.

If you set the ajaxUpdate= false, then no AJAX is performed and you will see the real link on the address bar.

Let me know if you have problems setting firebug for firefox. I think that is why is causing confusion.

Hi Antonio,

Thanks a lot. Somehow it is working now even when I remove the ajaxUpdate=>false. Still, I could not understand why since the url creation for buttons are outside the CGridView declaration.

I did a mistake, Although, I borrowed your code, I tested it not from the button but from my link thats why I still see problem, see portion of code below:

Thanks a lot. Somehow it is working now even when I remove the ajaxUpdate=>false. Still, I could not understand why since the url creation for buttons are outside the CGridView declaration.

It should always work Daniel as with or without AJAX the URLs are taken from the HREF. Now I dare you to find a solution for the SORTing

Happy is working of you

ps: about XHR and firebug or chrome development tools console is a *must have* tool Daniel. Once you get your hands onto it, you wont be able to develop-debug without it. Hint: check the javascript breakpoints now coming with those consoles.

It should always work Daniel as with or without AJAX the URLs are taken from the HREF. Now I dare you to find a solution for the SORTing

Happy is working of you

ps: about XHR and firebug or chrome development tools console is a *must have* tool Daniel. Once you get your hands onto it, you wont be able to develop-debug without it. Hint: check the javascript breakpoints now coming with those consoles.

Cheers!

Hi Antonio,

I used firebug for some times, but never understand the XHR functionality until yesterday

The part that I could not understand fully is that the $buttons definitions are outside the gridview definition, why the ajax call can update the $buttons.url while its definition is outside the gridview?