繁体   English   中英

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

[英]In Yii grouped dropDownList, how to set default selected item?

情况:
我在dropDownList对象中指定“选定的项目”时遇到麻烦。 我正在使用具有多个optgroup项的分组dropDownList,每个项都包含许多选择项。 挑战在于设法找到一种方法来指定默认的选定项目,因为如上所述,每个项目都在一个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>
//...

解决了。

只需在调用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>
//...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM