Yii Framework Forum: ajaxLink + renderPartial + JQuery (load again) - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

ajaxLink + renderPartial + JQuery (load again) Rate Topic: -----

#1 User is offline   acainoks 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 16-November 10
  • Location:Poznań - Poland

Posted 15 March 2011 - 10:36 AM

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...
1

#2 User is offline   Gustavo 

  • Master Member
  • Yii
  • Group: Moderators
  • Posts: 916
  • Joined: 27-July 10
  • Location:Curitiba - Brasil

Posted 15 March 2011 - 10:39 AM

the code should be
$this->renderPartial($view, array('id' => $id, 'array'=> $arr));


and that should display your view without a layout and without JS
--
Extensions:
translate modue - module to handle translations
multiActiveRecord - db selection in models
redisCache - redis cache component
mpCpanel - interact with cpanel api
mUploadify - use uploadify uploader in your application

Gustavo Salomé Silva
0

#3 User is offline   acainoks 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 16-November 10
  • Location:Poznań - Poland

Posted 15 March 2011 - 11:01 AM

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

also do nothing, maybe JQuery is unseen for ajaxLink ?
0

#4 User is offline   Gustavo 

  • Master Member
  • Yii
  • Group: Moderators
  • Posts: 916
  • Joined: 27-July 10
  • Location:Curitiba - Brasil

Posted 15 March 2011 - 11:10 AM

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
--
Extensions:
translate modue - module to handle translations
multiActiveRecord - db selection in models
redisCache - redis cache component
mpCpanel - interact with cpanel api
mUploadify - use uploadify uploader in your application

Gustavo Salomé Silva
0

#5 User is offline   Anupam 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 90
  • Joined: 28-February 10
  • Location:INDB, India

Posted 15 March 2011 - 11:40 AM

View Postacainoks, on 15 March 2011 - 10:36 AM, said:

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 }) );'))

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


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.

This post has been edited by Anupam: 15 March 2011 - 07:43 PM

0

#6 User is offline   Anupam 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 90
  • Joined: 28-February 10
  • Location:INDB, India

Posted 15 March 2011 - 11:45 AM

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 ...
0

#7 User is offline   acainoks 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 16-November 10
  • Location:Poznań - Poland

Posted 15 March 2011 - 03:37 PM

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
0

#8 User is offline   acainoks 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 16-November 10
  • Location:Poznań - Poland

Posted 15 March 2011 - 05:21 PM

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 ??
0

#9 User is offline   Gustavo 

  • Master Member
  • Yii
  • Group: Moderators
  • Posts: 916
  • Joined: 27-July 10
  • Location:Curitiba - Brasil

Posted 15 March 2011 - 06:44 PM

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

try to include it somewhere else, in the view for example
--
Extensions:
translate modue - module to handle translations
multiActiveRecord - db selection in models
redisCache - redis cache component
mpCpanel - interact with cpanel api
mUploadify - use uploadify uploader in your application

Gustavo Salomé Silva
0

#10 User is offline   Anupam 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 90
  • Joined: 28-February 10
  • Location:INDB, India

Posted 15 March 2011 - 07:37 PM

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');


Quote

Anupam this is scenario:
1)create new DIV with id = contentDIV
2)And AFTER that load data in to new DIV


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)
                );

0

#11 User is offline   acainoks 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 16-November 10
  • Location:Poznań - Poland

Posted 16 March 2011 - 03:47 AM

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 ?
0

#12 User is offline   Anupam 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 90
  • Joined: 28-February 10
  • Location:INDB, India

Posted 16 March 2011 - 06:01 PM

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.
0

#13 User is offline   acainoks 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 16-November 10
  • Location:Poznań - Poland

Posted 19 March 2011 - 08:35 AM

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()???

Attached File(s)


0

#14 User is offline   Command 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 33
  • Joined: 06-September 10
  • Location:Berlin

Posted 19 March 2011 - 07:41 PM

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.yiiframew...ssOutput-detail )
0

#15 User is offline   acainoks 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 16-November 10
  • Location:Poznań - Poland

Posted 20 March 2011 - 06:30 PM

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
0

#16 User is offline   acainoks 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 16-November 10
  • Location:Poznań - Poland

Posted 21 March 2011 - 03:55 PM

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..... <_<
0

#17 User is offline   kareem zok 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 18-January 10
  • Location:lebanon

Posted 30 October 2011 - 04:19 PM

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
Kareem zock
Software engineer

T: +(961) 03 497265
E: kareemzok@gmail.com
Skype: kareem_zok

Posted Image Linkedin

Posted Image Facebook

Posted Image Twitter

About me.com
0

#18 User is offline   kareem zok 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 18-January 10
  • Location:lebanon

Posted 30 October 2011 - 04:57 PM

i found the solution here >> http://www.yiiframew...e-ajax-request/
Kareem zock
Software engineer

T: +(961) 03 497265
E: kareemzok@gmail.com
Skype: kareem_zok

Posted Image Linkedin

Posted Image Facebook

Posted Image Twitter

About me.com
0

#19 User is offline   Jesse 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 15
  • Joined: 04-August 11
  • Location:North Vancouver, BC

Posted 26 April 2013 - 11:51 PM

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
0

#20 User is offline   StreetCoder 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 58
  • Joined: 13-April 13

Posted 20 March 2014 - 02:47 AM

it works for me man, thank you
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users