Search code examples
checkboxdrop-down-menuyiiframeworksyii-extensions

Convert DropDownList to CheckBoxList in Yii


Im a beginner with Yii Framework. I have a lot of dropdownList in my search form and i want to convert them to cheboxList but i don't know how to proceed?

For Example this is the view code of one of the DropdownList

. '<div class="row" style="width:100%;">'
        . CHtml::encode('Pet')
        //. CHtml::textField('HotelType', (isset($_GET['HotelType'])) ? $_GET['HotelType'] : '', array('id'=>'HotelType'))

               . CHtml::dropDownList('Pet', 'pet_id', array(
                    ''=>'All',
                    'Y'=>'Yes',
                    'N'=>'No',
            ),array(
                //'options' => (isset($_GET['HotelType'])) ? $_GET['HotelType'] : array('studio'=>array('selected'=>true), array($_GET['HotelType']=>array('selected'=>true)))
                'options' => array($_GET['Pet']=>array('selected'=>true)),
                'style' => 'width:100%;'
            ))
        . '</div>'

what kind of modifications should I do (in view, controller and model if necessary) to transfome the dropdown into a checkbox ?

Thank you


Solution

  • In the view you need to change dropDownList to checkBoxList() http://www.yiiframework.com/doc/api/1.1/CHtml#checkBoxList-detail

    Then in the controller, depending on what you are trying to do with the selection, adjust the method to receive the multiple options.

    Then pass them to the model to either update a DB record or be used in an SQL query to return the data.