Yii2 Pjax GridView的操作按钮的问题 [英] Yii2 Pjax GridView action buttons issue
本文介绍了Yii2 Pjax GridView的操作按钮的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图让一个Ajax的GridView使用Pjax。一切工作正常,除了查看,更新和删除按钮都没有AJAX。在code是:
I am trying to make an Ajax GridView using Pjax. Everything is working fine except the view, update and delete buttons are not AJAX. The code is:
<?php yii\widgets\Pjax::begin(['id' => 'demo']); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'name',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php yii\widgets\Pjax::end(); ?>
现在的问题是,对于删除,查看和更新环节都有属性数据pjax = 0即禁用AJAX功能。我找不到如何设置太数据pjax = 1。
The problem is that the links for delete, view and update have the attribute data-pjax=0 which disables AJAX functionality. I cant find out how to set it too data-pjax=1.
推荐答案
您必须做到如下图所示:
You must do like below:
对于删除操作
1 - 改变你的删除操作如下图所示:
1- Change your delete action like below:
public function actionDelete($id) {
$this->findModel($id)->delete();
if (Yii::$app->getRequest()->isAjax) {
$dataProvider = new ActiveDataProvider([
'query' => ModelName::find(),
'sort' => false
]);
return $this->renderPartial('index', [
'dataProvider' => $dataProvider
]);
}
return $this->redirect(['index']);
}
2-在网格视图:
2- In your grid view:
['class' => 'yii\grid\ActionColumn',
'buttons' => [
'delete' => function ($url, $model) {
return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
'title' => Yii::t('yii', 'Delete'),
'data-pjax'=>'w0',
]);
}
]
],
现在,它的工作原理与 Pjax
。
Now, it works with Pjax
.
备注
- 在
我的code deleteAction()
可能会降低性能。你可以写你自己的。 -
W0
通常PJax
的默认密码。您可以添加一个id为PJax
,写在那里吧。 - 这是相同的
更新
和查看
,但你需要改变你的方式展现你的更新
和视图
的意见。 - 在此强烈建议您先看看
Yii2
官方PJax文件:的 http://www.yiiframework.com/doc-2.0/yii-widgets-pjax.html
- My code in
deleteAction()
may decrease performance. You can write your own. w0
usually is the default id ofPJax
. You can add an id toPJax
and write it there instead.- This is the same for
Update
andView
, But you need to change the way you show yourupdate
andview
views. - This is highly recommended to take a look at
Yii2
's official PJax document: http://www.yiiframework.com/doc-2.0/yii-widgets-pjax.html
这篇关于Yii2 Pjax GridView的操作按钮的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文