ymuskie
(Muskie88)
October 28, 2010, 9:50am
1
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!
buddy, you are using a $model with a radioButtonList? I think it should be activeRadioButtonList isn’t it?
tri
(tri - Tommy Riboe)
October 28, 2010, 10:27am
3
ymuskie:
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
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.
tri
(tri - Tommy Riboe)
October 28, 2010, 10:41am
5
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.