【问题标题】:pre-selected items in CActiveForm->checkBoxList in YiiYii 中 CActiveForm->checkBoxList 中的预选项目
【发布时间】:2012-05-14 03:32:47
【问题描述】:

我有两个数组。一个是 $categories ,其中包含从我的数据库中检索到的所有类别,另一个是 $preSelectedCategories ,其中包含需要在我的复选框列表中预先选择的类别表单已加载。我试着这样做:

<?php echo $form->labelEx($model,'category_id'); ?>
<?php echo $form->checkBoxList($model, 'category_id', $categories, $preSelectedCategories, array('multiple'=>true)); ?>
<?php echo $form->error($model,'category_id'); ?>

但我没有成功。谁能帮我解决这个问题?谢谢!

编辑:我已经知道使用 CHtml::checkBoxList 会有所帮助,但我在这里想要使用 CActiveForm::checkBoxList 因为我正在使用模型验证复选框列表。

【问题讨论】:

    标签: yii selecteditem checkboxlist


    【解决方案1】:

    一种选择是使用CHtml::activeName 为输入获取适当的名称并将其传递给CHtml::checkBoxList,就像其他人建议的那样。

    在我看来,另一个更合适的选择是在呈现表单之前将您想要预先检查的category_ids 添加到控制器中的模型中(仅当它是一个 GET 请求时)。那么你根本不需要修改表单。

    【讨论】:

    • 我不知道如何在控制器中设置预先检查的category_ids。可以举个例子吗?
    • 在控制器操作中调用$this-&gt;render(); 之前,您是否尝试过$model-&gt;category_id = array_merge((array)$model-&gt;category_id, $preSelectedCategories);?我不知道您如何保存这些值以及如何在模型上定义该字段,因此这可能不起作用,但这是一般的想法。 (如果控制器和模型代码不起作用并且您需要更多帮助,请将其添加到您的问题中。)
    • 谢谢,您的解决方案效果很好!我忘了我们可以在渲染视图之前为模型属性设置值,我的错!
    【解决方案2】:

    您可以轻松地使用选定的项目预先填充 checkBoxList,它的第二个参数中包含一组选定的键。

    $selected_keys = array_keys(CHtml::listData( $model->books, 'id' , 'id'));
    
    echo CHtml::checkBoxList('Author[books][]', $selected_keys, $books);
    

    请在我的博客上查看完整示例:

    http://scriptbaker.com/how-to-make-yii-checkboxlist-selected/

    【讨论】:

    • 感谢您发布您的答案!请务必仔细阅读FAQ on Self-Promotion。另请注意,每次链接到您自己的网站/产品时,都要求发布免责声明。我已为您添加了披露;以后请务必自己处理!
    • @AndrewBarber 感谢您的评论,您的意思是如果我引用我自己博客中的 URL,我应该明确告诉它来自我的博客?并添加诸如“在我的博客上”之类的词?
    【解决方案3】:

    CHtml::actviceCheckBoxList(您使用的 CActiveForm::checkBoxList 是它的包装器)具有这样的语法

     public static function activeCheckBoxList($model,$attribute,$data,$htmlOptions=array())
    

    如果你想手动设置预选值 - 你应该使用 CHtml::checkBoxList 代替

    public static function checkBoxList($name,$select,$data,$htmlOptions=array())
    

    这是一段 CHtml 类参考

     * @param string $name name of the check box list. You can use this name to retrieve
         * the selected value(s) once the form is submitted.
         * @param mixed $select selection of the check boxes. This can be either a string
         * for single selection or an array for multiple selections.
         * @param array $data value-label pairs used to generate the check box list.
         * Note, the values will be automatically HTML-encoded, while the labels will not.
    

    【讨论】:

    • 我已经知道 CHtml::checkBoxList 可以提供帮助,但问题是我想使用 CActiveForm::checkBoxList 以便我可以使用模型的规则来验证字段。
    【解决方案4】:
    <?php 
    $categories = array(1,2,3);
    $preSelectedCategories = array(1=>true,2=>true); // use this way 
    echo CHtml::checkBoxList('category_id',$preSelectedCategories,$categories); 
    ?>
    

    试试这个我试过了,运行成功。

    【讨论】:

    • 您的解决方案不起作用。据我所知,checkAllLast 是布尔类型,它确定是否应将选中所有复选框放在复选框列表的末尾。我想要的是在页面加载时自动检查一些复选框。
    • 抱歉未经测试的回复,我已经更正了答案,它工作正常。
    • 现在你只是在重复 sl4mmer 的建议。
    • 谢谢,您的解决方案现在有效,但我真正想要的是使用 CActive::checkBoxList 而不是 CHtml::checkBoxList,因为我正在使用模型来验证它。
    【解决方案5】:

    删除 $preSelectedCategories 变量。 将 $model->category_id 设置为包含选定复选框值的数组。

    <?php echo $form->labelEx($model,'category_id'); ?>
    <?php 
    $model->category_id = array('value1','value2');
    echo $form->checkBoxList($model, 'category_id', $categories, array('multiple'=>true)); ?>
    <?php echo $form->error($model,'category_id'); ?>
    

    你应该试试这个,但我没有测试这个。

    【讨论】:

      【解决方案6】:

      在将属性显示在表单上之前,用数组填充属性。

      在控制器中:

      public function actionUpdate($id) {
      
        $model=$this->loadModel($id);
      
        //For example
        $categories = array(0=>'Option One',1=>'Option Two',2=>'Option Three');
        $preSelectedCategories = array(1,2);
      
        //Magic here
        $model->category_id = $preSelectedCategories;
      
        if(isset($_POST['NameOfModel']) {
          //category_id reset with incoming form data...
        }
        ...
        $this->render('update',array('model'=>$model,'categories'=>$categories));
      }  
      

      在视图中,在你的表单中:

      echo $form->checkBoxList($model,'category_id',$categories);
      

      由于我在这里对其进行了修改,因此未经测试...改编自 Bootstrap 扩展的表单小部件。

      【讨论】:

        【解决方案7】:

        大部分 yii 成员都有同样的问题,所以这里有更甜的代码和清晰的解释。

        首先您需要找到您预先选择的类别,例如 -

        $criteria = new CDbCriteria();
        $criteria->select = 'category_id as id';
        $criteria->condition = 'userid = :userid';
        $criteria->params = array(':userid' => Yii::app()->user->id);
        
        //store pre-selected id into variable  - $selected_keys
        $selected_keys = array_keys(CHtml::listData(MyCategory::model()->findAll($criteria), 'id', 'id'));
        

        现在生成整个类别列表,比如 -

        $list = CHtml::listData(Categories::model()->findAll(array('order'=>'id')), 'id', 'category_name');
        
        //htmlOptions for class and others elements
        $htmlOptions = array('template' => '{input}{label}', 'separator'=>'', 'class'=>'in-checkbox', 'multiple'=>true, 'checked'=>'checked');
        

        查看部分 -

        <?php echo $form->labelEx($model, 'Category', array('class'=>'col-md-3 control-label')); ?>
        <?php $model->Category = $selected_keys; //assign pre-selected list to Category list
              echo $form->checkBoxList($model, 'Category', $list, $htmlOptions); ?>
        <?php echo $form->error($model, 'Category'); ?>
        

        试试这个,效果很好..

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多