如何在表单加载时检查Yii2.0 CheckboxList项目? [英] How to have Yii2.0 CheckboxList items as checked as the form loads?
问题描述
我正在尝试在YII 2.0 Framework中使用Activeform复选框列表小部件
I am trying to use a Activeform checkboxlist widget in YII 2.0 Framework
在我的情况下,我有一个名为列表"的数组,该数组具有将显示为复选框列表的语言名称,现在我可以做到这一点,但是我希望在加载表格时选中复选框
in my case, i have an array called "list" which has the names of languages which will be rendered out as checkboxlist, now i am able to do that, but i want the check boxes to be checked as the form loads.
$list = [0 => 'PHP', 1 => 'MySQL', 2 => 'Javascript'];
$list2 = [0,2];
使用以下行,我能够使用HTML帮助器类实现所需的功能:
using the following line, i am able to acheive what i want using HTML helper classes :
<?= Html::checkboxList('CuisineId',$list2,$list); ?>
但是我希望能够使用Activeform Widget CheckboxList来做到这一点,它按照
but i want to be able to do this using the Activeform Widget CheckboxList, which as per documentation is to be used in the following way :
static checkboxList($ items,$ options = [])
static checkboxList( $items, $options = [] )
因此,在我的情况下,我已经知道如何通过以下方式为$ items传递参数:
So in my case, i have figured how to pass the parameter for $items, which is in the following way:
<?= $form->field($record, 'CuisineId')->checkboxlist($list);?>
但是现在我不知道如何传递将允许选中复选框的参数.
But now i do not know how to pass the parameters that will allow the checkboxes to be checked.
推荐答案
The solution was just to add the following line of code and it worked! :
$record->CuisineId = $list2;
这篇关于如何在表单加载时检查Yii2.0 CheckboxList项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!