CActiveForm中的预选项目->;Yii中的复选框列表
我有两个数组。一个是$categories,它包含从我的数据库检索到的所有类别;另一个是$preSelectedCategories,它包含加载表单时需要在我的复选框列表中预选的类别。我试着这样做:CActiveForm中的预选项目->;Yii中的复选框列表,yii,selecteditem,checkboxlist,Yii,Selecteditem,Checkboxlist,我有两个数组。一个是$categories,它包含从我的数据库检索到的所有类别;另一个是$preSelectedCategories,它包含加载表单时需要在我的复选框列表中预选的类别。我试着这样做: <?php echo $form->labelEx($model,'category_id'); ?> <?php echo $form->checkBoxList($model, 'category_id', $categories, $preSelectedCate
<?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,因为我使用的是一个模型来验证复选框列表。
CHtml::actviceckboxlist
(您使用的是它的包装器的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::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
在我看来,另一个更合适的选择是,在呈现表单之前(仅当表单是GET请求时),将您希望预先检查的
类别id
添加到控制器中的模型中。然后您就不需要修改表单了。一个选项是,像其他人建议的那样,为输入获取一个合适的名称,并将其传递给CHtml::checkBoxList
在我看来,另一个更合适的选择是,在呈现表单之前(仅当表单是GET请求时),将您希望预先检查的
类别id
添加到控制器中的模型中。这样您就根本不需要修改表单。删除$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'); ?>
您应该试试这个,但我没有测试。删除$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'); ?>
您应该试试这个,但我没有测试。在表单上显示属性之前,先用数组填充属性 在控制器中:
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 extension的表单小部件。在表单上显示属性之前,先用数组填充属性 在控制器中:
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 extension的表单小部件。您可以轻松地用选定项预先填充复选框列表,它在第二个参数中包含选定键的数组
$selected_keys = array_keys(CHtml::listData( $model->books, 'id' , 'id'));
echo CHtml::checkBoxList('Author[books][]', $selected_keys, $books);
请在我的博客上查看完整示例:
您可以轻松地用选定项预先填充复选框列表,它在第二个参数中包含选定键的数组
$selected_keys = array_keys(CHtml::listData( $model->books, 'id' , 'id'));
echo CHtml::checkBoxList('Author[books][]', $selected_keys, $books);
请在我的博客上查看完整示例:
大多数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'); ?>
试试这个,做得很好。大多数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'); ?>
试试这个,效果很好。我已经知道CHtml::checkBoxList可能会有所帮助,但问题是我想使用CActiveForm::checkBoxList,以便使用模型的规则验证字段。我已经知道CHtml::checkBoxList可能会有所帮助,但问题是,我想使用CActiveForm::checkBoxList,以便使用模型的规则验证字段。您的解决方案不起作用。据我所知,
checkAllLast
是一种布尔类型,用于确定是否应将checkall复选框置于复选框列表的末尾。我想要的是在加载页面时自动选中一些复选框。对于未经测试的回复,我很抱歉,我已经更正了答案,效果很好。现在您只是重复sl4mmer的建议。谢谢,您的解决方案现在可以工作了,但我真正想要的是使用cacive::checkBoxList
而不是CHtml::checkBoxList
,因为我正在使用模型来验证它。您的解决方案不起作用。据我所知,checkAllLast
是一种布尔类型,用于确定是否应将checkall复选框置于复选框列表的末尾。我想要的是在加载页面时自动选中一些复选框。对于未经测试的回复,我很抱歉,我已经更正了答案,效果很好。现在您只是重复sl4mmer的建议。谢谢,您的解决方案现在可以工作了,但我真正想要的是使用cacive::checkBoxList
而不是CHtml::checkBoxList
,因为我正在使用模型来验证它。我不知道如何在控制器中设置预先检查的类别id
s。你能给我举个例子吗?你试过$model->category\u id=array\u merge((array)$model->category\u id,$preSelectedCategories)在调用$this->render()之前执行code>代码>在控制器操作中?我不知道您是如何保存这些值的,也不知道该字段是如何在您的模型上定义的,所以这可能不起作用,但这只是一般的想法。(如果问题不起作用且需要mo,请在问题中添加控制器和型号代码。)