ajaxLink + renderPartial + JQuery (load again)

Hi,

in my main.php I have :


<?php Yii::app()->clientScript->registerCoreScript('jquery');?>

I load by ActionLoad content in new DIV and there(loaded view) I have ajaxLink :


CHtml::ajaxLink('CLICK', array($this->createUrl('map/trade'),'cityID'=> $cityID ), array(

			'type' => 'POST',

			'update'=>'#contentDIV'), 

			array('onclick'=>'$("#wrapper").prepend($("<div>").attr("id","contentDIV").addClass("trade").offset({ top: 150, left: 500 }) );'))

And ActionLoad :


public function actionLoad()

    {

        $view = $_POST['array'][0];

	$arr = (isset ($_POST['array'][1])) ? $_POST['array'][1] : null;

	$id  = Yii:: app () -> user -> id;


        $this->renderPartial($view, array('id' => $id, 'array'=> $arr), false, [b]true[/b]);

    }

OK when last variable is TRUE > JQuery is load again and method POST not working , but new view is displayed,

when is FALSE do nothing… :(

I wont display new view in contentDIV with POST and not load again JQuery…

the code should be




$this->renderPartial($view, array('id' => $id, 'array'=> $arr));

and that should display your view without a layout and without JS

Gustavo, this:


$this->renderPartial($view, array('id' => $id, 'array'=> $arr));

also do nothing, maybe JQuery is unseen for ajaxLink ?

the thing is

jquery is ready to be loaded when you registerCoreScript(‘jquery’);

if you call renderPartial JS files wont be included, only if the 4rd parameters is true

you should use


$this->render($view,array('...'));

and if you dont need a layout set it to false

ur ajaxlink code is ambiguous. u r writing 2 click event code for that link, ajaxoption update and an ‘onclick’.

that’s why its not working.

writing the below code will itself on a click, update the ‘#contentDIV’ with new content.


CHtml::ajaxLink('CLICK', array($this->createUrl('map/trade'),'cityID'=> $cityID ), array(

			'type' => 'POST',

			'update'=>'#contentDIV'),)



wht i got here is u want to update the ‘#contentDIV’ and also add a new ‘div’ element to the ‘#wrapper’. so u should write the ‘success’ option rather than ‘update’. like this:




CHtml::ajaxLink('CLICK', array($this->createUrl('map/trade'),'cityID'=> $cityID ), array(

			'type' => 'POST',

			'datatype'=>'html',

			'success'=>'function(data){

				$("#contentDIV").html(data);

				$("#wrapper").prepend("<div id=\"someId\" class=\"trade\" style=\"  \"> </div>");

				}',)

		);



renderPartial code seems OK.


$this->renderPartial($view, array('id' => $id, 'array'=> $arr), false, true);

try it…

[EDIT]: using ajaxlink with both ‘success’ and ‘onclick’, works fine. i doubt but it works. but i don’t wanna rely on that. if had multiple statements for ajax call, should write in the same function.

1 more flaw is that on ur ajaxlink u r trying to update ‘#contentDIV’ and also want to create new div inside ‘#wrapper’ with an id ‘contentDIV’.

so this is confusing 2 elements of same type with same id…

what u r actually trying to achieve …

OK

I have one layer where content is load by AJAX, in loaded view I have this code:


...

echo "<td>". CHtml::ajaxLink('HANDEL', array('map/trade/','cityID'=> $cityID ),

       array('update'=>'#contentDIV'),array('onclick'=>'alert(4)'))."</td>";

echo "<td>". CHtml::link('Wesprzyj',array('map/trade/'),array('onclick'=>'alert(4)')) ."</td>";

....

And ajaxLink not working, simple link display alert

When this same code :


echo CHtml::ajaxLink('HANDEL', array('map/trade/','cityID'=> $cityID ),

       array('update'=>'#contentDIV'),array('onclick'=>'alert(4)'));

I put in to main page - it’s working, why? JQuery problem?

Anupam this is scenario:

1)create new DIV with id = contentDIV

2)And AFTER that load data in to new DIV

I think I found solution…

when I wrote i loaded view:


echo  json_encode(Yii::app()->clientScript->isScriptRegistered('jquery'));

displayed ‘false’, what I must write in loaded view to use JQuery script from main page ??

renderPartial doesnt use layout, and you are including jquery script inside the layout

try to include it somewhere else, in the view for example

On the main page u include the jquery as core script, now on every page u load it’ll be available


yii::app()->getClientScript()->registerCoreScript('jquery');

now code ur ajaxlink like this. here as u needed, first created the new div #contentDIV & then loaded the ajax return data into that div.




CHtml::ajaxLink('CLICK', array($this->createUrl('map/trade'),'cityID'=> $cityID ), array(

                        'type' => 'POST',

                        'datatype'=>'html',

                        'success'=>'function(data){

                                $("#wrapper").prepend("<div id=\"contentDIV\" class=\"trade\" style=\"  \"> </div>");

                                $("#contentDIV").html(data);

				return false;

                                }',),

		array('id'=>'some-id', 'live'=>false)

                );



Anupam I tried Your code and also zero response, it working when in renderPartial 4th variable is TRUE, but load again JQuery


public function actionLoadCityData()

    {

		$id  = Yii:: app () -> user -> id;

        $this->renderPartial('load_cityData', array('id' => $id, 'cityID' => $_POST['cityID']),false,true);

    }

Gustavo I put this code in my view and also not working…


Yii::app()->clientScript->registerScriptFile('assets/51651de/jquery.js',CClientScript::POS_BODY);

what is $processOutput ?

thats what to be done, 4th parameter should be true if u want scripts to be executed from thr renderpartial output.

register jquery as in my last post.

Anupam I have this code on my main page :


<?php Yii::app()->clientScript->registerCoreScript('jquery');?>

And 1st step (see image below) works good, and in page source code is JQuery script code for Button1.

2nd step not working, and no js script for “another ajax button” … registerScript()???

are you generating the view for the ajax event with the renderpartial with the 4th param set to true (that is needed because without it yii would try to append the script to the others that are generated on loading the page, but this has already happened).

$processoutput do that for you ( http://www.yiiframework.com/doc/api/1.1/CController#processOutput-detail )

OK I decide to create simple button with id ‘button1’ and render this code on the end of loaded content:


$('#button1').click(function(){jQuery.ajax({

	'type':'POST',

	'datatype':'html',

	'data':{'cityID': $someCityID},

	'success':function(data){

		closeContextDIV ();

		$('#wrapper').prepend('<div id=\"contextDIV\" class=\"trade\" style=\"  \"> </div>');

        $('#contextDIV').html(data);

        return false;},

	'url':'/gra/game/index.php?r=map/trade','cache':false});

	return false;})

now question is : How render this js code? step by step … :)

thanks

I choose most brutal way to solve my problem:


echo '<script language="javascript" type="text/javascript">';

	echo "$('#".$button1."').click(function(){jQuery.ajax({

	'type':'POST',

	'datatype':'html',

	'data':{'cityID':".$cityID."},

	'success':function(data){

		closeContextDIV ();

		$('#wrapper').prepend('<div id=\"contextDIV\" class=\"trade\" style=\"  \"> </div>');

        $('#contextDIV').html(data);

        return false;},

	'url':".Yii::app()->createUrl("/map/trade").",'cache':false});

	return false;});";

echo '</script>';

and working… <_<

i m having a problem concerning ajaxlink even if i put the 4rd param in renderpartial

i have 2 link ajax link in 2 different sction the first is to publish an item when it s activated and the section if item is not activated we need to click reactivate then we can click publish to let user see it or he can delete it and all 3 links are ajaxlink in the same view , my prob is

when i click reactivate for item then i want to click publish . this item and the one flowing it are published its like the param id for both is sent whil ei just want thefirst to b publish . and when i go to the page view and click publish nothin bad happen . and same issue when i click delete any help ? :S

i found the solution here >> http://www.yiiframework.com/wiki/178/how-to-avoid-multiple-ajax-request/

Hi, I tried most of these solutions on this thread, but nothing worked. I found this simple solution posted by glyph. The Ajax call would work the first time, but not the second time around.

"add to a controller action method will prevent an ajax-loaded div from clobbering jQuery.yii" -Glyph


Yii::app()->clientScript->scriptMap['*.js'] = false;

Original URL: 4932-jquery-yii-is-undefined-after-ajax-request/page__p__154810#entry154810

it works for me man, thank you