yii2,使用新数据部分渲染视图

yii2, partially render view with new data

yii2 和 PHP 的新手,运行 在这里休息一下。
基于 and this post,我尝试重新创建 AJAX 调用。
现在,使用 Kartik SwitchInput 小部件,我至少试图将值输入控制器,然后 return 它到索引页面上的另一个位置。

这是视图中的内容

...
<?= GridView::widget([
    'dataProvider' => $customers,
    'columns' => [
        ...
        [
            'label' => 'Status',
            'format' => 'raw',
            'attribute' => 'status',
            'value' => function($models){
                return SwitchInput::widget([
                    'name' => $models->id,
                    'value' => $models->status,
                    'pluginEvents' => [
                        'switchChange.bootstrapSwitch' => 'function(e) { 
                            $.ajax({
                                method: "POST",
                                url: "'.Url::to(['update-status']).'",
                                data: { "status": e.currentTarget.checked, "id": e.currentTarget.name },
                                error:function(res){
                                    console.log(res.responseText);
                                }
                            })
                            console.log(`${e.currentTarget.checked}  ${e.currentTarget.name}`);
                        }'
                    ]
...

这是控制器:

class CustomerController extends Controller{
    public function actionIndex(){

        $customers = new ActiveDataProvider([
            'query' => Customer::find()
        ]);

        $models = $customers->getModels();

        return $this->render('index', [ 
            'customers' => $customers,
            'models' => $models
        ]);
    }

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

        return $this->renderPartial('index', [
            'status' => $status,
            'id' => $id
        ]);
    }
}

每当我按下开关时,它都会 return 出现 500 内部错误。 Console.logging 它显示:$customers 未定义。
据我所知,每当我调用 actionUpdateStatus 时,Yii 都会完全重新呈现视图,并尝试找到不在 UpdateStatus 中的 $customers。
那么如何return将数据单独切换回同一个视图呢?

我发布了你提到的 之一,但我没有指定一件事是重新加载 gridview,这不是那里的要求,但可以使用 $.pjax.

一些你需要看的东西

  • 根据您的要求,updateStatus 操作不需要任何视图。
  • 您只需要 return 一些状态代码到您在 gridView 中用于切换输入的 ajax 调用,并刷新您的 gridview 以加载新的更新状态。
  • 在 ajax 成功回调函数中检查状态代码是否正常,然后您应该刷新 gridview 而不是使用 render partial。
  • 而且您没有更新 updateStatus 中的任何内容?你需要切换状态

因此将您的操作更改为以下内容

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

        $model = Customer::findOne($id);
        $model->status=$status;
        if(!$model->save()){
           throw new Exception(implode("\n",\yii\helpers\ArrayHelper::getColumn($model->errors,'0'));
        }
        return $this->asJson(['success' => true]);
    } catch (Exception $e) {
        return $this->asJson(['success' => false, 'message' => $e->getMessage()]);
    }
}

确保已将 GridView 包装在 Pjax 开始和结束内,并向 Pjax 小部件添加 id

<?php 
    Pjax::begin(['id'=>'pjax-container']);

    echo GridView::widget([
    'dataProvider' => $customers,
    'columns' => [
    .......
    ]);

    Pjax::end();
 ?>

你的 ajax 调用如下

'switchChange.bootstrapSwitch' => 'function(e) {
    $.ajax({
        method: "POST",
        url: "'.Url::to(['update-status']).'",
        dataType:'json',
        data: { "status": e.currentTarget.checked, "id": e.currentTarget.name },
        success:function(data){
             if(data.success){
                  $.pjax({container: "#pjax-container"});
             }else{
                  console.log(data.message);
             }
        },
        error:function(res){
            console.log(res.responseText);
        }
    });
}'