Yii Framework Forum: Raido Button with If statement - Yii Framework Forum

Jump to content

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

Raido Button with If statement Rate Topic: -----

#1 User is offline   ymuskie 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 07-October 10

Posted 28 October 2010 - 04:50 AM

HI,

I've a Radio button field with the value BUY and SELL. Currently, it will trigger the Ajax when user make the selection. I want to change it to only trigger the Ajax while user select "SELL". How to put the if statement?

Here is the code I'm using now:

	<?php
		$buysell = array('BUY'=>'BUY', 'SELL'=>'SELL');
		echo $form->radioButtonList($model,'buy_sell', $buysell, array(
																																		'separator'=>'', 
																																		'labelOptions'=>array('style'=>'float:left;'), 
																																		'style'=>'float:left;'
																																		'ajax' => array(
																																				'data'=>'js:{sql:"SELECT share_qty FROM tbl_transaction WHERE stock_id =\""+ $(this).val() + "\""}',
																																				'type'=>'GET', 
																																				'url'=>CController::createUrl('transaction/xsql'),
																																				'success'=>'js:function(data) {
																																					var dataArray = data.split(\',\');
																																					$("#'.CHtml::activeId($model,'share_qty').'").val(dataArray[0]);
																																				}'
																																		)
																																	));
	?>	


Thanks!
0

#2 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,450
  • Joined: 04-October 10

Posted 28 October 2010 - 05:23 AM

buddy, you are using a $model with a radioButtonList? I think it should be activeRadioButtonList isn't it?
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#3 User is online   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 28 October 2010 - 05:27 AM

View Postymuskie, on 28 October 2010 - 04:50 AM, said:

HI,

I've a Radio button field with the value BUY and SELL. Currently, it will trigger the Ajax when user make the selection. I want to change it to only trigger the Ajax while user select "SELL". How to put the if statement?

Here is the code I'm using now:

<?php
$buysell = array('BUY'=>'BUY', 'SELL'=>'SELL');
echo $form->radioButtonList($model,'buy_sell', $buysell, array(
  'separator'=>'', 
  'labelOptions'=>array('style'=>'float:left;'), 
  'style'=>'float:left;'
  'ajax' => array(

    'beforeSend'=>'... $(if this).val() ... return false; ...',

    'data'=>'js:{sql:"SELECT share_qty FROM tbl_transaction WHERE stock_id =\""+ $(this).val() + "\""}',
    'type'=>'GET', 
    'url'=>CController::createUrl('transaction/xsql'),
    'success'=>'js:function(data) {
      var dataArray = data.split(\',\');
      $("#'.CHtml::activeId($model,'share_qty').'").val(dataArray[0]);
    }'
  )
));
?>	


Thanks!

Possibly add a handler for the beforeSend event

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#4 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,450
  • Joined: 04-October 10

Posted 28 October 2010 - 05:27 AM

Apart from that the answer is simple:

CHtml::radioButtonList('buy_sell',0,array('buy'=>'buy','sell'=>'sell'),array('onclick'=>'if(this.value=="sell") alert(this.value);'));



instead of an alert put your ajax call.

Yii has great flexibility, and for that sometimes we try to Yiimaking everything forgetting what we know. I find easier to actually program the javascript within the onclick feature than by using or even remembering so many properties and options.

By the way, I think is a baaaad idea to put your sql code in the client javascript. Just the id in the data as a parameter and handle the rest on the server.
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
0

#5 User is online   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 28 October 2010 - 05:41 AM

View PostAntonio Ramirez, on 28 October 2010 - 05:27 AM, said:

Apart from that the answer is simple:

CHtml::radioButtonList('buy_sell',0,array('buy'=>'buy','sell'=>'sell'),array('onclick'=>'if(this.value=="sell") alert(this.value);'));



instead of an alert put your ajax call.

Yii has great flexibility, and for that sometimes we try to Yiimaking everything forgetting what we know. I find easier to actually program the javascript within the onclick feature than by using or even remembering so many properties and options.

By the way, I think is a baaaad idea to put your sql code in the client javascript. Just the id in the data as a parameter and handle the rest on the server.


You're absolutely right about the click handler approach. I got busy removing tabs instead of thinking about why I thought the ajax call would be mandatory. ;)
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#6 User is offline   Antonio Ramirez 

  • Elite Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 1,450
  • Joined: 04-October 10

Posted 28 October 2010 - 12:15 PM

glad to help
¿How long would it take for you to understand that you own nothing in this world?

www.ramirezcobos.com
www.2amigos.us
www.github.com/tonydspaniard
www.github.com/2amigos


Posted Image
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