0

I need to update a modal window and show it when I click in the edit button.

CONTROLLER:

public function actionUpdateAjax($id)
    {
        $contratos = ZfContratos::model()->findByPk($id);

        $this->renderPartial('//ZfContratos/_form_update', array('model'=>$contratos), false, true);
    }

INDEX:

<?php $this->beginWidget(
    'bootstrap.widgets.TbModal',
    array('id' => 'actualizar_contrato')
); ?>
    <div class="modal-header">
        <a class="close" data-dismiss="modal">&times;</a>
        <h4>Actualizar contrato</h4>
    </div>
    <div class="modal-body">

        <?php $this->renderPartial('//ZfContratos/_form_update', array('model'=>$contrato));?>

    </div>
    <div class="modal-footer">

        <?php $this->widget(
            'bootstrap.widgets.TbButton',
            array(
                'label' => 'CANCELAR',
                'url' => '#',
                'htmlOptions' => array('data-dismiss' => 'modal'),
            )
        ); ?>
    </div>
<?php $this->endWidget(); ?>

AND VIEW _contratos

At this moment I have this:

<?php echo CHtml::link('EDITAR', array('//ZfInmuebles/UpdateAjax', 'id'=>$data->zf_contrato_id), array('class'=>'btn', 'id'=>'vermas')); ?>

But I need that to be an ajaxbutton or ajaxlink, that refresh the div "actualizar_contrato" and show it.

Sergio_HW
  • 155
  • 3
  • 18
  • yes it is possible,you just need to add a save/submit button to your modal making a ajax post call on this button to submit the form in the background, on success you can toggle close the form automatically. – Manquer Mar 12 '14 at 12:54
  • At this moment, when you click and take you to the new page, the fields contain the actual value of the model, with the ajax call, can I keep that? – Sergio_HW Mar 12 '14 at 12:57
  • hmmm.. i dont see any problem with that, i suppose this page is like a typical "view" page? However if you want reduce even that page, simply renderPartial another view containing said info, and you can update the modal body with that content on saving, or show a different modal etc – Manquer Mar 12 '14 at 14:47
  • Can you put an example please? – Sergio_HW Mar 12 '14 at 15:14
  • i have made a example in the answer.. modify your CHtml::link to ajaxLink, and update your controller accordingly – Manquer Mar 12 '14 at 16:12

1 Answers1

0

This is just a reference implementation to give you an idea of how to do this, do modify as per your requirements

// your view
// Button to trigger modal
 <?php echo CHtml::ajaxLink('EDITAR',array('//ZfInmuebles/UpdateAjax', 'id'=>$data-> zf_contrato_id), array(
          'type'=>'POST',
          "success"=>'js:function(html){
              $("#actualizar_contrato >.modal-body").html(html);
              $("#actualizar_contrato").modal("show");
                }
          '));?
.....
  // modal
    <?php $this->beginWidget(
        'bootstrap.widgets.TbModal',
        array('id' => 'actualizar_contrato')
    ); ?>
        <div class="modal-header">
            <a class="close" data-dismiss="modal">&times;</a>
            <h4>Actualizar contrato</h4>
        </div>
        <div class="modal-body">
        <?php 
            $contrato = new ZfContratos; 
            $this->renderPartial('//ZfContratos/_form_update', array('model'=>$contrato));
        ?>
        </div>
        <div class="modal-footer">
            <?php echo CHtml::ajaxLink('Save',  // Link Text
            array('//ZfContratos/update', 'id'=>$data->zf_contrato_id), //url
            array(// ajaxOptions
                'data'=> 'js:$("#id_of_the_form").serialize()' ,
                'type'=>'POST',
                'success'=>'js:function(html){$("#actualizar_contrato >.modal-body").html(html) }'
                )
            ) 
            );?>
            <?php $this->widget(
                'bootstrap.widgets.TbButton',
                array(
                    'label' => 'CANCELAR',
                    'url' => '#',
                    'htmlOptions' => array('data-dismiss' => 'modal'),
                )
            ); ?>
        </div>
    <?php $this->endWidget(); ?>
    .....

And now in your controller modify as like below

    //ZfContratos Controller
    public function actionUpdate(){
        // Your regular logic for saving 
        if(Yii::app()->request->isAjaxRequest){
            if($model->save()){

                $this->renderPartial("_mvsavedView",array('model'=>$model)) //OR echo "Saved Successfully"; 
            } else {
                print_r($model->getErrors());
            }
            Yii::app()->end();  
        }
    }
Manquer
  • 7,390
  • 8
  • 42
  • 69
  • when I click in "EDITAR" nothing happens, it doesnt open the modal window with the form – Sergio_HW Mar 12 '14 at 16:43
  • I had used editar button for saving the form. not triggering the modal, i have now adjusted the answer accordingly – Manquer Mar 12 '14 at 18:39
  • The thing is that I need to load the form with a given id contrato, but i cant update the id that i give to the model: ZfContratos::model()->findByPk($contrato_id); $this->renderPartial('//ZfContratos/_form_update', array('model'=>$contrato)); – Sergio_HW Mar 12 '14 at 18:47
  • See edit please, I think I confused you, I want to load a form, the button "SAVE" of the form works fine, the problem is refresh the div "actualizar_contrato" when he click in "EDITAR" of the view _contratos – Sergio_HW Mar 13 '14 at 09:35
  • It doesn't work, the modal is not display when I click – Sergio_HW Mar 13 '14 at 17:45