Yii CListView custom ajax search fileter

We can make a custom search on CListView listing page with pagination.

Following steps, we are making search on left side filters but cList view results will filter on this ajax search :

1 search form   2. js script function with update Clistview grid widget 3. controller action as per values and render view.

1 View page :

– it can be index.php for listing, just add id of your choice to you listing widget

<?php $this->widget(‘zii.widgets.CListView’, array(
‘dataProvider’=>$dataProvider,
‘itemView’=>’_view’,
‘id’=>’slistview’,
)); ?>

2. Js script part , from where you push to start ajax, you can call it on submit button or on any link etc.

<?php
Yii::app()->clientScript->registerScript(‘searchlist’, ”

$(‘#search’).submit(function(){
$.fn.yiiListView.update(‘slistview’, {
data: $(‘#search’).serialize()+’&isAjaxRequest=1′
});
return false;
});
“);

3. Controller submit action handler and check if request is ajax

public function actionIndex()
{
//print_r($_GET);
$cid=(isset($_POST[‘cat’]) && is_numeric($_POST[‘cat’])) ? trim($_POST[‘cat’])  : ”;
$this->layout=’//layouts/column1′;

$criteria=new CDbCriteria;
$criteria->select=’t.*’;

if(!empty($cid))
{
$criteria->join=’JOIN product_category pc ON pc.pid=t.id’;

$criteria->condition=’pc.cid=’.$cid;

}

if(isset($_GET[‘isAjaxRequest’]) && $_GET[‘isAjaxRequest’]==’1′) {
$cid=(isset($_GET[‘cat’]) && is_numeric($_GET[‘cat’])) ? trim($_GET[‘cat’])  : ”;

if($cid)
{
$criteria->join=’JOIN product_category pc ON pc.pid=t.id ‘;
$criteria->condition=’pc.cid=’.$cid;
}
}

$dataProvider=new CActiveDataProvider(‘Product’,array(‘criteria’=>$criteria));

$this->render(‘index’,array(
‘dataProvider’=>$dataProvider,
));

}

 

 

 
?>

contact us