0

情况:
我在dropDownList 对象中指定“选定项目”时遇到了一些问题。我正在使用一个分组的 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>
//...
4

1 回答 1

1

解决了。

只需在调用 dropDownList 代码之前设置 $model->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>
//...
于 2012-11-11T23:42:39.103 回答