Форма поиска Yii2 без перезагрузки страницы - PullRequest
0 голосов
/ 31 октября 2018

У меня есть страница с макетом вкладки. Я строю форму поиска для 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 голосов
/ 01 ноября 2018

Это потому, что вы не завернули свою форму в

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

и, следовательно, ваша страница перезагружается каждый раз, когда вы пытаетесь выполнить поиск, хотя результаты отображаются правильно.

Предоставление data-pjax=>1 для options формы недостаточно, вам нужно хранить форму внутри begin() и end() разделов Pjax. Поэтому измените код на следующий.

<?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();?>
...