开发者

Form needs to recalculate order price

I have a form for creation new order. One order can include severa开发者_开发问答l products. Each product has a price. Order's price equals to sum of prices of all product, included into it.

My form needs to update order's price, as user sets a quantity for each of the products.

Ideas ?


Use Javascript and the onchange HTML event on the quantity input fields.
When the user changed the quantity, the Javascript can process the form and calculate the new order price. Javascript is able to calculate the total price for a number of items, by using something like this:

item_quantity = document.getElementById('item_quantity').value;
item_price = document.getElementById('item_price').value;
// Perform some chacks here, to see whether the input values are valid
item_total = item_price * item_quantity;

Do this for all your items and add all item_total values to get the total order price.

By picking handy ids for your price and quantity fields, you can easily loop this and thus be independent of the amount of items in your order.


Since you're using ruby on rails i would you could use an observer, that could check the value change event of your quantity fields. That way you would be keeping the logic out of your view.

http://it.toolbox.com/blogs/database-geek/ror-passing-multiple-parameters-via-observe_field-18656

http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html

You could also just give your user an update quantities button that would submit the form to the same action so if the form is in new.html.erb then pressing the button would call the new action and would redirect them back to the same page, but you could update the total in the controller.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜