Yii2: Updating Grid-view using Pjax

痞子三分冷 提交于 2019-11-28 10:33:12

try to explain how to do it as a widget; it's a generic solution, so contact me in case of troubles:

Controller (@your-alias/controllers/yourController):

...
public function actionManage($param=''){
    $model = new YourModel();

    if (Yii::$app->request->isPjax && $model->load(Yii::$app->request->post()) && $model->save())
    {
        $model = new YourModel(); //reset model
    }
    $model->paramId = $param;
    $queryParams = Yii::$app->request->getQueryParams();
    $queryParams['YourModelSearch']['param'] = $param;
    $searchModel = new YourModelSearch();
    $dataProvider = $searchModel->search($queryParams);

    return $this->renderAjax('@your-alias/widgets/views/index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
        'model' => $model,
    ]);
}...

widgets (@your-alias/widgets/) [form, view]:

_form:

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\widgets\Pjax;

/**
 * @var yii\web\View $this
 * @var yourModule/models/YourModel $model
 * @var yii\widgets\ActiveForm $form
 */
?>
<?php
$js = <<<JS
// get the form id and set the event
$('form#{$model->formName()}').on('beforeSubmit', function(e) {
   var \$form = $(this);
   // do whatever here, see the parameter \$form? is a jQuery Element to your form
    console.log(\$form);
    console.log("MODEL CODE = " + $("#yourmodel-code").val());
}).on('submit', function(e){
    e.preventDefault();
});
JS;
//$this->registerJs($js);
$this->registerJs(
   '$("#new-your-model").on("pjax:end", function() {
        commonLib.divAction("#div_new_model", "hide"); //hide form
        $.pjax.reload({container:"#models"});  //Reload GridView
    });', \yii\web\View::POS_READY
);
?>

<div class="model-form">

    <?php Pjax::begin(['id' => 'new-model', 'timeout' => false, 'enablePushState' => false]) ?>
        <?php $form = ActiveForm::begin([
            'id' => $model->formName(),
            //'method' => 'post',
            'action' => ['/module/controller/manage?param='.$model->code],
            'options' => ['data-pjax' => true ],
            //'layout' => 'default',
            ]); ?>

        <?= $form->field($model, 'code')->textInput(['maxlength' => 255]) ?>

        ...


        <div class="form-group">
            <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
        </div>

        <?php ActiveForm::end(); ?>
    <?php yii\widgets\Pjax::end() ?>

</div>

index view (grid view):

use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;

/**
 * @var yii\web\View $this
 * @var yii\data\ActiveDataProvider $dataProvider
 * @var yourModule\models\search\YourModelSearch $searchModel
 */
?>
<div class="model-index">

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

    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <p>


        <?= Html::button(Yii::t('bp', 'Add ...'), [
            'class' => 'btn btn-success',
            'onclick'=>'js:commonLib.divAction("#div_new_model", "show")'
        ])?>
    </p>

    <div id="div_new_model" style="display:none">
        <?= Html::button(Yii::t('common', 'Cancel'), [
            'class' => 'btn btn-success',
            'onclick'=>'js:commonLib.divAction("#div_new_model", "hide")'
        ])?>

        <!-- Render create form -->
        <?= $this->render('_formModel', [
            'model' => $model,
        ]) ?>
    </div>

<?php Pjax::begin(['id' => 'models', 'timeout' => false, 'enablePushState' => false]) ?>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            ...

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>
<?php Pjax::end() ?>
</div>

widget call (in view):

echo @your-alias\widgets\YourWidget::widget([
'param' => $model->param,]);

$.pjax.reload('#my-grid-pjax' , {timeout : false});

Robin Thankachan Plavila

To update GridView table without page reload using pjax:

use yii\grid\GridView;
use yii\widgets\Pjax;

Pjax::begin(['id' => 'todaysactivity', 'timeout' => false, 'enablePushState' => false])

Use jQuery/JavaScript as follows:

var url = baseurl + '/activity/logging'; // url where the gridview table need to update
$.pjax.reload({container: "#todaysactivity", url: url}); // refresh the grid

I couldn't find a suitable solution to update the grid-view widget using pjax. I resolved by using a auto-refresh method for the grid-view page. I understand that this is not the best solution and I am still looking for a suitable solution.

The method I have used is like this:

<script>
function autoRefresh()
{
   window.location.reload();
}

setInterval('autoRefresh()', 60000); // this will reload page after every 1 minute.
</script>

What you're looking for is

<script type="text/javascript">
$.pjax.defaults.timeout = false;
</script>

The default pjax timeout setting doesn't give the page time to do anything so it reloads.

Reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!