O
O
Oversec2016-09-19 11:58:44
Yii
Oversec, 2016-09-19 11:58:44

How to configure Ajax and GridView to work?

That's the day I rack my brains, how can I do so that a person can change the data in the table (GridView) and the table is automatically updated without reloading the page? Tried with Pjax - didn't work. Is it possible via Ajax?
An example of Pjax code that came out of my
view/index.php

$this->registerJs(
    '$( ".pjax" ).click(function(event) {
   $.pjax.reload({container: "#tasks"});
});

 <?php Pjax::begin(['id' => 'tasks']); ?>
 <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            [
                'class' => 'yii\grid\CheckboxColumn',
            ],
            [
                'attribute' => 'doc',
                'contentOptions' => ['style' => 'font-size: 1.3em !important'],
                'format' => 'raw',
                'filter' => [
                    0 => 'Нету',
                    1 => 'Есть',
                ],
                'value' => function ($model, $key, $index, $column) {
                    $active = $model->{$column->attribute} === 1;
                    return Html::a(
                        $active ? 'Есть' : 'Нету',
                        ['task/doc', 'id' => $model->id],
                        [
                            'class' => 'pjax label label-' . ($active ? 'success' : 'warning'),
                        ]
                    );
                },
            ],
?>

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

conreollers/TaskController.php
public function actionDoc($id)
    {
        $model = Task::findOne($id);
        $model->doc = !$model->doc;
        $model->save(false);

        $this->redirect('index');
    }

The table is updated when clicked, but the data does not change + if this is done in the middle of the table, then it flips up.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2016-09-19
@slo_nik

Tried with Pjax - didn't work. Is it possible via Ajax?

It turns out that you are already using ajax.
In the controller, in action, why did you set the redirect? Where do you get id from? Are you sure he is? Check.
How do you send data to the server? Do you want to hang a click on a table cell?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question