Yii Framework Forum: Chtml::ajaxlink With Yii::app()->Request->Geturl(); - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

Chtml::ajaxlink With Yii::app()->Request->Geturl(); CHtml::ajaxLink with Yii::app()->request->getUrl(); Rate Topic: ***** 2 Votes

#21 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 873
  • Joined: 20-April 11
  • Location:India

Posted 26 September 2012 - 08:30 AM

in a view i have

<div class="panel" id="explorer_handler"></div>

echo CHtml::ajaxLink(
	'Test request',          // the link body (it will NOT be HTML-encoded.)
	array('/site/explorer'), // the URL for the AJAX request. If empty, it is assumed to be the current URL.
	array(
		'update'=>'#explorer_handler'
	)
);


in that controller

public function actionExplorer()
	{
		
		  $this->renderPartial('explorer',array());
	}


in explorer view i have the above links

<div class="panel" id="student_panel_handler">
   <?php .
.
.
.
.
$this->renderPartial('student_panel',array('model'=>$model,
		'list'=>$posts,
		)) ;
    ?>
        
    </div>





 



and in student_panel view

if(isset($_REQUEST['name']) and $_REQUEST['name']!=NULL)
{
echo CHtml::ajaxLink("name", Yii::app()->createUrl('/site/manage' ), array('type' =>'GET','data' =>array( 'name' => $name,'admissionnumber'=>'','Students[batch_id]'=>$bat ),'dataType' => 'text',  'update' =>'#student_panel_handler',array()));

}

if(isset($_REQUEST['admissionnumber']) and $_REQUEST['admissionnumber']!=NULL)
{
echo CHtml::ajaxLink("add.No", Yii::app()->createUrl('/site/manage' ), array('type' =>'GET','data' =>array( 'name' => $name,'admissionnumber'=>'','Students[batch_id]'=>$bat ),'dataType' => 'text',  'update' =>'#student_panel_handler',array()));

}

if(isset($_REQUEST['Students']['batch_id']) and $_REQUEST['Students']['batch_id']!=NULL)
{
echo CHtml::ajaxLink("Batch", Yii::app()->createUrl('/site/manage' ), array('type' =>'GET','data' =>array( 'name' => $name,'admissionnumber'=>'','Students[batch_id]'=>$bat ),'dataType' => 'text',  'update' =>'#student_panel_handler',array()));

}






<?php echo CHtml::ajaxLink('A', Yii::app()->createUrl('/site/manage' ),array('data' =>array('name' =>$name,'admissionnumber'=>$ad,'Students[batch_id]'=>$bat, 'val' => 'A'),'dataType' => 'text','update'=>'#student_panel_handler'),array()); 

<?php echo CHtml::ajaxLink('B', Yii::app()->createUrl('/site/manage' ),array('data' =>array('name' =>$name,'admissionnumber'=>$ad,'Students[batch_id]'=>$bat, 'val' => 'B'),'dataType' => 'text','update'=>'#student_panel_handler'),array()); 

<?php echo CHtml::ajaxLink('C', Yii::app()->createUrl('/site/manage' ),array('data' =>array('name' =>$name,'admissionnumber'=>$ad,'Students[batch_id]'=>$bat, 'val' => 'C'),'dataType' => 'text','update'=>'#student_panel_handler'),array()); 

<?php echo CHtml::ajaxLink('D', Yii::app()->createUrl('/site/manage' ),array('data' =>array('name' =>$name,'admissionnumber'=>$ad,'Students[batch_id]'=>$bat, 'val' => 'C'),'dataType' => 'text','update'=>'#student_panel_handler'),array());

and in thats action

public function actionManage()
{
.
.
.
.
.
$this->renderPartial('student_panel',array('model'=>$model,
		'list'=>$posts,
		)) ;

}

Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#22 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 873
  • Joined: 20-April 11
  • Location:India

Posted 26 September 2012 - 08:37 AM

these ajax links are simply working fine with only with parameter 'val'

?php echo CHtml::ajaxLink('A', Yii::app()->createUrl('/site/manage' ),array('data' =>array('val' => 'A'),'dataType' => 'text','update'=>'#student_panel_handler'),array()); 

<?php echo CHtml::ajaxLink('B', Yii::app()->createUrl('/site/manage' ),array('data' =>array('val' => 'B'),'dataType' => 'text','update'=>'#student_panel_handler'),array()); 

<?php echo CHtml::ajaxLink('C', Yii::app()->createUrl('/site/manage' ),array('data' =>array('val' => 'C'),'dataType' => 'text','update'=>'#student_panel_handler'),array()); 

<?php echo CHtml::ajaxLink('D', Yii::app()->createUrl('/site/manage' ),array('data' =>array('val' => 'C'),'dataType' => 'text','update'=>'#student_panel_handler'),array());


its only working because of these are the same links before update

when we pass a dynamic value , the previous value is passing..its showing the current dynamic value but passing previous one.
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#23 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,063
  • Joined: 16-February 11
  • Location:Japan

Posted 26 September 2012 - 09:00 AM

Well, I wanted to see the code that you have modified according to my suggestion.
Where did you register the javascript to attach the event handler?
And have you changed all of your CHtml::ajaxLink to CHtml::link?
0

#24 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 873
  • Joined: 20-April 11
  • Location:India

Posted 27 September 2012 - 12:26 AM

yes i changed it in student_panel view

<?php
Yii::app()->clientScript->registerScript('ajax-link-handler', "
$('body').on('click', '#student_panel_handler a', function(event){
	alert(1); exit;
        $.ajax({
                'type':'get',
                'url':$(this).attr('href'),
                'dataType': 'html',
                'success':function(data){
                        $('#student_panel_handler').html(data);
                }
        });
        event.preventDefault();
});
");
?>

 <?php if(isset($_REQUEST['name']) and $_REQUEST['name']!=NULL)
{
echo CHtml::link("name", array('/site/manage', 'name' => '','admissionnumber'=>$ad,'Students[batch_id]'=>$bat));
}
<?php if(isset($_REQUEST['admissionnumber']) and $_REQUEST['admissionnumber']!=NULL)
{
echo CHtml::link("", array('/site/manage',  'name' => $name,'admissionnumber'=>'','Students[batch_id]'=>$bat));
}
.
.
.
.
echo CHtml::link("All", array('/site/manage', 'name' => $name,'admissionnumber'=>$ad,'Students[batch_id]'=>''));

echo CHtml::link("A", array('/site/manage', 'name' => $name,'admissionnumber'=>$ad,'Students[batch_id]'=>''));



the links directly going to site/manage
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#25 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 873
  • Joined: 20-April 11
  • Location:India

Posted 27 September 2012 - 01:06 AM

<?php
Yii::app()->clientScript->registerScript('ajax-link-handler', "
$('body').on('click', '#student_panel_handler a', function(event){
        alert(1); exit;
        $.ajax({
                'type':'get',
                'url':$(this).attr('href'),
                'dataType': 'html',
                'success':function(data){
                        $('#student_panel_handler').html(data);
                }
        });
        event.preventDefault();
});
");
?>


when i put this code the ajaxbutton in that is also not working

<?php  echo CHtml::ajaxButton("Apply",CController::createUrl('/site/manage'), array(
					  
																									'data' => 'js:$("#search-form").serialize()',
																									'update' => '#student_panel_handler',
																									'type'=>'GET',
																									
																								     ),array());?>

Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#26 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,063
  • Joined: 16-February 11
  • Location:Japan

Posted 27 September 2012 - 01:57 AM

Try putting this part in the main view script.
<?php
Yii::app()->clientScript->registerScript('ajax-link-handler', "
$('body').on('click', '#student_panel_handler a', function(event){
	alert(1); exit;
        $.ajax({
                'type':'get',
                'url':$(this).attr('href'),
                'dataType': 'html',
                'success':function(data){
                        $('#student_panel_handler').html(data);
                }
        });
        event.preventDefault();
});
");
?>

0

#27 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 873
  • Joined: 20-April 11
  • Location:India

Posted 27 September 2012 - 02:13 AM

no way !! :(
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#28 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 873
  • Joined: 20-April 11
  • Location:India

Posted 27 September 2012 - 02:28 AM

i put this and links in a different view and a new action , but its not working..
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#29 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,063
  • Joined: 16-February 11
  • Location:Japan

Posted 27 September 2012 - 03:26 AM

Would you please start with this simplified version?
Does it popup the alert?

In your main view
<div class="panel" id="explorer_handler"></div>
...
<?php
echo CHtml::ajaxLink(
	'Test request',
	array('/site/explorer'),
	array(
		'update'=>'#explorer_handler'
	)
);
?>
...
<?php
Yii::app()->clientScript->registerScript('ajax-link-handler', "
$('body').on('click', '#student_panel_handler a', function(event){
	alert('link clicked');
	event.preventDefault();
});
");
?>


In the controller
public function actionExplorer()
{
	$this->renderPartial('explorer',array());
}


In explorer view
<div class="panel" id="student_panel_handler">
<?php
...
$this->renderPartial('student_panel',array('model'=>$model,
	'list'=>$posts,
)) ;
?>
</div>


In student_panel view
<?php
if(isset($_REQUEST['name']) and $_REQUEST['name']!=NULL)
{
	echo CHtml::link("name", array('/site/manage'));
}
if(isset($_REQUEST['admissionnumber']) and $_REQUEST['admissionnumber']!=NULL)
{
	echo CHtml::link("", array('/site/manage'));
}
...
echo CHtml::link("All", array('/site/manage'));
echo CHtml::link("A", array('/site/manage'));


In the controller
public function actionManage()
{
...
$this->renderPartial('student_panel',array('model'=>$model,
		'list'=>$posts,
	)) ;
}

0

#30 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 873
  • Joined: 20-April 11
  • Location:India

Posted 27 September 2012 - 04:42 AM

that is not working softark

this code alerts

if (!Yii::app()->clientScript->isScriptregistered("ajax-link-handler"))
   Yii::app()->clientScript->registerScript("ajax-link-handler","
      $('body').live('click',function(){alert(1);})
");

Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#31 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 873
  • Joined: 20-April 11
  • Location:India

Posted 27 September 2012 - 04:48 AM

this working but it alerts when click anywhere

Yii::app()->clientScript->registerScript('ajax-link-handler', "
$('body').live('click', '#student_panel_handler a', function(event){
        alert('link clicked');
        event.preventDefault();
});
");

Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#32 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 873
  • Joined: 20-April 11
  • Location:India

Posted 27 September 2012 - 05:19 AM

this works on click of link

Yii::app()->clientScript->registerScript('ajax-link-handler', "
$('#student_panel_handler a').live('click', '#student_panel_handler a', function(event){
        alert('link clicked');
		$.ajax({
                'type':'get',
                'url':$(this).attr('href'),
                'dataType': 'html',
                'success':function(data){
                        $('#student_panel_handler').show(data);exit;
                }
        });
        event.preventDefault();
});
");

Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#33 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 873
  • Joined: 20-April 11
  • Location:India

Posted 27 September 2012 - 05:47 AM

hey softark

this worked perfectly
Yii::app()->clientScript->registerScript('ajax-link-handler', "
$('#student_panel_handler a').live('click', function(event){
        $.ajax({
                'type':'get',
                'url':$(this).attr('href'),
                'dataType': 'html',
                'success':function(data){
                        $('#student_panel_handler').html(data);
                }
        });
        event.preventDefault();
});
");


solved
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#34 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,166
  • Joined: 05-January 12
  • Location:Morocco

Posted 27 September 2012 - 06:02 AM

$('body').on('click', '#student_panel_handler a', function(event){
    …

is correct, but

$('body').live('click', '#student_panel_handler a', function(event){
    …

is not.

http://api.jquery.com/on/
http://api.jquery.com/live/
0

#35 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 873
  • Joined: 20-April 11
  • Location:India

Posted 27 September 2012 - 06:34 AM

but this didnt work for me

$('body').on('click', '#student_panel_handler a', function(event){



but this worked

$('#student_panel_handler a').live('click', function(event){


thank u very much softark :) :) :)

i will give our app link, what we work together.
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#36 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,063
  • Joined: 16-February 11
  • Location:Japan

Posted 27 September 2012 - 07:16 AM

$('body').on('click', '#student_panel_handler a', function(event){

I really don't know why this doesn't work.

$('#student_panel_handler a').live('click', '#student_panel_handler a', function(event){

As bennouna says, this is not correct. Or, the 2nd parameter is useless.
So you may change it to ...
$('#student_panel_handler a').live('click', function(event){

I never used jQuery.live(), but if it serves you well, then it must be good.
0

#37 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 873
  • Joined: 20-April 11
  • Location:India

Posted 27 September 2012 - 08:10 AM

i think there are some $(document).ready scripts ..thats y.. anyway its working perfectly...
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#38 User is offline   chayo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 08-April 11

Posted 27 September 2012 - 08:43 AM

i think u should give unique id for your ajax link, and unbind click ajax event for last ajax link.

Yii::app()->clientScript->('unbindAjax','$("#ajaxLinkId").unbind("click")')

where #ajaxLinkId are the id of your last ajax link.
and use $this->renderPartial('viewName',array(),true,false);
every rendering view that have client script.

and avoid using jQuery('body').on("click") because it hard to unbind the click trigger event.
use jQuery("#ajaxLinkId").on("click") instead of jQuery("body").

if you still wanna use jQuery('body') u can use some namespace on it trigger event like
jQuery('body').on("click.namespace","#ajaxLinkId",function(e){$.ajax({})});
to unbind just use
Yii::app()->clientScript->('unbindAjax','$("body").unbind("click.namespace")')

and dont forget to disabled live event:
CHtml::$liveEvent=false;



I use this for my last property management project, that have lot of ajax on master form detail and every child form has an ajax.
0

#39 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 873
  • Joined: 20-April 11
  • Location:India

Posted 28 September 2012 - 12:59 AM

View Postchayo, on 27 September 2012 - 08:43 AM, said:

i think u should give unique id for your ajax link, and unbind click ajax event for last ajax link.



anyway it worked for me.
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • 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