Yii2:如何在 CheckboxList 中显示选中的值 [英] Yii2: How to show checked values in CheckboxList

查看:41
本文介绍了Yii2:如何在 CheckboxList 中显示选中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to show checked values in my checkboxlist in Yii 2.0. Following is my code:

主阵列:

<?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 数组?陷入困境.

I am getting checked item in $checkedFeatureArr from database. Now I just want to show checked items. So, Where should I pass $checkedFeatureArr array? Getting stuck in this.

任何帮助将不胜感激.

推荐答案

当它与模型一起使用时,你应该用选定的值填充 feature 模型属性.

When using it with model, you should fill feature model property with selected values instead.

$model->feature = $checkedFeatureArr;

然后它会被自动检查.

其他提示:

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

最好在渲染 ActiveField 之前创建 ActiveForm.

It's better create ActiveForm before rendering ActiveField.

这篇关于Yii2:如何在 CheckboxList 中显示选中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆