Cgridview - data update.

Hi i’m very new and my question might be very stupid and confusing hopefully someone can understand and try to help out.

I got a json file from souce A and manage to show it in cgridview. (Done by controller)

in View file

with ID | Name |

001 XXX

002 Xxx

003 xxx

let said it have 10 records. I wish to allow user to select one of the ID and click on submit button, it will pass the ID value and update the data in another model

I think checkbox is the best way to pass the value.

Example data :

Original record.

item 1 : 002

so after user select one of the record and click submit, let said 003. It will pass and update the record.

Updated record

item 2 : 003

Can anyone tell me how to perform it? I really don know what to do in controller.

You can solve this problem using ajax (without reload page) or make an url request.

In both ways, you have to pass data to an action of controller and make your needs.

Hi bro Fabrizio thanks for explaination but i think i need some sample coding cuz i totally lost my way @_@

ok at least done something by my own…so below is my code in View file - update. I would like to update the model by check the checkbox.

So below code is successfully get the NID but i do not know how to update the model. Any one can help ??

6794

sample.jpg

<?php

?>

<div class="form">

<?php $form=$this->beginWidget(‘CActiveForm’, array(

'id'=&gt;'social-box-form',


'enableAjaxValidation'=&gt;false,

)); ?>

&lt;p class=&quot;note&quot;&gt;Fields with &lt;span class=&quot;required&quot;&gt;*&lt;/span&gt; are required.&lt;/p&gt;





&lt;?php echo &#036;form-&gt;errorSummary(&#036;model); ?&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'nid'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'nid',array('size'=&gt;11,'maxlength'=&gt;11)); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'nid'); ?&gt;


&lt;/div&gt;

<?php $this->endWidget(); ?>

</div><!-- form -->

<?php

$this->widget(‘zii.widgets.grid.CGridView’, array(

'id'=&gt;'box-grid',


'dataProvider'=&gt;&#036;dataProvider,


    'selectableRows'=&gt;1,


    'afterAjaxUpdate' =&gt; 'reInstallButtons',


   'columns' =&gt; array(


     array(


         'header' =&gt; 'No.',


         'value' =&gt; '++&#036;row',


         ),     


         


    array(


        'name' =&gt; 'NID',


        'type' =&gt; 'raw',


        'value' =&gt; 'CHtml::encode(&#036;data[&quot;nid&quot;])'


    ),


    array(


        'name' =&gt; 'Title',


        'type' =&gt; 'raw',


        'value' =&gt; 'CHtml::encode(&#036;data[&quot;title&quot;])',


    ),


     array(


         'header' =&gt; 'Show',          


         'class'=&gt;'CCheckBoxColumn',


         'id'=&gt;'box_id',


         'selectableRows'  =&gt; null,


         'checked'=&gt;'0'


         ),


),

));

?>

<?php

&#036;str_js = &quot;





    function reInstallButtons() {


        installButtons();


    }


    


    function installButtons() {


        &#036;('input[type=checkbox]').click(function(){


            var data = &#036;. fn.yiiGridView.getChecked('box-grid', 'box_id');


            var dataString = JSON.stringify(data);





          console.log('Box 1 updated&#33;&#33;' + dataString);


                   


        });


    }





    installButtons();


&quot;;





Yii::app()-&gt;clientScript-&gt;registerScript('status-update', &#036;str_js);

?>

After you got data from select, you can make an ajax call:




$urlAjax = $this->createUrl('controller/action/to/call');

Yii::app()->clientScript->registerScript( <<< EOT_JS


function launchAjaxCall()

{

      $.post(

           '{$urlAjax}',

            { p1 : '...', p2 : '... },

            function(data) {

                   console.log('model updated!');

            }

      );

}


EOT_JS

);



Bro Fabrizio Caldarelli sorry can u be more specific ? I’m so dumb sorry !! Where should i put

You should get checked value here, is it right?




function installButtons() {

$('input[type=checkbox]').click(function(){

var data = $. fn.yiiGridView.getChecked('box-grid', 'box_id');

var dataString = JSON.stringify(data);


console.log('Box 1 updated!!' + dataString);


});

}



So you should make ajax call in this block.

okok