иго*_*ь88 6 php crud pjax yii2
我正在尝试使用带有删除按钮的Pjax创建Ajax GridView.删除没有Ajax.我是Yii2的新手,所以任何帮助都将不胜感激.谢谢.
的index.php
<?php Pjax::begin(['id' => 'countries']) ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'title',
['class' => 'yii\grid\ActionColumn',
'buttons' => [
'delete' => function ($url, $model, $key) {
return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
'title' => Yii::t('yii', 'Delete'),
'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
'data-method' => 'post',
]);
},
]
],
],
]); ?>
<?php Pjax::end() ?>
Run Code Online (Sandbox Code Playgroud)
调节器
public function actionDelete($id)
{
$model = new Category();
$this->findModel($id)->delete();
$dataProvider = new ActiveDataProvider([
'query' => Category::find(),
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
'model' => $model,
]);
}
Run Code Online (Sandbox Code Playgroud)
这是Controller中的公共函数actionIndex()
public function actionIndex()
{
$model = new Category();
$dataProvider = new ActiveDataProvider([
'query' => Category::find(),
]);
if ($model->load(Yii::$app->request->post()) && $model->save())
{
$model = new Category();
}
return $this->render('index', [
'dataProvider' => $dataProvider,
'model' => $model,
]);
}
Run Code Online (Sandbox Code Playgroud)
Pjax::end();首先在 gridview 末尾添加然后指定:
'delete' => function ($url, $model, $key)
{
return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
'title' => Yii::t('yii', 'Delete'),
'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
'data-method' => 'post',
]);
},
Run Code Online (Sandbox Code Playgroud)
请注意,您不需要指定, 'data-pjax' => '0'因为它会禁用 pjax 链接。
欲了解更多详情,请查看此链接
您的控制器应该是:
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8057 次 |
| 最近记录: |