I have a simple search form (site/search.php) to send search string to controller and show search result if available. I’m using CGridView to show data.
UPDATE Here is my view (site/search.php) file:
<form name="quickSearchForm" action="<?php echo Yii::app()->createUrl("site/search"); ?>" method="POST">
<div data-role="fieldcontain">
<input type="search" name="searchText" id="searchText" value="<?php echo $searchText; ?>"/>
<input type="submit" name="submitBtn" id="submitBtn" value="search"/>
<a href="#">Advanced</a>
</div>
</form>
<?php
if (!empty(itemList)) {
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'task-grid',
'dataProvider'=>$itemList,
'pager'=>array(
'header' => '',
'firstPageLabel' => '<<',
'prevPageLabel' => '<',
'nextPageLabel' => '>',
'lastPageLabel' => '>>',
),
'template'=>'{pager}{items}{pager}',
'columns'=>array(
array(
'name' => 'name',
'value' => '$data->name',
),
array(
'name' => 'date',
'value' => 'date("d/m/Y",strtotime($data->date) )',
),
),
),);
} //end if
?>
At controller, I’m using $searchText to get data with CActiveDataProvider then render back to search.php.
<?php
$this->render($view = "search", array(
'searchText' => $searchText,
'itemList' => $itemListBySearch,
'taskResult' => $tasks,
));
END UPDATE
My problem is $itemList has many item, GridView will paginate. But, I click on button page [2], browser direct to r=site/search&page=2 and itemList lost!
May I use ajax instead direct to r=site/search&page=2???
Yii pager’s default implementation preserves all GET values from the request, but your search form is using POST, so search term aren’t included in pagination links. Try to change form method to GET and you’ll get desired behavior:
PS: IMHO search forms should always use GET method to send search options, because it provides ability to copy URL which holds all search terms, and then send it to someone else (for example to developers) or save it for further reproduction of the same request.