3

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

    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 selected options while edit: I found how to give selected options. I found here how to do it. First select has 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 to set it's value to 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 chosen value while editing entity is always - primary school. What's wrong? UPDATE @Tristup helped me to set value of first dropdown, but there are two more dependent dropdowns and I have problems with it. Here is my next question

Community
  • 1
  • 1
Sergey Scopin
  • 2,217
  • 9
  • 39
  • 67

1 Answers1

1

The second parameter for the dropDownList is the default selection.

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

Example :

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

here '0' is the value for the default selection.

Hope this will work for you.

Tristup
  • 3,603
  • 1
  • 14
  • 26