Friday, January 14, 2011

Creating an instant search box for the CListView

We have our index page, with a CListView displaying a list of items, in this case contacts.

We want a single "instant" search box, that will filter the contacts list instantly.

Your index.php view page should look something like this:

array('label'=>'Create Contact', 'url'=>array('create')),
array('label'=>'Manage Contact', 'url'=>array('admin')),

//this adds a javascript event listener to the search box that will query the server with each keystroke. yw0 is the id of the clistview. q is the id of the text input box:
Yii::app()->clientScript->registerScript('search', "
  $.fn.yiiListView.update('yw0', {
  data: $(this).serialize()
  return false;


<!-- add a search box: -->
<input type="text" id="q" name="q" />

<?php $this->widget('zii.widgets.CListView', array(
)); ?>
Change your actionIndex function in your ContactController to look something like this:
public function actionIndex() {
  $model = new Contact($scenario='search');
  $model->first_name = $_GET['q'];

  //add the ->search() call: 

Now when you type in the field, it should filter the contacts by their first name.

  • change onkeyup so it fires with a few characters and after a small delay
  • add a new search() function that will search several attributes at once using "OR" instead of "AND". This can be done in the compare() function with the 4th argument.
  • Some heavy caching on the server so that the database is not overwhelmed.


  1. show error => Undefined index: q

  2. you have to choose isset($_GET['q']) method , otherwise it show undefine q error :)

  3. Wow this is great. thanks very much for this tutorial..... really really helpful. :)

  4. I get some empty alert boxes popping up whenever I click on the objects returned from the search. I dont alert anything in my code, and this only occurs if I click on a returned result immediately after searching. has this issue ever come up for you?

  5. Is it possible to add more than one of these boxes in order to filter by more than one element of the table?

  6. Really nice job
    I have a book table and a tags table. I would like to be able to search by tags. Could you help me please, how could I do that?

  7. Hello, your tutorial really help me, thank you very much...
    Now, how to combine "criteria" and "pagination"??

    I'm trying this, but not work:
    $criteria = new CDbCriteria(array(
    'order' => 'id DESC',

    $dataProvider=new CActiveDataProvider('TblBarang', array(


    Please, help me...

  8. It's ok, i find the solution. This is my code at index.php:

    pagination = array('pageSize' => 5);
    $dataProvider->sort = array('defaultOrder' => 'tgl_upload DESC');
    'summaryText' => '', //untuk menghilangkan "Displaying 1 sd 10"
    'tgl_upload'=>'Update Terbaru',

    Thank you. :)
    Or do you have another solution??