[英]Yii2: How to show checked values in CheckboxList
I want to show checked values in my checkboxlist in Yii 2.0. 我想在Yii 2.0中的复选框列表中显示选中的值。 Following is my code:
以下是我的代码:
Main Array: 主阵列:
<?php
$featureArr = array(
'Change Requester' => 'Change Requester',
'Clone Request' => 'Clone Request',
'Suspend Request' => 'Suspend Request',
'In-Process Requests Open in Edit Mode' => 'In-Process Requests Open in Edit Mode',
'Allow On-the-Fly Notifications' => 'Allow On-the-Fly Notifications',
'Additional Comments Field' => 'Additional Comments Field',
'Do Not Validate Draft Requests' => 'Do Not Validate Draft Requests',
'(Web-Only) Allow File Attachments' => '(Web-Only) Allow File Attachments',
);
Getting data from table to display checked items: 从表中获取数据以显示选中的项目:
$formFeatureModel = FormFeatureForm::find()->where("form_id=" . $model->id)->all();
$checkedFeatureArr = array();
foreach ($formFeatureModel as $data) {
$checkedFeatureArr[$data->feature] = $data->feature;
}
?>
Checkbox field 复选框字段
<?= $form->field(new FormFeatureForm, 'feature')->checkboxList($featureArr, ['class' => 'featureCls']) ?>
I am getting checked item in $checkedFeatureArr
from database. 我从数据库中获取
$checkedFeatureArr
检查项目。 Now I just want to show checked items. 现在我只想显示检查项目。 So, Where should I pass
$checkedFeatureArr
array? 那么,我应该在哪里传递
$checkedFeatureArr
数组? Getting stuck in this. 陷入困境。
Any help would be appreciated. 任何帮助,将不胜感激。
When using it with model, you should fill feature
model property with selected values instead. 将其与模型一起使用时,应使用选定的值填充
feature
模型属性。
$model->feature = $checkedFeatureArr;
Then it will be checked automatically. 然后会自动检查。
Additional tips: 其他提示:
It's better avoid concatenation in SQL, replace ->where("form_id=" . $model->id)
with ->where(['form_id' => $model->id])
. 最好避免在SQL中连接,将
->where("form_id=" . $model->id)
替换为->where(['form_id' => $model->id])
。
It's better create ActiveForm
before rendering ActiveField
. 在渲染
ActiveField
之前,最好创建ActiveForm
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.