Hello, I wanted an ajax search for my website, implemented with my CListView. Could find some hints on the forum but nothing really working.
http://www.yiiframework.com/forum/index.php?/topic/15509-any-plans-to-enhance-clistview/
What brought me a step further is this post:
solution that I’ve updated to work with delayed effect (to give a rest to the DB) and CActiveDataProvider.
View
Yii::app()->clientScript->registerScript('search',
"var ajaxUpdateTimeout;
var ajaxRequest;
$('input#search').keyup(function(){
ajaxRequest = $(this).serialize();
clearTimeout(ajaxUpdateTimeout);
ajaxUpdateTimeout = setTimeout(function () {
$.fn.yiiListView.update(
'yw0',
{data: ajaxRequest}
)
},
500);
});"
);
?>
<b><label for="search">Cerca: </label></b>
<input type="text" id="search" name="search" />
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'viewList',
'viewData'=>array('category'=>$category),
'sortableAttributes'=>array(
'title',
'category'
),
)); ?>
Controller
/**
* shows every offer in a list
*/
public function actionIndex($search = '')
{
$criteria = new CDbCriteria();
$criteria->addSearchCondition('title', $search, true, 'OR');
$dataProvider = new CActiveDataProvider( 'Offer', array(
'criteria'=>$criteria,
)
);
$this->render( 'index', array( 'dataProvider' => $dataProvider, 'category'=>new Category, ) );
}
Hope anybody can find this useful, as it took me the whole morning to figure it out