dipayan
(Johnmaclein Jm)
August 21, 2012, 10:56am
1
Hi All,
Here is the scenario,
I have an ajax link in view and three divs with it like below
<?php
ech CHtml::ajaxLink("link",
CHtml::normalizeUrl(array('user/ajaxRefreh')),
arary(
'type'=>'POST',
'dataType'=>'json',
'success'=>"function(data){
$('#div1').html(data.div1);
$('#div2').html(data.div2);
$('#div3').html(data.div3);
}"
),
array(
'id'=>'link1',
)
);
?>
<div id="div1">
</div>
<div id="div2">
</div>
<div id="div3">
</div>
now in my controller I have this kind of function
public function actionAjaxRefreh(){
echo CJSON::encode(
array(
'div1'=>$this->renderPartial('_view1', array(), false, true),
'div2'=>$this->renderPartial('_view2', array(), false, true),
'div3'=>$this->renderPartial('_view3', array(), false, true),
)
);
}
But when I am running this code its showing the page in html tab and below it written div1, div2, div3 are null
and the divs are not refrehing at all.
Please help me. What am I mising and if its not the correct way, please show me the right path
mdomba
(Maurizio Domba Cerin)
August 21, 2012, 12:30pm
2
Note that the third parameter of renderPartial decides if the rendered content will be returned or echoed… you set it to false so no data is returned…
dipayan
(Johnmaclein Jm)
August 21, 2012, 12:59pm
3
I had done that, but what’s happening is that the targeted div becomes completely blank. Don’t know why… its showing nothing in that div…
mdomba
(Maurizio Domba Cerin)
August 21, 2012, 1:03pm
4
Did you check if correct data is returned by the ajax call?
dipayan
(Johnmaclein Jm)
August 21, 2012, 1:16pm
5
There was not showing any json tab in firebug
Total html tab and response was blank in firebug
div was completely blank after clicking on ajax link
I used exactly these lines in controller
echo CJSON::encode(
array(
'div1'=>$this->renderPartial('_view1', array(), false, true),
'div2'=>$this->renderPartial('_view2', array(), false, true),
'div3'=>$this->renderPartial('_view3', array(), false, true),
)
);
and changed third parameter to true.
The way what I am is that correct or there is some another correct way out?
please help me
mdomba
(Maurizio Domba Cerin)
August 21, 2012, 1:26pm
6
So the ajax call is made but there are no data returned.
Try to just
echo "anytext"
instead of the JSON::encode(), just to see if you will get that text as a response…
If you get the text then the problem is in the rendering of your views. Do you need the 4th parameter to be true ?
dipayan
(Johnmaclein Jm)
August 21, 2012, 1:33pm
7
mdomba:
So the ajax call is made but there are no data returned.
Try to just
echo "anytext"
instead of the JSON::encode(), just to see if you will get that text as a response…
If you get the text then the problem is in the rendering of your views. Do you need the 4th parameter to be true ?
once I tried this one
echo CJSON::encode(
array(
'div1'=>"hello"
)
)
this code refreshed the div with this "hello" but only
$this->renderPartial('_view1', array(), true, true),
is not working
I tried this one too
$this->renderPartial('_view1', array(), true),
is also not working
But I need that 4th param true as it contains some ajax links too
Drini
(Idrini)
August 21, 2012, 2:59pm
8
echo CJSON::encode(
array(
'div1'=>$this->renderPartial('_view1', array(), true, true),
'div2'=>$this->renderPartial('_view2', array(), true, true),
'div3'=>$this->renderPartial('_view3', array(), true, true),
)
);
if that does not work, you must check in your view files if you have stuff there that messes it up.
try to comment out all code in the view files and replace with "div1" "div2" and "div3" in each file,if that works,
then try to add more and more of the code you really want to use in _view1 until
a ) it stops working
b ) it all works
then you will either know that there is no wrong in _view1 or what code in _view1 that makes it stop working.
Then do the same with the other two and then share the code that stops it from working with us and maybe we can help.
dipayan
(Johnmaclein Jm)
August 22, 2012, 8:56am
9
Hi all,
Thanks for the solution.
What was happening is that
problem was about third parameter it must be false when you are rendering partially by default but when you are updating using ajax and json it should be true. and then it works like a charm fine.
now my fourth parameter is true always and i can put any ajax link inside it no problem at all.
Thanks
mdomba and Sampa
mdomba
(Maurizio Domba Cerin)
August 22, 2012, 8:59am
10
I’m glad you solved it… but I don’t understand you writing that the problem is the 3rd parameter… when on the #3 post you wrote you tried that already…
dipayan
(Johnmaclein Jm)
August 27, 2012, 5:15am
11
No there are two times when i need to do renderPartial
In that div for the first time when the page loads. It means in the main page.
when you are updating the div’s content by calling an action from the controller.
That time I had done the third parameter either (false, false) for both (1,2) situations
or I had done (true, true) for them.
Whereas it should be "false" for situation 1
and “true” for situation 2 in the third parameter’s place.
Thanks for the help.