Yii Framework Forum: ajaxLink does not work - Yii Framework Forum

Jump to content

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

ajaxLink does not work Rate Topic: ****- 1 Votes

#1 User is offline   jrn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 104
  • Joined: 03-October 11
  • Location:Los Angeles

Posted 16 May 2012 - 09:05 AM

Hi,

I am trying to use ajaxLink but am failing horribly.
I have a view which is getting rendered by the command renderPartial after an ajax call.
This new subview contains some links. I am trying to generate ajaxLinks with the following command:
echo CHtml::ajaxLink('This is an ajaxLink', array('site/showText'), array('update'=>'#content'));


I can see the link 'This is an ajaxLink' which also has an id assigned by yii (yt0).

However, I can not see any javascript added to my view file which would correspond with this generated id. Is this due to the fact that I am rendering the view partially?

There is no activity clicking on the ajaxLink - monitoring with firebug.

Thanks a lot in advance for your help :)
0

#2 User is offline   Haensel 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 444
  • Joined: 14-January 11
  • Location:Vienna (Austria)

Posted 16 May 2012 - 09:15 AM

a classic:
On an initial GET request the whole view is rendered and the JS is included. But that's not true for rendered partials including javascript. Anyways, there is a way to tell Yii to process the rendered output of a view to also add the newly generated js by setting the fourth parameter of renderPartial to true:

$this->renderPartial('_partial',array('data'=>$data),false,true);


Some additional hints:
1. Always assign your own ids to everything that uses JavaScript. The ids generated by Yii are easily duplicated when reloading partials which can cause weird stuff to happen (multiple ajax requests on one click etc.).
2. Some scripts may now be downloaded on every partial rendering action. Jquery for example. If that happens you can use

Yii::app()->clientScript->scriptMap=array("jquery.js"=>false, 'duplicatedScrip.js'=>false)

to prevent that from happening
2

#3 User is offline   jrn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 104
  • Joined: 03-October 11
  • Location:Los Angeles

Posted 16 May 2012 - 09:33 AM

Awesome! This did the trick! Thank you very much Hänsel.

May I ask you some more questions?

1. If scripts get downloaded more than once would I use this
Yii::app()->clientScript->scriptMap=array("jquery.js"=>false, 'duplicatedScrip.js'=>false)


in the view file?

2. I'd like to pass a variable to the controller through the ajaxLink.
I am using
echo CHtml::ajaxLink('This is an ajaxLink', array('site/showText/variable/value'), array('update'=>'#content'));


and access the value of the variable with $_GET. Is this the best way to do it? :)
0

#4 User is offline   Haensel 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 444
  • Joined: 14-January 11
  • Location:Vienna (Austria)

Posted 16 May 2012 - 10:20 AM

View Postjrn, on 16 May 2012 - 09:33 AM, said:

Awesome! This did the trick! Thank you very much Hänsel.


You're welcome ;)

View Postjrn, on 16 May 2012 - 09:33 AM, said:

1. If scripts get downloaded more than once would I use this
Yii::app()->clientScript->scriptMap=array("jquery.js"=>false, 'duplicatedScrip.js'=>false)

in the view file?


You can, yes. But I always do this in the controller action wrapped in an "isAjaxRequest check" like

if(Yii::app()->request->isAjaxRequest)
    Yii::app()->clientScript->scriptMap=array("jquery.js"=>false, 'duplicatedScrip.js'=>false)


View Postjrn, on 16 May 2012 - 09:33 AM, said:

2. I'd like to pass a variable to the controller through the ajaxLink.
I am using
echo CHtml::ajaxLink('This is an ajaxLink', array('site/showText/variable/value'), array('update'=>'#content'));


and access the value of the variable with $_GET. Is this the best way to do it? :)


Try
echo CHtml::ajaxLink('This is an ajaxLink', array('site/showText','parameter'=>'value'), array('update'=>'#content'));


Cheers
1

#5 User is offline   jrn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 104
  • Joined: 03-October 11
  • Location:Los Angeles

Posted 16 May 2012 - 10:55 AM

Great! Thanks again for all your advice :)
0

#6 User is offline   codesutra 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 596
  • Joined: 15-March 11
  • Location:India

Posted 16 July 2012 - 01:25 PM

View PostHaensel, on 16 May 2012 - 10:20 AM, said:

You're welcome ;)



You can, yes. But I always do this in the controller action wrapped in an "isAjaxRequest check" like

if(Yii::app()->request->isAjaxRequest)
    Yii::app()->clientScript->scriptMap=array("jquery.js"=>false, 'duplicatedScrip.js'=>false)




Try
echo CHtml::ajaxLink('This is an ajaxLink', array('site/showText','parameter'=>'value'), array('update'=>'#content'));


Cheers



Hey Haensel,

first of all thanks for your great post.

But i have a trouble here with this code.Well i have also done the same thing as you have guided.But What is my problem here is :

i have a Cgrid view Which i am showing with ajax call and with the help of renderPartial view

In this Cgrid view i have a CCheckBoxColumn.

my whole application is running with Ajax calls and renderPartial views.So, When call ajax for first time and load a renderPartial view with then perform a selection of checkboxes and hit a ajax Button for any other ajax call(Delete a multiple records) then it render a same Cgrid view with a updated results.

But then i try to select a checkboxes then only a main CheckBox(Which perform select All checkbox) is working like checked All or unchecked All.But child check boxes are not selectable individually.

I have a one more button which peform a Add record Ajax call which load a CActivForm to add a record.So,After Adding a record it again load that View which contain a Gird View with CCheckBoxColumn.So, when i try to select an individual checkbox then its not working.Only select All or unselect All action can be performed. :(

I know it is just an issue of redundancy of scripts.But your suggested solution is not working here.

Here is my code

For Controller :-
public function actionChangeDropdownView($dropdown){

		switch ($dropdown){
			case 'state':
				if(Yii::app()->request->isAjaxRequest)
				{
					Yii::app()->clientscript->scriptMap=array("jquery.js"=>false, 'duplicatedScrip.js'=>false);
				}
				$dataProvider=new CActiveDataProvider('State', array(
                               'pagination'=>array(
                               'pageSize'=>15,
				)));
				$this->renderPartial('_loadstate',array('dataProvider'=>$dataProvider),false,true);
				break;
			case 'costUnit':
				if(Yii::app()->request->isAjaxRequest)
				{
					Yii::app()->clientscript->scriptMap=array("jquery.js"=>false, 'duplicatedScrip.js'=>false);
				}
				$dataProvider=new CActiveDataProvider('Unit', array(
                               'pagination'=>array(
                               'pageSize'=>15,
				)));
				$this->renderPartial('_loadunit',array('dataProvider'=>$dataProvider),false,true);
				break;
			case 'industry':
				if(Yii::app()->request->isAjaxRequest)
				{
					Yii::app()->clientscript->scriptMap=array("jquery.js"=>false, 'duplicatedScrip.js'=>false);
				}
				$dataProvider=new CActiveDataProvider('Industry', array(
                               'pagination'=>array(
                               'pageSize'=>15,
				)));
				$this->renderPartial('_loadindustry',array('dataProvider'=>$dataProvider),false,true);
				break;
		}
	}

public function actionAddStateProcess(){

		$model= new State('addState');
		$model->scenario='addState';

		if(isset($_POST['State']))
		{
			$model->attributes=$_POST['State'];
			$model->save();
		}

		if(Yii::app()->request->isAjaxRequest)
		{
			Yii::app()->clientscript->scriptMap=array("jquery.js"=>false, 'duplicatedScrip.js'=>false);
		}

		$dataProvider=new CActiveDataProvider('State', array(
                                'pagination'=>array(
                                'pageSize'=>15,
		//'params' => array('city' => $cityId,'sortby'=>$sortBy),
		)));
		$this->renderPartial('_loadstate',array('dataProvider'=>$dataProvider),false,true);
	}




This is my code for View file :-

     
					echo CHtml::beginForm('','post',array('id'=>'state-list-form'));
					$this->widget('zii.widgets.grid.CGridView', array(
					    'dataProvider'=>$dataProvider,
					    'id'=>'stateGird',
					    'selectableRows'=>2, // multiple rows can be selected
					    'columns'=>array(
					        array(
					            'class'=>'CCheckBoxColumn',
					            'id'=>'checkState',
					        ),
					      //  'id',
					        'state_name',
					        /*
					         *  array(            // display a column with "view", "update" and "delete" buttons
					            'class'=>'CButtonColumn',
					           // 'template'=>'{delete}',
					        ),
					         */
					    ),
					));
					echo CHtml::endForm();



Please suggest me something what wrong i am doing here.

Thanks in Advance
jayant
CodeSutra
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