Chtml::button problem, need help

Hi All,

I have another question about the ajaxbutton. In my page, i already have a first button. This button onClick will trigger controller function sell/loadProduct. In this loadProduct, i will renderPartial a form that allow user to key in info, i return it in JSON form and append to a div. In this form i have also include a ajaxbutton where to trigger the CJuiDialog. The problem now is why the button that generate form the view have the name id name with the first button? when i click the button, it don’t trigger the controller function uploadImage but tyrigger the first button function loadProduct. I have try to put id name but also doesn’t work. Can anyone help me?

Here is the button generate from view




CHtml::ajaxLink('Edit','',array('ajax' => array(

					'type'=>'POST',

					'dataType'=>'json',											

					'data'=>array('counter'=>$itemFormInfo['counter'],'server'=>$itemFormInfo['server'],'productType'=>$itemFormInfo['productType']),

					'url'=>CController::createUrl('sell/loadDialog'),	

					'success'=>'function(data){	

						 $("#uploadBox").dialog("open");

					}',					


					),								

				),array("id"=>'test'))



Please post your view code - especially the renderPartial. I suspect you’re running into the Yii/jQuery duplicate script problem.

Matt

ok Matt,

Please give me some time, i will get the the code to show you.

Here is the code

View index.php // the first page in view




<?php echo CHtml::Button('Add',array('ajax' => array(										'type'=>'POST',

'dataType'=>'json',	'data'=>array('listing_type'=>'js:document.getElementById("listing_type").options[document.getElementById("listing_type").selectedIndex].value',

															'item_pl'=>'js:document.getElementById("item_pl").value'),

											'url'=>CController::createUrl('sell/addList'),	

											'success'=>'function(data){

												if(data.status=="1"){

													switch(data.product_type){

													case "0":

													$("#currency_list").append(data.currency_field);

													document.getElementById("currency_pl").value = data.currency_pl;														

													break;

													case "4":

													$("#hla_list").append(data.hla_field);

													document.getElementById("hla_pl").value = data.hla_pl;

													break;

													case "5":

													$("#item_list").append(data.item_field);

													document.getElementById("item_pl").value = data.item_pl;

													break;

													}

													document.getElementById("product_counter").value = data.counter;	

													$("a#inline").fancybox();

													

													if(data.server=="all" && data.totalServerList > 50){

														loadForm(data.totalServerList,data.product_type);

													}													

												}else if(data.status=="2"){

													alert("Cannot create duplicate item.");

												}else{

													alert("Please select all the information.");

												}

											}',

									),)); ?>



view _curForm() Full code




<?php

$prodInfo = $modelC2cProductsListing->getProductInfo($formInfo['prodId'],'0');


echo '<div id="div_'.$formInfo['counter'].'">';

echo '<table width="900" cellpadding="0" cellspacing="0" border="0">';

echo '<tr><td width="30px" align="center" valign="top" style="padding-top:5px;">'.CHtml::CheckBox('cur_'.$formInfo['counter'],'', array ('id'=>'cur_'.$formInfo['counter'],'value'=>'Y',)).'</td>';

						

if($prodInfo['c2c_products_listing_id']!=""){

	echo '<td width="350px" align="left" valign="top" style="padding-top:5px;">'.$formInfo['gameName'].'<br>'.$formInfo['serverFullName'].'<BR>Reference # '.$prodInfo['c2c_products_listing_id'].' - <span style="color:blue">This listing already created</span><br><div id="errorMsg_'.$formInfo['counter'].'"></div>'.CHtml::hiddenField('title_'.$formInfo['counter'],$formInfo['gameName'].'<br>'.$formInfo['serverFullName']).'</td>';

	echo '<td width="130px" align="left" valign="top" style="padding-top:5px;">'.CHtml::textField('stock_'.$formInfo['counter'],$prodInfo['available_quantity'],array('id'=>'stock_'.$formInfo['counter'],'class'=>'TextField','size'=>15)).'</td>';

	echo '<td width="180px" align="left" valign="top" style="padding-top:5px;">'.CHtml::dropDownList('curCurrency_'.$formInfo['counter'],'',array('RMB' => 'RMB', 'USD' => 'USD'),array('options' => array($prodInfo['products_base_currency']=>array('selected'=>true)))).' '.CHtml::textField('curPrice_'.$formInfo['counter'],$prodInfo['products_price'], array('id'=>'curPrice_'.$formInfo['counter'],'class'=>'TextField', 

   'size'=>12)).'</td>';		   

	echo '<td width="180px" align="left" valign="top" style="padding-top:5px;">';

	$delivery = $modelProductsDeliveryMode->getDeliveryMethod("0");

	$splitDm = split(",",$prodInfo['delivery_mode']);

						

	foreach($delivery as $deliveryMethod){

		$checked = "";

		for($i=0;$i<count($splitDm);$i++){

			if($splitDm[$i] == $deliveryMethod['products_delivery_mode_id']){

				$checked = 'checked';

			}

		}

		echo CHtml::CheckBox($formInfo['counter'].'_dm[]',$checked, array ('id'=>$formInfo['counter'].'_'.$deliveryMethod['products_delivery_mode_id'].'_dm','value'=>$deliveryMethod['products_delivery_mode_id'],)).' 

		&nbsp;&nbsp;<span id="'.$formInfo['counter'].'_'.$deliveryMethod['products_delivery_mode_id'].'_dmText" style="float;left" onClick="checkPm(\''.$formInfo['counter'].'_'.$deliveryMethod['products_delivery_mode_id'].'_dm\')">'.$deliveryMethod['products_delivery_mode_title'].'</span><br>';

	}

}else{

	echo '<td width="350px" align="left" valign="top" style="padding-top:5px;">'.$formInfo['gameName'].'<br>'.$formInfo['serverFullName'].'<br><div class="errorMsg" id="errorMsg_'.$formInfo['counter'].'"></div>'.CHtml::hiddenField('title_'.$formInfo['counter'],$formInfo['gameName'].'<br>'.$formInfo['serverFullName']).'</td>';

	echo '<td width="130px" align="left" valign="top" style="padding-top:5px;">'.CHtml::textField('stock_'.$formInfo['counter'],'',array('id'=>'stock_'.$formInfo['counter'],'class'=>'TextField','size'=>15)).'</td>';

	echo '<td width="180px" align="left" valign="top" style="padding-top:5px;">'.CHtml::dropDownList('curCurrency_'.$formInfo['counter'],'',array('RMB' => 'RMB', 'USD' => 'USD')).' '.CHtml::textField('curPrice_'.$formInfo['counter'],'', array('id'=>'curPrice_'.$formInfo['counter'],'class'=>'TextField', 

   'size'=>12)).'</td>';

	echo '<td width="180px" align="left" valign="top" style="padding-top:5px;">';

					

	$delivery = $modelProductsDeliveryMode->getDeliveryMethod("0");

	foreach($delivery as $deliveryMethod){

		echo CHtml::CheckBox($formInfo['counter'].'_dm[]','', array ('id'=>$formInfo['counter'].'_'.$deliveryMethod['products_delivery_mode_id'].'_dm','value'=>$deliveryMethod['products_delivery_mode_id'],)).' 

		&nbsp;&nbsp;<span id="'.$formInfo['counter'].'_'.$deliveryMethod['products_delivery_mode_id'].'_dmText" style="cursor:pointer" onClick="checkPm(\''.$formInfo['counter'].'_'.$deliveryMethod['products_delivery_mode_id'].'_dm\')">'.$deliveryMethod['products_delivery_mode_title'].'</span><br>';

	}

}


echo'</td>';

echo '<td width="30px" align="center" valign="top"><div style="cursor:pointer;" onClick="removeCurList(\''.$formInfo['counter'].'\')">X</div></td></tr>';

echo '<tr><td height="10px" colspan="6"></td></tr>';

echo '<tr><td height="25px" colspan="6"><div id="errorMsg_'.$formInfo['counter'].'"></div></td></tr>';

echo '<tr><td height="10px" colspan="6"></td></tr>';

echo '</table>';

echo '</div>';

?>



Controller sell/addList




public function actionAddList(){

... code

	switch($productType){

	case '0': // Currencies	

        foreach($a as $<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='B)' />{   // the foreach loop to create the form

$currency_field .= $this->renderPartial('_curForm',array('modelC2cProductsListing'=>$modelC2cProductsListing,'modelProductsDeliveryMode'=>$modelProductsDeliveryMode,'formInfo'=>$formInfo), true);

        }

        break;

..... code	

echo CJSON::encode(array(

			'product_type'=>$productType,

			'currency_field'=>$currency_field,  // here i return the form in json


));

		

}



there is bunch of code and i try to collect the part relate to the button.

Each row of the form is going to append in the <div id="currency_field"></div>

Hi,

[list=1][]Using Firefox’s Debug, look to see if there are any duplicate scripts being added in the renderPartial.[list=1][]Your main view includes jQuery.js etc. Check to see that the renderPartial isn’t sending the same scripts again.[]I suspect that this is where the problem lies.[/list][]You can remove the duplicate scripts with Yii::app()->clientscript->scriptMap[‘jquery.js’] = false;[list=1][*]Put this code (for the duplicate scripts) in your renderPartial view.[/list][/list]

Gotta run to work, will take another look tonight.

Cheers,

Matt

Change the following line:




$currency_field .= $this->renderPartial('_curForm',array('modelC2cProductsListing'=>$modelC2cProductsListing,'modelProductsDeliveryMode'=>$modelProductsDeliveryMode,'formInfo'=>$formInfo), true, TRUE); // process output



You may end up downloading the same scripts again and again. I recommend you to follow a different approach, one that attachs a pre-registered javascript script and that handles the ‘click’ events on the rendered buttons. Once, you load AJAX content, call the function that binds the events.

Cheers

Hi Matt,

Yesterday i have send you reply but didn’t save in the topic…zzz. I have follow the step to check with firefox, doesn’t saw any multiple included js, i also have try to put the Yii::app()->clientscript->scriptMap[‘jquery.js’] = false; script but it doesn’t solve the bug.

Hi Antonio,

I have try to change the script that you advice but doesn’t solve the bug yet. The button id still follow the “Add” button id and trigger the “Add” button url controller. Can you show me some link related so i am able to study on that?

Hi Matt and antonio,

Thanks for your advice, i finally can found the bug. It is related to the jquery js file that calling from EAjaxUpload. I have focus on firebug console and finally found the imageUpload.php file call from controller has include again the jquery.js, after i insert Yii::app()->clientscript->scriptMap[‘jquery.js’] = false; to that page, it already solve half of the problem. I also change the button from ajaxButton to button, remove the ajax trigger but add in button onClick to trigger ajax function and it works!!! It solve the button problem that keep calling the first button function. Yeah…

chleong :)