ajaxlink not update [SOLVED]

index view


<table><tr><td><?php

                 echo CHtml::ajaxLink('<img title="YouTube" src="' . Yii::app()->request->baseUrl . '/images/youtube-icon.png" alt="youtube-icon.png" />',$this->createUrl('displayYouTube'), array('update' => '#youtr'));

                          ?>  </td></tr> 

                        <tr id="youtr"> <?php if ($this->displayYouTube): ?> <td>display something</td></tr> <?php endif; ?>

                    </table>

sitecontroller:


...

public $displayYouTube=false;

    

    public function actionDisplayYouTube(){

         $displayYouTube=true;

    }

...

i cannot see the update

you will never see the effect of ajax because you are returning true which you cannot see. try in your action the following


public function actionDisplayYouTube(){

         print "success";

    }

but i have a $displayYouTube variable and an if clause in the view

Does the ajax link send the proper informations ?

Is then the given response the one you need ?

And finally, I’m not sure that assigning your id “youtr” to the <tr> tag will update the innerHtml of the <td> tag …

(btw, I’m absolutely not a jax expert)

the ajax link simply call the action,what i need is to update the tr with the id="youtr" after the action change the variable to show the content,i have tested with a div inside td but is the same result

this is how ajax calss should work ::)

does this help you ?

In a grid view, if ‘decollage’ field is null (db defaults), an ajaxLink with #id 'deco4, ‘deco2’ … is displayed, otherwise the $data->decollage value is shown.


		array(

			'name'=>'decollage',

			'type'=>'raw',

			'value'=>'(!$data->decollage)?CHtml::ajaxLink(

					"Decollage", 

					array("decollage", "id"=>$data->id),

					array(

						"success"=> "function(responseText) {

							$(\'#deco$row\').replaceWith(responseText);

						}"

					), 

					array("class"=>"btn","id"=>"deco$row"))

				:$data->decollage',

		),

The controller action:

it checks the ajaxRequest, load model, set the value and echo the result wich will replace (note the javascript ‘replaceWith’) the ajaxLink




	/**

	 * Set the decollage of a particular model.

	 * @param integer $id the ID of the vol model to be set as decollage

	 */

	public function actionDecollage($id)

	{

		if(Yii::app()->request->isAjaxRequest){

			$now=date('H').':'.date('i').':00';

			$model=$this->loadModel($id);

			$model->decollage=$now;

			$model->save();

		echo $now;

		}

	}



ok i understand but tell me what for the parameter update with the id??

each ajaxLink has his self id (#deco1, #deco2 and so on) :


array("class"=>"btn","id"=>"deco$row"))

this link is replaced by ajax:




"success"=> "function(responseText){

$(\'#deco$row\').replaceWith(responseText);

}"

for simple update, use "update" instead of "replaceWith" (I think so )

ok.But how to run the code


<?php if ($this->displayYouTube): ?>...

with the variable changed?




<table><tr><td>

<?php

echo CHtml::ajaxLink('<img title="YouTube" src="' . Yii::app()->request->baseUrl . '/images/youtube-icon.png" alt="youtube-icon.png" />',

$this->createUrl('displayYouTube'),

// this should work ... give a try

array("success"=>"function(responseText){$(\'#youtr\').update(responseText);}"),

// or maybe that can work too ?

array('update' => '#youtr'));

?>  

</td></tr> 

<tr id="youtr"><td>your initial content</td></tr> 

</table>



in your controller:




public function actionDisplayYouTube(){

$result = "<td>What ever you want to display</td>";

echo $result;

}



thank you

Be carefull I’ve just modified the code.

yes i know but i understood what you mean.its just an example

If you have the woking solution post it here and mark the topic as [SOLVED], this might help someone ;)

ok.just another issue.supposed i want to update a div id on layout main.php instead the index.php,how to do in the update?

btw: processing ad I suggest doesnt follow a strict MVC design because of creating html stuff in the controller, so there must be for sure a better solution, but I don’t know how to deal with that …

it works fine since the id is on page.Thanks

just change #youtr with #your_div_id