P
P
Pavel Gogolinsky2014-10-23 12:33:29
Yii
Pavel Gogolinsky, 2014-10-23 12:33:29

How to organize ajax update of element with id="myElem" in Yii?

There is an element with id="myElem". The data is output to it via renderPartial.
I need to update the content of this element via ajax.
It is possible through CHtml::ajaxLink, which has a cool update parameter. But this option is not suitable. The content of the element should change when a value is selected from the drop-down list.
How to update the content of an element after an ajax request in yii without CHtml::ajaxLink?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rustamka Vorontsov, 2014-10-23
@gogolinsky

public function actionAjaxUpdate(){
        if(Yii::app()->request->isAjaxRequest){ // только ajax
            $this->layout = false; // отключим шаблон
            $this->render('то что тащит рендер', array(
                'params'=>$params
            ));
        }
        else
            throw new CHttpException(404, 'Запрашиваемая страница не существует.');
    }

Wrap Your Render
<div id="myElem">
        <?php $this->renderPartial('//то что тащим', true, false, array(
                 'params'=>$params 
        )) ?>
</div>

well js
$('#link').click(function (e){
        e.stopPropagation();
        var update = $('#myElem');
        $.ajax({
                'type': 'POST',
                'url': '/AjaxUpdate',
                'cache': false,
                'success':function(data) {
                                update.html(data);
                }
        });
});

And link
<?php echo CHtml::link('Обновить!', '#', array('id'=>'link')) ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question