ajax无法在gridview中的可编辑列中工作

时间:2015-04-04 06:27:04

标签: ajax gridview yii2

我想在yii2 gridview中使用可编辑列,它的工作和模型保存但是ajax在可编辑列中不起作用,当我单击应用按钮时ajax不完整

[  'class'=>'kartik\grid\EditableColumn',
    'headerOptions' => ['style' => 'width:15%;text-align:center','class' => 'kv-sticky-column'],
    'attribute'=>'name',
    'pageSummary'=>true,
    'vAlign'=>'middle',
    'contentOptions' => ['class' => 'kv-sticky-column'],
    'editableOptions'=> function ($model, $key, $index){
        return [
            'header'=>'Name', 
            'size'=>'md',
             //'value'=>$model->name,
              //'class'=>'has-success',
        ];
     }
],

上面是我的gridview联系人和控制器

public function actionIndex()     {

    $searchModel = new UsersSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    if(Yii::$app->request->post('hasEditable'))
    {
         $model =new Users(); 
        $bookId = Yii::$app->request->post('editableKey');
        $model = Users::findOne($bookId);


        $post = [];
        $posted = current($_POST['Users']);
        $post['Users'] = $posted;

     // Load model like any single model validation
    if ($model->load($post))
    {
        // When doing $result = $model->save(); I get a return value of false
        if($model->save())
        {
        if (isset($posted['name']))
        {
          $output =  $model->name;
        }
        $out = Json::encode(['output'=>$output, 'message'=>'']);
        }
    } 
    // Return AJAX JSON encoded response and exit
    echo $out;
    return;
    }
    return $this->render('index', [

        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

1 个答案:

答案 0 :(得分:1)

public function actionIndex() {

    $searchModel = new UsersSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    if(Yii::$app->request->post('hasEditable'))
    {
         $model =new Users(); 
        $bookId = Yii::$app->request->post('editableKey');
        $model = Users::findOne($bookId);


        $post = [];
        $posted = current($_POST['Users']);
        $post['Users'] = $posted;

     // Load model like any single model validation
    if ($model->load($post))
    {
        // When doing $result = $model->save(); I get a return value of false
        if($model->save())
        {
        if (isset($posted['name']))
        {
          $output =  $model->name;
        }
        $out = Json::encode(['output'=>$output, 'message'=>'']);
        }
    } 
    // Return AJAX JSON encoded response and exit
    echo $out;
    die; write die instead of return.so its solved it
    }
    return $this->render('index', [

        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}