author: Christian Salazar

licence: NEW BSD


Wiki en EspaƱol:


This widget presents a form containing a set of fields defined on the widget arguments, when user press the 'Find' button an ajax action will be fired, in response the client must select an option obtained from your remote query.


Este widget presenta un formulario al cliente, el cual dispara una consulta ajax con los parametros configurados, devolviendo al cliente una lista de opciones para seleccionar cuando este presiona el boton "Buscar"(Find).

Example Widget:


        //  use this attribute to pass any data readed from your Form.
        //  suppose you have $form->textField($model,'anyattribute');
        //  containing a value to be passed back to your action in order
        //  to perform a search.
        //  in your action this value is passed via 'arg' attribute name
        //  $_GET['arg']  

        'onSuccess'=>"function(data){ $('#logger').html(data); }",
        'onError'=>"function(err){ $('#logger').html('error: '+err);  }",
<div id='logger'></div>
    echo "Example text field receptor:";
    echo CHtml::textField('testValue','');

Your Action:

// in any controller, please be consistent with 'action' parameter on widget.
public function actionEFilterWidgetSample(){
    // 'keys' is an argument sent by EFilterWidget who informs about
    //  each key present in URL argument. is a helper.
    //  is a comma-separated key names string.  
    $keys = explode(",",$_GET['keys']);

    // a value readed from your form referenced by: 'actionInputArgumentID'
    $anyValue = $_GET['arg'];

    // sample array filtered by argument passed by URL
    //  example:  
    //  Person::model()->findByAttributes(array('uid'=>$_GET['uid']));
    //  available keys: $_GET['keys'], will return: "uid,firstname" (ie). 
    $sample = array(
         array('userid'=>'U1', 'name'=>'christian')
        ,array('userid'=>'U2', 'name'=>'anahi')
        ,array('userid'=>'U3', 'name'=>'pedro')
        ,array('userid'=>'U4', 'name'=>'lisbeth')
    //  IMPORTANT:
    //  The widget requires JSON data
    //  please return a value generated by CHtml::listData
    //  and converted to json using: CJSON::encode(...)
    header("Content-type: application/json");
    echo CJSON::encode(CHtml::listData($sample, 'userid', 'name'));