没有重新加载页面的 Yii2 搜索表单

Yii2 search form without reload page

我有一个带有选项卡布局的页面。我为 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,但没有用。它没有加载任何东西。

请帮我解决这个问题。

谢谢。

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

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

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

为表单的 options 提供 data-pjax=>1 是不够的,您需要将表单保留在 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();?>