CGridView and AjaxForm Connect

You are viewing revision #3 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#4) »

  1. Scenario
  2. Try and error
  3. How-To Do It


As I'm new to the framework (2 weeks) - I tried to build a functionallity I already use a lot in my old framework (PRADO). This means combining a GridView with a form. And this in one page, what allows you to select a record from the grid and view the values in the same page without jumping to a new page.

Try and error

I started to dig arround and came to a point where I used renderPartial to update the form, the problem is, that using this function, I destroy the jQuery id's which disallows me to use widgets like CAutocomplete... And I don't wanna miss the funcionallity.

How-To Do It

<div id="mySubView">

$this->widget('zii.widgets.grid.CGridView', array(
	'dataProvider' => $dataprovider,
	'columns' => array(
                    'deleteButtonUrl'=>'Yii::app()->createUrl("//recipient/delete", array("id" => $data[\'rec_id\']))',


    <div id="mySubViewForm">

    <?php $this->renderPartial('//recipient/_form', array('model'=>$submodel)); ?>



And to later update the values in the form, we need the following javascript update

<script type="text/javascript">

function updateEditForm(target_id){

    var id =$.fn.yiiGridView.getSelection(target_id);

    $('#save_btn').attr('disabled', (id > 0 ? false : true));

    $.getJSON('<?php echo Yii::app()->createUrl("//recipient/subviewload");?>'+'&id='+id,
			function(data) {


views _form

In the detail form we need the following code -> attention!!! I use the extension.ajaxform which is needed for the functionallity!

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
)); ?>


    <fieldset class="box">
	<?php echo $form->errorSummary($model); ?>

            <tr class="row">
                    <?php echo $form->labelEx($model,'org_id'); ?>

                    $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
                       'source' => $this->createUrl('Organisation/autocompleteOrganisation'),
                       'options' => array(

                    <?php echo $form->hiddenField($model,'org_id'); ?>
                    <?php echo $form->error($model,'org_id'); ?>
                    <?php echo $form->textField($model,'kom_id'); ?>
                    <?php echo $form->error($model,'kom_id'); ?>
                    <?php echo $form->hiddenField($model,'rec_id'); ?>
                    <?php echo CHtml::ajaxButton($model->isNewRecord ? 'Create' : 'Update',
                                'success'=>'function(responseText,statusText) {
		<?php echo $form->hiddenField($model,'report_id'); ?>
		<?php echo $form->error($model,'report_id'); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

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

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