如何创建相同表单的动态列表并在Yii2的视图上显示(可以将其视为具有不同信息的同一对象的列表)谢谢。
我在/frontend/views/site/example.php上有以下表格 那种形式我想把它放在一个清单上。
<div class="site-example">
<h1><?= Html::encode($this->title) ?></h1>
<p>Example of a list:</p>
<div class="row">
<div class="col-lg-5">
<?php $form = ActiveForm::begin(['id' => 'form-example']); ?>
<?= $form->field($model, 'email')->textInput(['readonly' => true, 'value' => $email]) ?>
<?= $form->field($model, 'lastname')->textInput(['readonly' => true, 'value' => $lastname]) ?>
<?= $form->field($model, 'phone')->textInput(['readonly' => true, 'value' => $phone]) ?>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>
答案 0 :(得分:0)
假设这是你的controller
。
<?php
namespace frontend\controllers;
use Yii;
use yii\base\Model;
use yii\web\Controller;
use frontend\models\YourForm;
class SiteController extends Controller
{
public function actionYourAction()
{
$forms = [new YourForm, new YourForm, new YourForm];
return $this->render('example', [
'forms' => $forms
]);
}
}
然后您的view
可能低于
<div class="site-example">
<h1><?= Html::encode($this->title) ?></h1>
<p>Example of a list:</p>
<div class="row">
<div class="col-lg-5">
<?php foreach ($forms as $index => $form): ?>
<?php $form = ActiveForm::begin(['options' => ['id' => "form-example-$index"]]); ?>
<?= $form->field($form, "[$index]email")->textInput(['readonly' => true, 'value' => $form->email]) ?>
<?= $form->field($form, "[$index]lastname")->textInput(['readonly' => true, 'value' => $form->lastname]) ?>
<?= $form->field($form, "[$index]phone")->textInput(['readonly' => true, 'value' => $form->phone]) ?>
<?php ActiveForm::end(); ?>
<?php endforeach; ?>
</div>
</div>
</div>