Yii2搜索表单,无需重新加载页面

时间:2018-10-31 08:06:59

标签: yii2 yii2-basic-app

我有一个带有选项卡布局的页面。我为gridview构建一个搜索表单。但是,每次使用搜索时,它都会重新加载页面,并带我回到第一个选项卡。我该如何解决这个问题?

这是我的代码:

<?php $form = ActiveForm::begin([
     'options' => ['data-pjax' => true ],
    'action' => ['index'],
    'method' => 'get',
    ]); ?>

    Approve month: <input type="string" name="approvemonth"><br><br>
    Team: <input type="string" name="team"><br><br>
    Difficulty: <input type="string" name="difficulty">

    <div class="form-group">
    <?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
    <?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
    </div>

<?php ActiveForm::end(); ?>


    <?php echo GridView::widget([
            'dataProvider' => $dataProvider15,
            'filterModel' => true,  
            'pjax'=>true,
            'panel' => [
                'type' => GridView::TYPE_PRIMARY,
                'heading' => '<h3 class="panel-title"><i class="glyphicon glyphicon-user"></i>Avg total time by modeler without handover</h3>',
            ],
            'columns' => [
                [
                'attribute'=>'approvemonth', 
                'filter' => Html::input('string', 'approvemonth'),
                [
                    'attribute' =>'team',
                    'filter' => Html::input('string', 'team'),
                    // 'group' => true,
                ],
                [
                    'attribute' =>'difficulty',
                    'filter' => Html::input('string', 'difficulty'),
                    // 'group' => true,
                ],
                [
                    'attribute' =>'Total',
                    'format'=>['decimal',2]
                ],
                [
                    'attribute' =>'Avg',
                    'format'=>['decimal',2]
                ],
            ]
        ]); 
        ?> 

我尝试了pjax,但是没有用。它没有加载任何东西。

请帮助我。

谢谢。

1 个答案:

答案 0 :(得分:1)

那是因为您没有将表格包装在

<?php Pjax::begin()?>
<?php Pjax::end()?>

,尽管结果显示正确,但是每次尝试搜索时页面都会重新加载。

为表单的data-pjax=>1提供options是不够的,您需要将表单保留在Pjax的begin()end()部分中。因此,将代码更改为以下内容。

<?php \yii\widgets\Pjax::begin();?>
<?php $form = ActiveForm::begin([
    'options' => ['data-pjax' => true],
    'action' => ['index'],
    'method' => 'get',
]);?>

    Approve month: <input type="string" name="approvemonth"><br><br>
    Team: <input type="string" name="team"><br><br>
    Difficulty: <input type="string" name="difficulty">

    <div class="form-group">
    <?=Html::submitButton('Search', ['class' => 'btn btn-primary'])?>
    <?=Html::resetButton('Reset', ['class' => 'btn btn-default'])?>
    </div>

<?php ActiveForm::end();?>


    <?php echo GridView::widget([
    'dataProvider' => $dataProvider15,
    'filterModel' => true,
    'pjax' => true,
    'panel' => [
        'type' => GridView::TYPE_PRIMARY,
        'heading' => '<h3 class="panel-title"><i class="glyphicon glyphicon-user"></i>Avg total time by modeler without handover</h3>',
    ],
    'columns' => [
        [
            'attribute' => 'approvemonth',
            'filter' => Html::input('string', 'approvemonth'),
        ],
        [
            'attribute' => 'team',
            'filter' => Html::input('string', 'team'),
            // 'group' => true,
        ],
        [
            'attribute' => 'difficulty',
            'filter' => Html::input('string', 'difficulty'),
            // 'group' => true,
        ],
        [
            'attribute' => 'Total',
            'format' => ['decimal', 2],
        ],
        [
            'attribute' => 'Avg',
            'format' => ['decimal', 2],
        ],
    ],
]);
?>
<?php \yii\widgets\Pjax::end();?>