Cgridview data based on dropdownlist value

Hi,

I have a dropdownlist with an option selected by default. Below the dropdownlist is the grid (Cgridview) which should display only those records satisfying the condition depending on the dropdownlist value.

How do i get the value of dropdownlist in CActiveDataprovider to set the condition?

By default this is set in your gridview as


'dataProvider' => $model->search(),

And you can see the associated code in your controller and model (if gii / giix) generated

If you are overriding your dataprovider just make sure that you invoke a method by which you define your criteria/conditions.

Thanks enfield but ‘dataProvider’ => $model->search() returns only table values. And my dropdownlist vlaues are hardcoded. so i want to know how can i get the selected value of the dropdownlist in the search() function of the model.

I’m a very newbie in Yii framework. I would like to create a page. Which is when the dropdown list change, the listview/gridview will be change by dropdown value. I already stack as tad. I lost 1-2 days on this issue. please help me.

this is my view

<div class="row">

    &lt;?php





    &#036;records = Company::model()-&gt;findAll();


    &#036;company_list = CHtml::listData(&#036;records, 'id', 'name');


    echo CHtml::dropDownList('company_id','', &#036;company_list,


    array(


    'onchange'=&gt;&quot;&#036;.fn.yiiListView.update('ajaxListView', {url: '&quot;.Yii::app()-&gt;createUrl('department/dynamicsectionlist').&quot;?company_id='+&#036;('#company_id option:selected').val()})&quot;,


    'prompt'=&gt;'Please select a company',


    )); ?&gt;


&lt;/div&gt;

<?php

$this->widget(‘zii.widgets.CListView’, array(

'dataProvider'=&gt;&#036;dataProvider,


'itemView'=&gt;'_view_section',


'id'=&gt;'ajaxListView',

));

?>

This is model

public function search()

{


    // Warning: Please modify the following code to remove attributes that


    // should not be searched.





    &#036;criteria=new CDbCriteria;





    &#036;criteria-&gt;compare('id',&#036;this-&gt;id);


    &#036;criteria-&gt;compare('name',&#036;this-&gt;name,true);


    &#036;criteria-&gt;compare('p_id',&#036;this-&gt;p_id);


    &#036;criteria-&gt;compare('created',&#036;this-&gt;created,true);


    &#036;criteria-&gt;compare('updated',&#036;this-&gt;updated,true);


    &#036;criteria-&gt;compare('company_id',&#036;this-&gt;company_id);





    return new CActiveDataProvider(&#036;this, array(


        'criteria'=&gt;&#036;criteria,


    ));


}

This is Controller

public function actionDynamicsectionlist()

{       


    &#036;company_id = &#036;_POST['company_id'];


    &#036;criteria=new CDbCriteria();


    &#036;criteria-&gt;condition .= 't.id IN (SELECT t2.id, t2.name FROM department t2 WHERE t2.company_id = :company_id)';





    &#036;criteria-&gt;params[':company_id'] = &#036;company_id;


    &#036;dataProvider = new CActiveDataProvider( 'Department', array( 'criteria' =&gt; &#036;criteria, ) );


    &#036;this-&gt;render( 'sectionlist', array( 'dataProvider' =&gt; &#036;dataProvider ) );





}

But it is not working. Please help me.

Regads Thura747

tad you can send parameter from your dropDownlist in GET via ajax or simple get request, and use it in search.