msoa
August 7, 2013, 1:48am
1
Hi,
Considering following assets:
SearchBox( form):
. . . .
<div class="searchBox">
<?php
echo CHtml::beginForm(Yii::app()->createUrl('search'));
echo CHtml::textField('searchItem');
echo CHtml::linkButton(Yii::t('form','Search'));
echo CHtml::endForm()
?>
</div>
. . . .
SearchController:
<?php
class SearchController extends Controller
{
public $layout = 'index';
public function actionIndex()
{
//var_dump($_POST);
$criteria = new CDbCriteria();
$criteria->compare('title',$_POST['searchItem'],true);
$dataProvider = new CActiveDataProvider('Advert',array(
'criteria'=>$criteria,
'pagination'=>array(
'pageSize'=>5,
),
));
$this->render('index',array('dataProvider'=>$dataProvider));
}
}
[b]
[/b]
index.php (view file):
<?php
$this->widget('zii.widgets.CListView',array(
'id'=>'list-ads',
'dataProvider' => $dataProvider,
'itemView' => '_ad',
'summaryText'=>'',
'emptyText'=>'',
'pager'=>array(
'header'=>'',
'prevPageLabel'=>'<',
'nextPageLabel'=>'>',
),
));
?>
$dataProvider is backed to $_POST[‘searchItem’] , hence when changing page for fetching other records(CLinkPager) $_POST[‘searchItem’] is not set, so when changing page raise this error:
Undefined index: searchItem (C:\Apache2\htdocs\website\protected\controllers\SearchController.php:16)
Question: How can i properly adjust the $dataProvider so that when changing paged in pagination can fetched other records?
Hi,
i think you may change the query like
$criteria = new CDbCriteria();
$criteria->select = '*'; // select All
$criteria->addCondition("title LIKE '%" . $_GET['searchItem'] . "%'");
kiran123
(Sharmakiran71)
August 7, 2013, 5:14am
3
If you pre assign model then you can use,
$criteria->compare('title',$_POST['searchItem'],true);
but for following case you had to use,
$criteria->condition = "Your condition here";
[size="2"]i.e[/size]
$criteria = new CDbCriteria;
$criteria->select = '*'; // select All
$criteria->condition = "key='$key'";
msoa
August 7, 2013, 8:01am
4
my problem is with fetching other records that are exist on other page(pagination 1 2 3 4 . . . )! when click on page 2 3 4 . . . the searchItem is not defined.
kiran123
(Sharmakiran71)
August 7, 2013, 10:08am
5
For that use below code to pass parameter…
$dataProvider = new CActiveDataProvider('Advert',array(
'criteria'=>$criteria,
'pagination'=>array(
'params' => array('param1' => $param1, 'param2' => $param2) // Insert this line
),
msoa
August 8, 2013, 12:16am
6
For that use below code to pass parameter…
$dataProvider = new CActiveDataProvider('Advert',array(
'criteria'=>$criteria,
'pagination'=>array(
'params' => array('param1' => $param1, 'param2' => $param2) // Insert this line
),
Don’t work. In fact i want CListView based on $_POST variables. ?
seenivasan
(Chellamnivas)
August 8, 2013, 5:34am
7
Dear Friend
The problem here is that we are making a varaible by POST method in one request and make it available in another request.
The varaiable is only available in immediate next request(first page).
To make it available on subsequent page requests we have to make it stateful.
public function actionIndex()
{
if(isset($_POST['searchItem']) && $_POST['searchItem']!==null)
Yii::app()->user->setState('searchItem',$_POST['searchItem']);
$searchItem=Yii::app()->user->getState('searchItem');
$criteria = new CDbCriteria();
$criteria->compare('title',$searchItem,true); //$searchItem is available on susequent requests.
$dataProvider = new CActiveDataProvider('Advert',array(
'criteria'=>$criteria,
'pagination'=>array(
'pageSize'=>5,
),
));
$this->render('index',array('dataProvider'=>$dataProvider));
}
I hope it would clear the things.
Regards.
msoa
August 8, 2013, 9:56am
8
seenivasan:
Dear Friend
The problem here is that we are making a varaible by POST method in one request and make it available in another request.
The varaiable is only available in immediate next request(first page).
To make it available on subsequent page requests we have to make it stateful.
public function actionIndex()
{
if(isset($_POST['searchItem']) && $_POST['searchItem']!==null)
Yii::app()->user->setState('searchItem',$_POST['searchItem']);
$searchItem=Yii::app()->user->getState('searchItem');
$criteria = new CDbCriteria();
$criteria->compare('title',$searchItem,true); //$searchItem is available on susequent requests.
$dataProvider = new CActiveDataProvider('Advert',array(
'criteria'=>$criteria,
'pagination'=>array(
'pageSize'=>5,
),
));
$this->render('index',array('dataProvider'=>$dataProvider));
}
I hope it would clear the things.
Regards.
I used following code. Changed the type of ajax request to POST and set the data: {searchItem:‘html’},
$(document).ready(function(){
$('#list-ads').click(function(){
$.fn.yiiListView.update = function(id, options) {
var settings = $.fn.yiiListView.settings[id];
$('#'+id).addClass(settings.loadingClass);
options = $.extend({
type: 'POST',
data: {searchItem:'html'},
url: $.fn.yiiListView.getUrl(id),
success: function(data,status) {
$.each(settings.ajaxUpdate, function(i,v) {
var id='#'+v;
$(id).replaceWith($(id,'<div>'+data+'</div>'));
});
if(settings.afterAjaxUpdate != undefined)
settings.afterAjaxUpdate(id, data);
$('#'+id).removeClass(settings.loadingClass);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
$('#'+id).removeClass(settings.loadingClass);
alert(XMLHttpRequest.responseText);
}
}, options || {});
if(options.data!=undefined && options.type=='GET') {
options.url = $.param.querystring(options.url, options.data);
options.data = {};
}
options.url = $.param.querystring(options.url, settings.ajaxVar+'='+id);
if(settings.beforeAjaxUpdate != undefined)
settings.beforeAjaxUpdate(id);
$.ajax(options);
};
});
})
But if Javascript is disabled the functionality will loss.
@seenivasan : Your practice is good and acts in both Ajax and Post method. Very Thanks.