在Yii分组的dropDownList中,如何设置默认选中的项目?

时间:2012-11-11 16:05:49

标签: php html drop-down-menu yii

情况:
我在dropDownList object中指定“所选项目”时遇到了一些问题。我正在使用具有多个optgroup项目的分组dropDownList,每个optgroup项目包含许多选择项目。挑战是试图找到一种方法来指定默认的选定项目,因为如上所述,每个项目都在一个optgroup中。

问题:
使用分组表单时如何设置默认选定项?或者为什么下面的代码不起作用?

部分代码:

<?php
// retrieve the parent categories first
$parents = Categories::model()->findAllByAttributes(array('idCategoryParent'=>0));
// prepare an array to hold parent categories as optgroups, and their child categories as selectable items
$categories = array("Select a Category Below");
// prepare an array to hold the location of the selected item (if any)
$selectedItemInfo = array();


foreach($parents as $parent) {

    // retrieve the sub categories for this parent category
    $children = Categories::model()->cache(Yii::app()->findAllByAttributes(array('idCategoryParent'=>$parent->idCategory));

    // prepare an array to temporarily hold all sub categories for this parent category
    $categoriesChildren = array();

    // iterate over sub categories
    foreach($children as $child) {

        // Assign the category name (label) to its numeric id
        // Example. '10' => 'Coffee Mugs'
        $categoriesChildren[$child->idCategory] = $child->label;

        /**** Important part
        * we may on occasion have an 'id' as a url parameter
        * if 'id' isset, and 
        * if the current sub category idCategory matches 'id'
        * mark this sub category as the item we want selected in our dropDownList
        */
        if(isset($_GET['id']) && $child->idCategory == $_GET['id']){
            // store the location of the current sub category
            $selectedItemInfo[0] = $parent->label;
            $selectedItemInfo[1] = $child->idCategory;
        }
    }

    /*** Another Important part 
    * here we assign the whole child categories array 
    * as a single element in our categories array
    * that can be accessed by the category name (label)
    * of the parent category. 
    * Passing this entire $categories array to our
    * dropDownList will make it render as a grouped
    * dropDownList with the groups as optgroup items.
    */
    // Example. 'Kitchenware' => {array}
    $categories[$parentLabel] = $categoriesChildren;
}

?>

<?php

// get our selected item
/**** Remember
* The element $selectedItemInfo[0] holds our parent category's string name (label)
* The element $selectedItemInfo[1] holds our child category's id
*/
$selectedItem = $categories[$selectedItemInfo[0]][$selectedItemInfo[1]];

// create an options array to inform dropDownList of our selected item
$options = array('options'=>array($selectedItem=>array('selected'=>true)));

?>

//...
<div class="row">
<?php echo $form->labelEx($model,'idCategory'); ?>
<?php echo $form->dropDownList($model,'idCategory', $categories, $options); ?>
<?php echo $form->error($model,'idCategory'); ?>
</div>
//...

1 个答案:

答案 0 :(得分:1)

<强>解决。

只需在调用dropDownList代码之前设置$ model-&gt; idCategory即可。像这样:

//...
<div class="row">
<?php echo $form->labelEx($model,'idCategory'); ?>
<?php $model->idCategory = $_GET['id']; ?>
<?php echo $form->dropDownList($model,'idCategory', $categories); ?>
<?php echo $form->error($model,'idCategory'); ?>
</div>
//...