Yii2 - 在 GridView 中使用 Ajax/Pjax 通过 switch Toogle 更新数据

Ger*_*ang 3 javascript php ajax pjax yii2

我想使用 Switch Toogle 更新 GridView 中的数据,而不刷新当前页面。

这是图像:
图像1

所以我想使用toogle 开关更新属性,status如上图所示。

这是我的代码:

索引.php

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    //'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        //'alumni_id',
        'tahun_lulus',
        'file_excel',
        [
            'attribute' => 'status',
            'format' => 'raw',
            'value' => function($data){
                if($data->status==0){
                    return SwitchInput::widget([
                        'name' => 'status_11',
                        'pluginOptions' => [
                            'size' => 'mini',
                            'onColor' => 'success',
                            'offColor' => 'danger',
                            'onText' => 'Active',
                            'offText' => 'Inactive',
                        ],
                        'value' => true,
                    ]);
                }
                else if($data->status==1){
                    return SwitchInput::widget([
                        'name' => 'status_11',
                        'pluginOptions' => [
                            'size' => 'mini',
                            'onColor' => 'success',
                            'offColor' => 'danger',
                            'onText' => 'Active',
                            'offText' => 'Inactive',
                        ],
                        'value' => false,
                    ]);;
                }
            }
        ],
        //'deleted',
        'created_at',
        'updated_at',

        [ 'class' => 'yii\grid\ActionColumn'],
    ],
]); ?>
Run Code Online (Sandbox Code Playgroud)

我怎样才能用 Ajax/Pjax 做到这一点?

Muh*_*lam 5

在我建议您解决方案之前,您需要修复一些问题,因为GridView下面的代码中有多余的代码。

[
    'attribute' => 'status',
    'format' => 'raw',
    'value' => function($data){
        if($data->status==0){
            return SwitchInput::widget([
                'name' => 'status_11',
                'pluginOptions' => [
                    'size' => 'mini',
                    'onColor' => 'success',
                    'offColor' => 'danger',
                    'onText' => 'Active',
                    'offText' => 'Inactive',
                ],
                'value' => true,
            ]);
        }
        else if($data->status==1){
            return SwitchInput::widget([
                'name' => 'status_11',
                'pluginOptions' => [
                    'size' => 'mini',
                    'onColor' => 'success',
                    'offColor' => 'danger',
                    'onText' => 'Active',
                    'offText' => 'Inactive',
                ],
                'value' => false,
            ]);;
        }
    }
],
Run Code Online (Sandbox Code Playgroud)

您可以将 的值传递$data->statusvalue的属性,SwitchInput而不是使用if(){}else{}

然后要实现您正在寻找的内容,您必须使用pluginEvent的选项SwitchInput并绑定switchChange.bootstrapSwitch事件以在状态SwitchInput更改时发送 ajax 调用,因此您的 Griview 代码应如下所示

<?php
use kartik\switchinput\SwitchInput;

$js = <<< JS
    function sendRequest(status, id){
        $.ajax({
            url:'/controller/action',
            method:'post',
            data:{status:status,id:id},
            success:function(data){
                alert(data);
            },
            error:function(jqXhr,status,error){
                alert(error);
            }
        });
    }
JS;

$this->registerJs($js, \yii\web\View::POS_READY);


echo  GridView::widget(
    [
        'dataProvider' => $dataProvider,
        //'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
    
            //'alumni_id',
            'tahun_lulus',
            'file_excel',
            [
                'attribute' => 'status',
                'format' => 'raw',
                'value' => function ($data) {
                    return SwitchInput::widget(
                        [
                            'name' => 'status_11',
                            'pluginEvents' => [
                                'switchChange.bootstrapSwitch' => "function(e){sendRequest(e.currentTarget.checked, $data->id);}"
                            ],
                    
                            'pluginOptions' => [
                                'size' => 'mini',
                                'onColor' => 'success',
                                'offColor' => 'danger',
                                'onText' => 'Active',
                                'offText' => 'Inactive'
                            ],
                            'value' => $data->status
                        ]
                    );
                }
            ],
            //'deleted',
            'created_at',
            'updated_at',
    
            [ 'class' => 'yii\grid\ActionColumn'],
        ],
    ]
); 
Run Code Online (Sandbox Code Playgroud)

注意:只需确保将 更改url:'/controller/action',为实际的 URL 和操作即可。如果您不使用,prettyUrl则必须将其更改为index.php?r=controller/action.

编辑

我已经更新了上面的代码,将行id的 与 一起传递status到控制器中的操作,该操作将获取如下所示的变量。

public function actionUpdate(){
   $status = Yii::$app->request->post('status');
   $id = Yii::$app->request->post('id');

}
Run Code Online (Sandbox Code Playgroud)