Searock
Searock

Reputation: 6498

How do I load checkboxList using ajax

I have a dropdown list: city.

Now I'm trying to load a checkbox list on selection of the the above dropdownlist using ajax.

For example, if I click on city xyz, it should populate a checkbox list such as Holiday package 1, 2 etc.

I've tried a similar example Creating a dependent dropdown but it dosent work for me even after echoing the listData.

How do I achive this? I'm new to Yii framework and a sample code snippet could help.

Upvotes: 2

Views: 2845

Answers (2)

Sebastien
Sebastien

Reputation: 11

In your view:

<?php echo $form->labelEx($model,'az_dropdown_id'); ?>
<?php echo $form->dropDownList($model, 'az_dropdown_id', CHtml::listData(Controller::model()->findAll(), 'id', 'name'), 
        array(
            'ajax' => array(
            'type'=>'POST', //request type
            'url'=>CController::createUrl('role/dynamicaction'), //url to call.
            'update'=>'#checkboxList_id', //selector to update
       ))); ?>
<div id "checkboxList_id'></div>

In your controller

$data=TheModel::model()->findAll('az_dropdown_id=:az_dropdown_id', 
                  array(':az_dropdown_id'=>(int) $_POST['CurrentController']['az_dropdown_id']));

$data=CHtml::listData($data,'id','name');
echo CHtml::checkboxList('idForCheckboxList','',$data);

Upvotes: 1

Alfredo Castaneda Garcia
Alfredo Castaneda Garcia

Reputation: 1659

The link you gave shows the correct way to do what you want to do. What was the problem? Why doesn't it work for you? Perhaps you could post your implementation and we could find out what was going wrong.

Upvotes: 1

Related Questions