簡體   English   中英

Yii2:從列表視圖中獲取數據以記錄活動記錄

[英]Yii2: get data from list view to record with active record

我正在創建一個問卷,我正在嘗試記錄獲得的答案,我在listview顯示從一個表(問題)獲得的問題,我想使用active record將它們記錄在另一個表(答案)中。 我試圖在 itemView 類中添加活動表單,但是每次我有 1 個以上的問題時,發送按鈕都會出現重復,然后我嘗試將它添加到 itemView 之外,如果提交按鈕只出現一次但我無法得到它是 itemView 中列出的數據,因為我不知道如何發送活動表單的字段以從 itamView 獲取數據,我嘗試通過 itemView 的渲染發送它們,但它拋出了未定義的變量錯誤。

看法

<?php $form = ActiveForm::begin([
'enableClientValidation' => false,
'enableAjaxValidation' => true,]) ?>

<?= ListView::widget([
    'layout' => '<div class="pull-left">{items}</div>',
    'dataProvider' => $dataProvider,
    'itemView' => function ($model, $key, $index, $widget) {
        return $this->render('_answers',[
            'model' => $model, 
            'index' => $index
        ]);
    },
]); ?><div class="form-group">
<?php echo Html::submitButton('<span class="fa fa-plus"></span>'.' '.Yii::t('backend', 'Send') , ['class' => 'btn btn-primary']) ?>

查看_answers

<td width="5%" class="vcenter" rowspan="3">
    <span class="panel-title-address"><label class="control-label">Nr: <?php echo ($index+1); ?></label></span>
</td>
<td width="95%" class="vcenter">
        <div class="form-group field-qquestion-0-title required">
            <label class="control-label" for="qquestion-type_id"><?= Yii::t('backend', 'Question'.' : ')?></label>
        </div>  
        <div class="form-group field-qquestion-0-title required">
            <label class="control-label" for="qquestion-type_id"><?= $model->question ?></label>
        </div>
    <div class="col-md-4">
        <?php echo $form->field($answer, 'answer')->textInput(['maxlength' => true]) ?>
    </div>
</td>

我想獲得的是每個問題的idanswer ,以便能夠將它們注冊到答案表中。

像這樣更改您的答案輸入,您將獲得每個問題的答案輸入:

<?php echo $form->field($answer, '['.$model->id.']answer')->textInput(['maxlength' => true]) ?>

當您提交表格時,它將與所有回答問題一起提交。 所以你可以像這樣檢查並保存$_POST

if(isset($_POST['Answer']) and !empty($_POST['Answer'])){
    foreach($_POST['Answer'] as $question_id => $answer){
        //save your answer to your question
    }
}

你也必須像這樣 foreach 改變你的ajaxvalidation

您收到 undefined variable 錯誤是因為您沒有將$form變量從主視圖傳遞給_answers.php 你可以像這樣傳遞它:

<?php $form = ActiveForm::begin([
'enableClientValidation' => false,
'enableAjaxValidation' => true,]) ?>

<?= ListView::widget([
    'layout' => '<div class="pull-left">{items}</div>',
    'dataProvider' => $dataProvider,
    'itemView' => function ($model, $key, $index, $widget) use ($form) {
        return $this->render('_answers',[
            'form' => $form,
            'model' => $model, 
            'index' => $index,
        ]);
    },
]); ?><div class="form-group">
<?php echo Html::submitButton('<span class="fa fa-plus"></span>'.' '.Yii::t('backend', 'Send') , ['class' => 'btn btn-primary']) ?>

至於如何使用問題 id 發送多個答案,您可以使用vvpanchev 的答案中提到的方式,也可以添加帶有問題 id 的隱藏字段。 帶有隱藏字段的_answers.php視圖:

<td width="5%" class="vcenter" rowspan="3">
    <span class="panel-title-address"><label class="control-label">Nr: <?php echo ($index+1); ?></label></span>
</td>
<td width="95%" class="vcenter">
        <div class="form-group field-qquestion-0-title required">
            <label class="control-label" for="qquestion-type_id"><?= Yii::t('backend', 'Question'.' : ')?></label>
        </div>  
        <div class="form-group field-qquestion-0-title required">
            <label class="control-label" for="qquestion-type_id"><?= $model->question ?></label>
        </div>
    <div class="col-md-4">
        <?php
            //set the question's id to answer model if you haven't done that already
            $answer->question_id = $model->id;
            //output the hidden input with question id
            echo \yii\helpers\Html::activeHiddenInput($answer, "[$index]question_id"); 
        ?>
        <?php echo $form->field($answer, "[$index]answer")->textInput(['maxlength' => true]) ?>
    </div>
</td>

如果您使用隱藏字段方法,那么在您的控制器中,您可以使用\\yii\\base\\Model::loadMultiple()方法將數據加載到答案模型中。 您還可以使用\\yii\\base\\Model::validateMultiple()進行驗證。 我假設答案模型類的名稱是Answer

$count = count(\Yii::$app->request->post('Answer', []));
$answers = [];
for ($i = 0; $i < $count; $i++) {
    $answers[] = new Answer();
}
if (
    \yii\base\Model::loadMultiple($answers, \Yii::$app->request->post())
    && \yii\base\Model::validateMultiple($answers)
) {
    // ... save your answers and/or do other things needed
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM