繁体   English   中英

如何使用Yii中的CGridview使用ajax请求保存数据

[英]How to save data using ajax request using CGridview in Yii

当我提交按钮进行更新时,它不保存数据。在我的view.php文件中 - >`

'id'=>'main-table-grid',
    'dataProvider'=>$dataProvider,
    'columns'=>array(
    array(
      'name'=>'section_no',
      'type'=>'raw',
      'value'=>'CHtml::link($data->section_no)',
    ),
        'section_name',
        'sub_sec_no',
        'sub_sec_name',
        array(
      'name'=>'text',
      'htmlOptions'=>array('width'=>'150'),
      'type'=>'raw',
      'value' => 'CHtml::textArea("MainTable[text]",$data->text)',
    ),
        'review_response',
    array(
      'name'=>'review_comment',
      'htmlOptions'=>array('width'=>'default'),
      'type'=>'raw',
      'value' => 'CHtml::textArea("MainTable[review_comment]",$data->review_comment)',
    ),
    array(
      'class' => 'CButtonColumn',
      'template' => '{update}{view}{delete}',
      'buttons' => array(
        'update' => array(
          'options' => array('class' => 'save-ajax-button'),
          'url' => 'Yii::app()->controller->createUrl("saveModel", array("id"=>$data->id))',
        ),
        'view',
        'delete',
      ),
    ),
    ),
)); 
?>
<script>
   $('#main-table-grid a.save-ajax-button').live('click', function(e)
    {
        var row = $(this).parent().parent();

        var data = $('input', row).serializeObject();

        $.ajax({
            type: 'POST',
            data: data,
            url: jQuery(this).attr('href'),
            success: function(data, textStatus, jqXHR) {
                console.log(text);
                console.log(textStatus);
                console.log(jqXHR);
            },
            error: function(textStatus, errorThrown) {
                console.log(textStatus);
                console.log(errorThrown);
            }
        });
        return false;
    });


    $.fn.serializeObject = function() {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };
</script>`

在我的控制器中我创建了一个动作方法。代码如下 - >

public function actionSaveModel($id) {
    $model=$this->loadModel($id);
    $this->performAjaxValidation($model);
    if(isset($_POST['MainTable']))
        {
      $model = new MainTable();
      $model->attributes = $_POST['MainTable'];
      $model->save();
      $this->render('admin', array(
         'model' => $model,
      ));
        }
  }

我在我的控制器中设置了权限

    array('allow', // allow authenticated user to perform 'create' and 'update' actions
        'actions'=>array('savemodel'),
        'users'=>array('@'),
    ),

我的问题是数据没有保存在表中。请告诉我这里的问题是什么。 谢谢。

好了,我已经解决了这个问题。我通过选择需要更新的ID并将数据发布到控制器来获取ajax响应。

<script type="text/javascript">
$(function() {  
  $('#MainTable_text').live('click', function(e)
    {
        var val=this.value;
        $.ajax({
            type: 'POST',
            data: {des:val},
            url: "<?php echo $this->createUrl("maintable/save"); ?>",
            success: function(data, textStatus, jqXHR) {
                console.log(data);
                console.log(textStatus);
                console.log(jqXHR);
            },
            error: function(textStatus, errorThrown) {
                console.log(textStatus);
                console.log(errorThrown);
            }
        });
        return false;
    });
});

之后,我在控制器中捕获值并在数据库中更新。

  public function actionSave() {
    $command = Yii::app()->db->createCommand();
    $user = $_POST['des'];
    if (isset($user)) {
      $command->update('main_table', array(
          'text' => $user,
              ), 'id=:id', array(':id' => $id));
      $this->render('save', array(
          'model' => $model,
      ));
    } else {
      echo 'error found';
    }
  }

请将您的jQuery代码放在此代码中:

<script type="text/javascript">
$(function() {
// put your jQuery code here
});
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM