A
A
AUN2015-04-03 10:39:15
JavaScript
AUN, 2015-04-03 10:39:15

Yii2 GridView and PJAX. How to setup?

Hey!
There is a gridView, information is displayed, there are buttons to edit, delete. It is necessary that the removal pass through ajax. Blogs have written about using pjax. I tried to use it - there is no ajax at all. Now, when you click on the delete button, you IMMEDIATELY follow the link. What to do?
Spread the full text of the view.

<h1>Выпуски</h1>
<?php
use yii\grid\GridView;
use yii\data\ActiveDataProvider;
use yii\grid\ActionColumn;
use yii\helpers\Html;

$dataProvider = new ActiveDataProvider([
    'query' => $model,
    'pagination' => [
        'pageSize' => 20,
    ],
]);

echo newerton\fancybox\FancyBox::widget([
    'target' => 'a[class=fancyboxWindow]',
    'config' => [
        'maxWidth' => '90%',
        'maxHeight' => '90%',
        'playSpeed' => 7000,
        'padding' => 0,
        'fitToView' => false,
        'width' => '70%',
        'height' => '70%',
        'autoSize' => false,
        'closeClick' => false,
        'openEffect' => 'elastic',
        'closeEffect' => 'elastic',
        'prevEffect' => 'elastic',
        'nextEffect' => 'elastic',
        'closeBtn' => false,
        'openOpacity' => true,
    ]
]);

\yii\widgets\Pjax::begin([
    'timeout' => 10000, // пробовал поиграться с параметрами, результата никакого
    'enablePushState'=>false
]);
echo GridView::widget([
    'dataProvider' => $dataProvider,
    'columns'=>[
        'id',
        'name',
        [
            'class' => 'yii\grid\DataColumn', // can be omitted, default
            'header'=>'Тип',
            'value' => function ($data) {
                    return $data->editionType->name;
                },
        ],
        [
            'format' => 'html',
            'value' => function($data) {
                    $name='/uploads/'.$data->img;
                    return Html::a(Html::img($name, ['width'=>'100px']), $name, ['class' => 'fancyboxWindow']);
                }
        ],
        'create',
        'update',
        [
            'class' => 'yii\grid\DataColumn', // can be omitted, default
            'header'=>'Автор',
            'value' => function ($data) {
                    return $data->user->name;
                },
        ],
        'year',
        'format',
        [
            'class' => ActionColumn::className(),
            'template' => '{update} {delete}',
            'buttons' => [
                'update' => function ($url, $model) {
                        return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, [
                            'title' => 'Редактировать',
                        ]);
                    },
                'delete' => function ($url, $model) {
                        return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
                            'title' =>'Удалить',
//                            'data-confirm'=>"Хотите удалить?",
                            'data-pjax'=>'1'
                        ]);
                    },
            ],

            'urlCreator' => function ($action, $model, $key, $index) {
                    if ($action === 'update') {
                        $url ='/panel/adverts/edition/update/'.$model->id;
                        return $url;
                    }
                    if ($action === 'delete') {
                        $url ='/panel/adverts/edition/delete/'.$model->id;
                        return $url;
                    }
                }
        ]
    ],
]);
\yii\widgets\Pjax::end();
?>
<a href="/panel/adverts/edition/create" class="btn btn-success">Создать</a>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
Zohei, 2015-04-09
@Zohei

add the data-pjax attribute to your links and change format to raw for columns

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question