CGridView + ArrayDataProvider + CPagination NOT WORK!!!

my code is:

==Conroller==

$page = new CPagination();

$page->setCurrentPage(1);

$this->render(‘index’,array(

‘data’ => new CArrayDataProvider($data,array(‘pagination’=>$page)),

));

==view==

$this->widget(‘zii.widgets.grid.CGridView’, array(

            'id'=>'data-grid',


            'selectableRows' => 0,


            'dataProvider'=>$data,


            'selectableRows'=>0,


            'columns'=>array(


     		.....


        ));

I want to show the page 2 when opened,and it work;

but then I click the page number,it not work.

sorry ,my english is very poor.

By


$page->setCurrentPage(1)

you’re forcing the paginator to show the first page over and over again. Try this in your view:




<?php $this->widget('zii.widgets.grid.CGridView', array(

  'id'=>'data-grid',

  'selectableRows'=>0,

  'dataProvider'=>new CArrayDataProvider($data),

  'columns'=>array(...),

)); ?>



Now feed your actual data (i.e. not a CArrayDataProvider!) to the $data variable in your view. You might have to set the id and keyField properties as well.

Thx for your help.But I want to show page2 when page initial and user can change the page by click.How?

By checking the source HTML code of a CGridView page, you will notice that a link button in the pager has a page parameter in it.

A link to the 2nd page should be something like the following:




/path/to/the/page?DataName_page=2



I think you can use it for the entry link to the page.

BTW, why do you want to show the 2nd page as the initial display?

The number of the total pages should vary case by case.

Do you mean the last page by it? If so, isn’t it possible to satisfy your needs by reversing the sorting order?

thank you very much ,the problem already solved.

Because I want to show the row which user select last time.The row may at page2 or last page.

I see. It’s one of the common needs.

How did you solve that? Could you share your experience?

change code like this:

==Conroller==

[color="#FF0000"]if(isset($_GET[‘dataname_page’]))

$selection = $_GET[‘dataname_page’];

else

$selection = $count_page;[/color]

$page = new CPagination();

[color="#FF0000"]$page->setCurrentPage($selection - 1);[/color]

$this->render(‘index’,array(

‘data’ => new CArrayDataProvider($data,array(‘pagination’=>$page)),

));

==view==

$this->widget(‘zii.widgets.grid.CGridView’, array(

‘id’=>‘data-grid’,

‘selectableRows’ => 0,

‘dataProvider’=>$data,

‘selectableRows’=>0,

‘columns’=>array(

));