fabiomartins87
fabiomartins87

Reputation: 167

How to render a partial view into 2 different DIVs, after AjaxSubmitButton on Yii?

Does anyone know how to render 2 different partials into 2 differents DIV's in the same page, after pressing a CHTML:AjaxSubmitButton and using the same controller?

I've searched around the web for this but so far I didn't find anything that could help me.

Thanks for your help.

Upvotes: 2

Views: 1950

Answers (1)

Michael Härtl
Michael Härtl

Reputation: 8587

You don't need a ajaxSubmitButton(). A ajaxButton() is sufficient.

In your view:

<?php echo CHtml::ajaxButton('Click me', array('mycontroller/myaction'), array(
    'dataType' => 'json',
    'success' => 'js:function(res) { 
        $("#div1").html(res.div1); 
        $("#div2").html(res.div2);
    }',
)); ?>

In your controller:

public function actionMyaction()
{
    echo json_encode(array(
        'div1' => $this->renderPartial('div1', null, true),
        'div2' => $this->renderPartial('div2', null, true),
     ));
}

Upvotes: 8

Related Questions