Yii2:如何在CheckboxList中显示选中的值

时间:2015-04-27 06:29:05

标签: php yii yii2

我想在Yii 2.0中的复选框列表中显示选中的值。以下是我的代码:

主阵列:

<?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',
);

从表中获取数据以显示已检查的项目:

$formFeatureModel = FormFeatureForm::find()->where("form_id=" . $model->id)->all();

$checkedFeatureArr = array();
foreach ($formFeatureModel as $data) {
    $checkedFeatureArr[$data->feature] = $data->feature;
}
?>

复选框字段

<?= $form->field(new FormFeatureForm, 'feature')->checkboxList($featureArr, ['class' => 'featureCls']) ?>

我从数据库中获取$checkedFeatureArr中的检查项目。现在我只想显示检查项目。那么,我应该在哪里传递$checkedFeatureArr数组?陷入困境。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:4)

将其与模型一起使用时,应使用选定的值填充feature模型属性。

$model->feature = $checkedFeatureArr;

然后会自动检查。

其他提示:

  • 最好避免在SQL中连接,将->where("form_id=" . $model->id)替换为->where(['form_id' => $model->id])

  • 在呈现ActiveForm之前,最好先创建ActiveField

相关问题