Implement Next/previous Buttons For _Form View

I have a customer that wants to be able to move to the next record in the database while in the _form view. This would be convenient for making updates to lots of records without having to always go back to the list view to select the next record.

I have the controller fetching the next record into a model and then I try to re-render the _form view with the new data and nothing changes. The model is getting to the _form.php but is not getting rendered on the page. I have used render(), renderPartial() and redirect() without any success.

If anyone has done this I would like some pointers on making this happen. It seems like it should be pretty strait forward but I can’t seem to make it work.

Thanks for any help,

Zeke

What widget are you using? CGridView or some other?

You may want to check out some tutorials on wiki:

http://www.yiiframework.com/wiki/381/cgridview-clistview-and-cactivedataprovider/

http://www.yiiframework.com/wiki/?tag=cgridview

Thanks for the reply!

I’m using the CActiveForm widget for the form and there is a CJuiDialog widget (nested inside the form).

I’ll take a look at those links and see if I find any helpful info.

Zeke

Those links seem to deal with grid or list views. I am trying to load a new record into the _form view by pressing a ‘Next’ or ‘Previous’ button. I don’t think ajax is an option because I really need to render the whole form page with new information.

Maybe I’m overlooking something simple but…

If each record has an ID and they are loaded like:

/site/controllerAction/bulkEdit/id

Wouldn’t your next button just be id+1?

So in your bulkEdit action, get a list of all Ids you will be cycling through and generate next/last buttons with the right URL scheme.

Dear Friend

The following is one simple implementation.

I hope this would serve the purpose.

Let us assume that we have model [b]Medico./b

MedicoController.php




public function actionSerialUpdate()

{   

	$criteria=new CDbCriteria();

	$count=Medico::model()->count($criteria);

	$pages=new CPagination($count);

        $pages->pageSize=1;//JUST GOING TO DISPLAY ONE RECORD.

	$pages->applyLimit($criteria);

	$model=Medico::model()->find($criteria);

	$this->performAjaxValidation($model);//JUST ENSURE THAT THIS METHOD EXISTS INSIDE YOUR CONTROLLER.

	if(isset($_POST['Medico']))

		{   

			$model=$this->loadModel($_POST['Medico']['id']);

			$model->attributes=$_POST['Medico'];

			if($model->save())

				{

					$this->render('_serialForm',array('model'=>$model,'message'=>"Successfully updated",'pages' => $pages));

					Yii::app()->end();

				}

		}

        $this->render('_serialForm', array(

			'model' => $model,

			'pages' => $pages

      ));

  }



views/medico/_serialForm.php




<div class="form">


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

	'id'=>'medico-form',

	'enableAjaxValidation'=>true, //ENABLED AJAX VALIDATION.

)); ?>


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


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

		if(isset($message)) //IF RECORD IS SUCCESSFULLY SAVED,WE ARE GOING TO DISPLAY SUCCESS MESSAGE.

			echo CHtml::tag("div",array('class'=>'flash-success'),$message);

	?>

     <!--WE ARE GOING TO HAVE A HIDDEN FIELD TO COLLECT THE PRIMARY KEY.-->

     <div class="row">

		

		<?php echo $form->hiddenField($model,'id'); ?>

		

	</div>

	<div class="row">

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

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

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

	</div>


	<div class="row">

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

		<?php echo $form->textField($model,'age'); ?>

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

	</div>


	<div class="row">

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

		<?php echo $form->textField($model,'sex',array('size'=>10,'maxlength'=>10)); ?>

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

	</div>


	<div class="row">

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

		<?php echo $form->textField($model,'native',array('size'=>60,'maxlength'=>64)); ?>

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

	</div>


	<div class="row buttons">

		<?php echo CHtml::submitButton('Save'); ?>

	</div>


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


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


<?php $this->widget('CLinkPager', array(

      'pages' => $pages, //WE ARE GOING TO USE CLINKPAGER TO NAVIGATE ACROSS MANY RECORDS.

  )) ?>



I am using CActiveForm.

I have not tested including a dialog box inside the form.

If you are finding any difficulty in inducting a dialog box,let me know.

Regards.

Well…I got it working. Not sure why it works the way it does but it works.

I had been trying to use a submit button or an ajax button and for what ever reason those didn’t work. So I tried a link. That worked! So I made a button with the link that turned out like this:

view code:




<INPUT TYPE="button" onClick="window.location='/projectdashboard/index.php/admin/resourceProfile/getPreviousRecord?id=<?php echo $resourceProfile->id; ?>'" value="Previous">



controller code:




public function actionGetPreviousRecord() {

    $thisId = $_REQUEST['id'];

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

    $record = ResourceProfile::model()->findBySql('SELECT * FROM t_resourceprofile WHERE lastName < "' . $model->lastName . '" ORDER BY lastName DESC Limit 1');

//    if ($record === null)

//      throw new CHttpException(404, 'The requested page does not exist.');

    if ($record !== null) {

      $id = $record->id;

    } else {

      $id = $thisId;

    }

    $this->render('update', array('resourceProfile' => $this->loadModel($id)));

  }



and that worked.

Maybe someone can explain why this worked and the submit buttons didn’t.

Thanks,

Zeke