【问题标题】:Yii2 action with PjaxYii2 动作与 Pjax
【发布时间】:2020-06-30 18:09:36
【问题描述】:

在我的 yii2 应用程序中,我有一个带有自定义操作的 gridview:

<?php Pjax::begin(); ?>
<?= GridView::widget([
    'dataProvider' => $data,
    'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            [
                'label' => 'Id Article',
                'value' => 'article.id',
            ],
            [
                'label' => 'Article Code',
                'value' => 'article.code',
            ],
            [
                'label' => 'Article Price',
                'value' => 'article.price',
            ],
            [
                'label' => 'Quantity',
                'value' => 'quantity',
            ],
            [
             'class' => 'yii\grid\ActionColumn',
             'template' => '{delete}',
             'urlCreator' => function ($action, $model, $key) {
                    if ($action === 'delete') {
                         return \yii\helpers\Url::toRoute(['estimate/delete-article', 'id_estimate_article' => $model->id]);
                    }
                }
            ], 

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

如您所见,我正在用 Pjax 包装我的 gridview 以处理 Ajax 响应。

这是我的控制器中的删除文章操作:

public function actionDeleteArticle($id_estimate_article)
{
    $estimateArticle = EstimateArticle::findOne(['id' => $id_estimate_article]);    
    $id_estimate_head = $estimateArticle->estimate->id;
    $estimateAccessories = $estimateArticle->estimateAccessories;
    foreach ($estimateAccessories as $estimateAccessory) {
        $estimateAccessory->delete();
    }
    $estimateArticle->delete();
    return $this->redirect($this->redirect(\yii\helpers\Url::toRoute(['add-informations', 'id_estimate_head' => $id_estimate_head])));
}

代码有效,但页面每次都会刷新(所以 Pjax 没有做的就是工作)。这是我第一次使用 Pjax。

我哪里错了? 提前感谢所有帮助

【问题讨论】:

标签: php yii2 yii2-basic-app


【解决方案1】:

您可以尝试使用此代码解决您的 Pjax 重新加载问题。

<?php Pjax::begin(); ?>
<?= GridView::widget([
    'dataProvider' => $data,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        [
            'label' => 'Id Article',
            'value' => 'article.id',
        ],
        [
            'label' => 'Article Code',
            'value' => 'article.code',
        ],
        [
            'label' => 'Article Price',
            'value' => 'article.price',
        ],
        [
            'label' => 'Quantity',
            'value' => 'quantity',
        ],
        [
            'class' => 'yii\grid\ActionColumn',
            'header' => 'Action',
            'template' => '{delete}',
            'buttons' => [
                'delete' => function ($url, $model, $key) {
                    return Html::a('Delete', yii\helpers\Url::to(['estimate/delete-article', 'id_estimate_article' => $model->id]), [
                        'class' => '',
                        'data' => [
                            'confirm' => 'Are you sure want to delete records?',
                            'method' => 'post',
                        ],
                    ]);
                }
            ]        
        ],

    ]
])
?>

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多