开发者

Yii framework....Calculate total on fly

I'm newbie in Yii framework and start learning on small web project so I got stuck on this position as how to solve it.

I have a 'Order_form' where a user has confirm his order and if want to change the 'Qty' so only this can change then bydoing this I need to change the Total value to be calculate on fly on same form if Qty got changed by user.

beginWidget('CActiveForm', array( 'id'=>'order-form', 'enableAjaxValidation'=>false, )); ?>
<div class="row">
        <?php echo $form->labelEx($model,'price'); ?>
        <?php echo $model->price; ?>
</div>

<div class="row">
        <?php echo $form->labelEx($model,'qty'); ?>
        <?php echo $form->textField($model,'qty'); ?>
        <?php echo $form->error($model,'qty'); ?>
</div>

<div class="row">
        <?php echo $form->labelEx($model,'total'); ?>
        <?php echo $form开发者_JAVA技巧->textField($model,'total'); ?>
</div>

endWidget(); ?>

$(document).ready(function() { var myVar = '999'; $("#model_total").val(myVar); }); '

Here I updated my script to clarify what I need, so here I'm trying to pass a value of '999' to 'Total' on the same form but this doesn't work. It might be that I'm missing/doing something wrong.

I would also like other way to calculate Qty x Price and get the result from the controller if this is possible if so please guide through code.


One reason the JS you have might not be working is: are you SURE the "total" field ID is "model_total"? Usually it's something like "Order_total". Look at your html source to verify (Firebug helps).

Anyway, some other suggestions:

A common way to resolve this order total problem is an "Update Cart" button, that POSTs the form back to the controller where the order total is calculated, and then the page rendered again with the new total.

Another way to do this would be to set up an onBlur live() (or delegate) listener that does one of two things (similar to what you are trying now):

(1) POST the form to the controller where the total is recalculated. Something like this (untested):

$("#order-form #Order_qty").live('blur',function() {
  $.ajax({
    'type':'POST',
    'data':$('#order-form').serialize(),
    'success':function(data){$('#Order_total').val(data)}
  });}
);

You will need to have something in your controller action like this:

if(isset($_POST['ajax']) && $_POST['ajax']==='order-form' && isset($_POST['Order'])) {
  // calculate new total based on the $_POST Model_qty field
  echo $new_total;
  Yii::app()->end();
}

(2) You could do the calculation totally in JS, and update it like you are trying to do now. Just remember you cannot update the order qty in the database this way, sine you are not hitting the controller.

$("#order-form #Order_qty").live('blur',function() {
  $('#Order_total').val($('#Order_price').val() * $('#Order_qty').val());
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜