Yii2 - 同一页面上的表单和相关网格视图

时间:2014-11-02 21:01:56

标签: php yii2

有人可以告诉我如何在YIi2中将gridview渲染为表单页面

我对Yii真的很陌生,并想知道如何实现这一目标。

由于

3 个答案:

答案 0 :(得分:3)

use yii\grid\GridView;

<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'status')->textInput() ?>
<?= GridView::widget([
    'dataProvider' => $itemDataProvider,
    'columns' => [

        ['class' => 'yii\grid\SerialColumn'],
        'start_date',
        'end_date',

        ['class' => 'yii\grid\CheckboxColumn'],
    ],
]); ?>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>

答案 1 :(得分:0)

尝试使用Pjax外部表单标记将modview添加到模态中,这样如果您希望过滤器不会提交主表单 例如

<?php
$form = ActiveForm::begin();
?>
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>

//button for showing modal
<button type="button" class="btn btn-default" data-toggle="modal" data-target="#modal_large">Browse</button>

 <?php ActiveForm::end(); ?>
<div class="modal" id="modal_large" tabindex="-1" role="dialog" aria-labelledby="largeModalHead" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="largeModalHead">Large Modal</h4>
            </div>
            <div class="modal-body">
                <?php
                Pjax::begin([
                    'enablePushState' => false
                ]);
                echo GridView::widget([
                    'dataProvider' => $imageDataProvider,
                    'filterModel' => $imageSearchModel,
                    'columns' => [
                        ['class' => 'yii\grid\SerialColumn'],
                        'field',
                        'created_at:datetime',
                        ['class' => 'yii\grid\ActionColumn'],
                    ],
                ]);
                Pjax::end();
                ?>     </div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">Close</button></div>

答案 2 :(得分:0)

在单页面视图上渲染表单和gridview。

控制器代码

包括您要渲染的参数:

  • $model用于 _form 视图。
  • $searchModel$dataProvider用于 索引 视图。

        public function actionCreate()
        {
            $model = new WorkflowStep;
    
            if ($model->load(Yii::$app->request->post()) && $model->save())
            {
                return $this->redirect([
                    'view', 'step_id' => $model->step_id
                ]);
            }
    
            $searchModel = new WorkflowStepSearch();
            $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
    
            return $this->render('create', [
                'model' => $model,
                'searchModel' => $searchModel,
                'dataProvider' => $dataProvider,
            ]);
        }
    

查看代码

现在在app / views / create.php中我们需要做的就是渲染其他所需的视图并传递预期的参数。

        <?php

        use yii\helpers\Html;

        /* @var $this yii\web\View */
        /* @var $model app\modules\tickets\models\WorkflowStep */

        $this->title = 'Create Workflow Step';
        $this->params['breadcrumbs'][] = ['label' => 'Workflow Steps', 'url' => ['index']];
        $this->params['breadcrumbs'][] = $this->title;

        ?>
        <div class="workflow-step-create">

            <h1><?= Html::encode($this->title) ?></h1>

            <?= $this->render('_form', [
                'model' => $model,
            ]) ?>

            <?= $this->render('index', [
                'searchModel' => $searchModel,
                'dataProvider' => $dataProvider,
            ]) ?>

        </div>