AjaxForm with customized CGridView


(Alarice Chan) #1

The form works but it throws the renderPartial result instead of displaying the main form and the table.

View Code:




<?php


 $course_options = array();

    $course_options[''] = "";

    $courses = AlumniCourses::model()->findAll();


    $add_cr_opts = "<option value=></option>";

    $add_yr_opts = "<option value=></option>";


    $copts = array();


    foreach ($courses as $course) {

        $copts[$course->course_id] = $course->course;

        $add_cr_opts .= "<option value=\"$course->course_id\">$course->course</option>";

    }


    $year_options = array();

    $cyr = date('Y');

    for($yr = 1993; $yr < $cyr; $yr++){

        $year_options[$yr] = $yr;

        $add_yr_opts .= "<option value=\"$yr\">$yr</option>";

    }


echo $msg;


$this->widget('ext.ajaxedit.FormGridView', array(

    'dataProvider' => new CActiveDataProvider('AlumniCmatch', array('criteria' => array(

        'select' =>'*', 'condition'=>'alumni_id=:alumniId', 'params'=> array(':alumniId'=>$alumni_id)

     ))),

    'id'=>'courseAlumniTable',

    'formId'=>'addCourseForm',

    'formAction'=>'index.php?r=alumni/courseEdit',

    'ownerKeyName'=> 'alumni_id',

    'ownerKey' => $alumni_id,

     'columns'=> array(

         array('class' => 'ext.ajaxedit.grid.HiddenFormColumn'),

         array( 'class' =>'ext.ajaxedit.grid.RelationColumn', 'textValues'=> $copts,'name' => 'course_id'),

         array( 'class' =>'ext.ajaxedit.grid.RelationColumn', 'textValues'=> $year_options, 'name' => 'yr_grad'),

         array('class' => 'ext.ajaxedit.grid.CustomButtonColumn', 'template'=> '{delete}', 'primaryKeyName'=>'entry_id')

     )

));


$this->widget('ext.ajaxform.JAjaxForm',

     array('formId'=>'addCourseForm',

        'options'=>array( 'target' => '#courseAlumniTable',

            'replaceTarget'=>true)

     )

);

Action Code:




 public function actionCourseEdit() {

        $msg = '';

        if (isset($_POST['entry_id'])) {

            if (!preg_match('/[\\d]+/', $_POST['entry_id'])) {

                $msg = '<div class=error_msg>Invalid submitted data.</div>';

            } else {

                $amatch = AlumniCmatch::model()->findByPk($_POST['entry_id']);

                if (is_null($amatch)) {

                    $msg = '<div class=error_msg>Invalid submitted data.</div>';

                } else {

                    $amatch->course_id = $_POST['course_id'];

                    $amatch->yr_grad = $_POST['yr_grad'];

                    if ($amatch->save()) {

                        $msg = "<div class=success_msg>Edit has been successfully saved.</div>";

                    } else {

                        $msg = "<div class=error_msg>Invalid submitted data.</div>";

                    }

                }

            }

        } else {

            $amatch = new AlumniCmatch();

            $amatch->alumni_id = $_POST['alumni_id'];

            $amatch->course_id = $_POST['course_id'];

            $amatch->yr_grad = $_POST['yr_grad'];

            if ($amatch->save()) {

                $msg = "<div class=success_msg>New Entry has been successfully saved.</div>";

            } else {

                $msg = "<div class=error_msg>Invalid submitted data.</div>";

            }

        }

        if (preg_match('/[\\d]+/', $_POST['alumni_id']))

            $this->renderPartial('_coursesTable', array('msg'=>$msg, 'alumni_id' => $_POST['alumni_id']));

    }



Main Form View Code:




<div class="form">




<?php $form = $this->beginWidget('GxActiveForm', array(

	'id' => 'alumni-form',

	'enableAjaxValidation' => true,

));

?>


	<p class="note">

		<?php echo Yii::t('app', 'Fields with'); ?> <span class="required">*</span> <?php echo Yii::t('app', 'are required'); ?>.

	</p>


	<?php echo $form->errorSummary($model); ?>


		<div class="row">

		<?php echo $form->labelEx($model,'name'); ?>

		<?php echo $form->textField($model, 'name', array('maxlength' => 255)); ?>

		<?php echo $form->error($model,'name'); ?>

		</div><!-- row -->

		<div class="row">

		<?php echo $form->labelEx($model,'current_position'); ?>

		<?php echo $form->textField($model, 'current_position', array('maxlength' => 255)); ?>

		<?php echo $form->error($model,'current_position'); ?>

		</div><!-- row -->

		<div class="row">

		<?php echo $form->labelEx($model,'company'); ?>

		<?php echo $form->textField($model, 'company', array('maxlength' => 255)); ?>

		<?php echo $form->error($model,'company'); ?>

		</div><!-- row -->

		<div class="row">

		<?php echo $form->labelEx($model,'address'); ?>

		<?php echo $form->textField($model, 'address', array('maxlength' => 255)); ?>

		<?php echo $form->error($model,'address'); ?>

		</div><!-- row -->

		<div class="row">

		<?php echo $form->labelEx($model,'awards'); ?>

		<?php echo $form->textArea($model, 'awards'); ?>

		<?php echo $form->error($model,'awards'); ?>

		</div><!-- row -->

		<div class="row">

		<?php //echo $form->labelEx($model,'pic'); ?>

		<?php //echo $form->checkBox($model, 'pic'); ?>

		<?php //echo $form->error($model,'pic'); ?>

		</div><!-- row -->


<?php

echo GxHtml::Button(Yii::t('app', 'Cancel'), array(

			'submit' => array('alumni/admin')

		));

echo GxHtml::submitButton(Yii::t('app', 'Save'));

$this->endWidget();

?>

                <br><br>

</div>

<label>Courses Taken</label>

<br>


    <?php

        $this->renderPartial('_coursesTable', array('msg'=>'','alumni_id' => $model->alumni_id));

    ?>