问题描述
我有一个问题模型和答案模型,其中要回答的问题是一对多关系。如何在Yii2中实现复选框列表,使其以如下形式显示:
i have a Questions model and Answers model in which Question to Answer is one-many relation. How do i implement checkboxlist in Yii2 so that it will display in a form as follows:
Form:
Question 1 description
[checkbox] Answer 1 description
[checkbox] Answer 2 description
Question 2 Description
[checkbox] Answer 3 description
[checkbox] Answer 4 description
Question 3 Description
[checkbox] Answer 5 description
[checkbox] Answer 6 description
[Save button]
当我单击保存时,例如。已选中答案1和答案3,帖子应以字符串形式返回 1,3。我该如何使用复选框列表?我尝试使用我的代码,但是现在,如果选中,则仅捕获答案5和答案6的值。如果选中答案4的答案1,则不会捕获它们。
When i click save and for eg. Answer 1 and Answer 3 is checked, post should return "1,3" in a string. How do i do that using checkboxlist? I tried with my codes but right now it only captures values of Answer 5 and Answer 6 if they are checked. If Answer 1 to Answer 4 is checked, it they are not captured. Should be something wrong with my loop.
希望有人可以帮助我。.
非常感谢
Hope someone can help me please..Thanks alot
Updated:我重组了代码,但仍然只能从属于上一个问题的答案中捕获值:
Updated:I restructured my codes but still it only captures values from the answers belonging to last question:
<?php
$form = ActiveForm::begin([
'id' => 'form',
'type' => ActiveForm::TYPE_HORIZONTAL,
]);
?>
<?php
$qnamodel = $questionmodel->joinWith('answermodel')->all();
$selected_array = array_map('intval', explode(',', $selected_list));
foreach($qnamodel as $num=>$per_qn)
{
echo '<div class="form-group"><div class="col-sm-9">'.$per_qn->QN_DESC.'</div></div>';
foreach($per_qn->answermodel as $per_ans)
{
$arr_list[$per_qn->QA_TABLE_ID][$per_ans->QA_ANS_TABLE_ID] = $per_ans->ANS_DESC;
}
$recordmodel->ANS_TABLE_ID = $selected_array;
echo $form->field($recordmodel, 'ANS_TABLE_ID', ['template' => "{label}\n{input}\n{hint}"])->checkboxList($arr_list[$per_qn->QA_TABLE_ID], $selected_array, ['separator' => '<p>'])->label(false);
echo '<p><br />';
}
?>
<?= Html::submitButton('save', ['class' => 'btn btn-primary']) ?>
<?phpgmst
ActiveForm::end();
?>
In Question Model
public function getAnswermodels()
{
return $this->hasMany(Answermodel::className(), ['QN_TABLE_ID' => 'QA_TABLE_ID']);
}
In Answer Model
public function getQNTABLE()
{
return $this->hasOne(Questionmodel::className(), ['QA_TABLE_ID' => 'QN_TABLE_ID']);
}
推荐答案
如果我了解您正确查看逻辑,您应该能够执行以下操作。我输入了通用的字段名和表名,以使代码更易于阅读。我还删除了其他一些代码,以使您更轻松地了解发生了什么。这应该替换视图的整个已发布代码;
If I've understood you view logic correctly, you ought to be able to do something like this. I've put in generic field names and table names to make the code a bit easier to read. I've also stripped out some of the other code to make it easier to see what's going on. This should replace the whole of your posted code for the view;
foreach ($model->questions as $question) {
echo '<p>' . $question->description . '</p>';
if ($question->answers) {
echo $form->field($question, 'answers[' . $question->id . '][]')->checkboxList(yii\helpers\ArrayHelper::map($question->answers, 'id', 'description'), ['separator' => '<p>']);
}
}
第一部分遍历所有可能的问题模型。如果该问题有答案,那么它将显示这些答案的复选框列表。 arrayHelper是一种从模型数组中获取值数组的简便方法。给定一个模型数组,您可以使用它来获取一个由id索引的数组,并带有描述中的标签。
This first part loops through all the possible questions for the model. If that question has answers, then it displays a checkbox list for those answers. The arrayHelper is an easy way of getting an array of values from an array of models; given an array of models, you use this to get an array indexed by id, with labels from the description.
请务必注意,复选框列表会将数据提交为数组,因此属性名称必须是数组。因此,复选框列表的属性必须采用 answers [形式。 $ question-> id。 ’] []’。提交表单后,您将获得一个名为answer的属性,该属性带有问题ID的索引,以及为该问题选择的答案的子数组。这样,您将知道选择了哪个问题的答案。我希望这会有所帮助!
It's really important to note that checkboxlist will submit the data as an array, so the name of the attribute needs to be an array. So the attribute for the checkboxlist needs to be of the form 'answers[' . $question->id . '][]'. When the form is submitted you will get an attribute named answer, with an index of the question id, with a sub-array of the answers selected for that question. This way you will know what question the answer has been selected for. I hope that this helps!
这篇关于将Yii2.0复选框用于父子关系模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!