Yii 1.1.3 setting selected value of dependent dropdown list

I have 3 dependent dropdowns on my page for entity creation.

    echo CHtml::dropDownList('OpenLessons[Building]', '', $buildingList,array(
        'ajax' => array(
        'type'=>'POST', 
        'url'=>CController::createUrl('ajax/floorList'), //url to call.
        'update'=>'#OpenLessons_Floor', //selector to update
        ))); 
    echo CHtml::dropDownList('OpenLessons[Floor]','', array(),array(
        'ajax' => array(
        'type'=>'POST',     
        'url'=>CController::createUrl('ajax/roomList'),
        'update'=>'#OpenLessons_Class_ID',
        )));
    echo CHtml::dropDownList('OpenLessons[Class_ID]',$model->Class_ID, array());

      

Now I want to give them the selected options while editing: I found how to give the selected options. I found here how to do it. First select this code:

<select name="OpenLessons[Building]" id="OpenLessons_Building">
<option value="19">primary school</option>
<option value="6">high school</option>
</select>

      

So, I want this value to be in high school, for example.

        echo CHtml::dropDownList('OpenLessons[Building]', '', $buildingList,array(
            'ajax' => array(
            'type'=>'POST', 
            'url'=>CController::createUrl('ajax/floorList'), 
            'update'=>'#OpenLessons_Floor', 
            'options' => array('High school'=>array('selected'=>true)),
//Also tried this 'options' => array('6'=>array('selected'=>true)),
            ))); 

      

And the selected value when editing an object is always elementary school. What's wrong? UPDATE @Tristup helped me set the value of the first dropdown, but there are two more dependent dropdowns and I'm having problems with it. Here is my next question

+3


source to share


1 answer


The second parameter for dropDownList is the default selection.

Chtml::dropDownList($name, $select, $data)
      

Run codeHide result


Example:



$options = array ('0' => 'Select A Value', '1' => 'Tristup','2'=>'Sergey');
echo CHtml::dropDownList('mySelect', '0', $options);
      

Run codeHide result


here '0' is the default value to select.

Hope this works for you.

+1


source







All Articles