Gridview, Cbuttoncolumn And Ajax

Hello,

I am trying to use Ajax with CButtonColumn :

View Admin




array(

            'class'=>'QButtonColumn',

                 'template' => '{update}{delete}',

                 'deleteButtonUrl'=>'Yii::app()->createUrl("/TRapportsActivites/delete", array("id" => $data->rapport_activite_id))',

                 'updateButtonUrl'=>'Yii::app()->createUrl("/TRapportsActivites/update", array("id" => $data->rapport_activite_id))',

                 'buttons'  => array(

                 'update' => array(

                     'options' => array(

                        'onclick' => 'js: $("#formUpdate").show();return false;;',

                     ),

                      'ajax' => array(

                    'type' => 'POST',

                    'ajaxUrl' => Controller :: createUrl('/TRapportsActivites/MajFormUpdate'),

                    'dataType' => 'json',

                    'data' => array('id' =>'js:this.value'),

                    'success' => 'function(data) {

                                           $("#' . CHtml::activeId($model, 'date') . '").val(data.date);

                                           $("#' . CHtml::activeId($model, 'heure') . '").val(data.heure);

                                           $("#' . CHtml::activeId($model, 'libelle') . '").val(data.libelle);

                                           $("#' . CHtml::activeId($model, 'facture') . '").val(data.facture);    


                                        }',

                    )

                 )



TRapportsActivitesController




public function actionMajFormUpdate()

	{

        if (Yii::app()->request->isAjaxRequest) {

            

            $id = $_POST['id']; 

            print_r($id);

            $model=$this->loadModel($id);

            echo CJSON::encode(array(

                'date' => $model->date,

                'heure' => $model->heure,

                'libelle' => $model->libelle_public,

                'facture' => $model->facture,

            ));

        }

    }



But Ajax don’t run actionMajFormUpdate.

In fact I would like to update my form in the same view as the viwe where is my gridview.

So, when I click button update, I display the form update with javascript, but the form is not populate with values.

I son’t think I choose the good way for do this, so If you have advice, let me know !

Nath who is sorry for his bad english

Hi Nath,

In your use case ,I will have done something like




'update' => array(

                     //'options' => array(

                     //   'onclick' => 'js: $("#formUpdate").show();return false;;',

                     //),

                      'ajax' => array(

                    'type' => 'POST',

           //use 'update' feature of ajax request

                    'update'=>'#formUpdate',      

                    'ajaxUrl' => Controller :: createUrl('/TRapportsActivites/MajFormUpdate'),

                    'dataType' => 'json',

                    'data' => array('id' =>'js:this.value'),

          // the form will be updated via "renderPartial" from controller action

                    /*'success' => 'function(data) {

                                           $("#' . CHtml::activeId($model, 'date') . '").val(data.date);

                                           $("#' . CHtml::activeId($model, 'heure') . '").val(data.heure);

                                           $("#' . CHtml::activeId($model, 'libelle') . '").val(data.libelle);

                                           $("#' . CHtml::activeId($model, 'facture') . '").val(data.facture);    


                                        }',*/

                    )

elsewhere in your view:


<div id="formUpdate"></div>

and in your controller:




public function actionMajFormUpdate()

        {

        if (Yii::app()->request->isAjaxRequest) {

            

            $id = $_POST['id']; 

            print_r($id);

            $model=$this->loadModel($id);

            $this->renderPartial('myUpdateForm',array('model'=>$model),false,true);

        }

    }



not tested, but it should work

Thank you for you answer.

At the End, I use JUIDialog, but I’am sure that will be usefull for me other time :wink: