Ajax search in CListView

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/

http://www.yiiframework.com/forum/index.php?/topic/7414-filter-data-in-clistview-and-cgridview-using-ajax-how/

http://www.yiiframework.com/forum/index.php?/topic/14228-clistview-searching-and-filtering-like-cgridview/

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 :D

Nice solution…

NOTE: moved to proper forum

thank. It’s good.

I’ve made a more detailed and extended wiki

http://www.yiiframework.com/wiki/185/clistview-ajax-filtering/

very nice solution thx!