I have a form for creation new order. One order can include several 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.
Related
I am struggling to find the right formula for my table. I have a database that I want to filter per date and get the total of each product on a different page.
As I need to prepare these products per day I would like to avoid counting every product by hand.
I have this formula =SUMIF(E40:E52,"Reese - box of 4*",D40:D52)that work without filter.
How can I do this function with filter?
image
My second question is about when an order as more than one product.Like here, we have a box of 4 that contains the product behind.
image
How can I get a count of the total number of each product, applying the filter option as well?
Thanks a lot for your help!
I'm trying to order or rank a contacts phone numbers in rails so that users can change the order of a phone number. I'm using the nested form gem if that helps.
The idea is that I want to rank them based the order in which they come in. so the user could move the phone number field up or down the list and then based on what position in the list the number is in, it would be ranked in that order.
so like it would be listed as
phone1
phone2
phone3
phone4
then the user could change the order to
phone2
phone4
phone1
phone3
and rails would know to re order them, ie,
change phone2's rank to phone1
phone 4's rank to phone2
etc.
I could change the order on the front end, but what that feels like there could be issues
thanks in advance
I'd run with something like https://github.com/swanandp/acts_as_list to manage the ranking stuff, but as far as the front-end goes, your best bet is to either use hidden form fields for the ordering (and update their values via JS), or each time a number is moved (via some sort of javascript goodness), submit that to a controller via xhr and re-render the list of numbers as a partial.
I have created a master-detail relation using ClientDataSets (Service & Addons). The Services are displayed in a DBLookupComboBox (cboServices) and once a service is selected the Addons are displayed in DBGrid (grdMain).
The Addons has a checkbox to indicate the Addon is selected, a name field, a quantities field that the user can change, a unit price field and a total price field.
I have created a OnQuantityChange method to update the total price using the unit price and quantity but how do I get the actual data from the row to do the updating? How do I reference the various fields in order to do something like the following:
grdMain.GetActiveRow.Column['TotalPrice'] :=
grdMain.GetActiveRow.Column['UnitPrice'] * grdMain.GetActiveRow.Column['Quantity'];
You can do that easily using calculated fields. I suggest you look at the following article for a detailed example with client dataset. Just search the article for calculated fields, you will find what you need.
TClientDataset example
HI, I have a products model and a dealers model . I am creating an app in which a dealer selects a product and then he's routed to purchase order form . I want to create a dynamic multiplication of Products*Price = Total price i.e. when the form opens , he adds the quantity of the product and the default price is multiplied with no. of products to give total price.
What shall I do ?
Please guide. Thanks
There are a lot of good guides out there for Rails 3 and Unobtrusive Javascript (UJS). Here's one that starts you from the ground up:
http://net.tutsplus.com/tutorials/javascript-ajax/using-unobtrusive-javascript-and-ajax-with-rails-3/
I have what is essentially a form, only the fields depend on the previous selections, and the type of form object aren't always typical form objects.
For example, the first choices are simply in the form of links. It could be a list of teachers, where when you select a teachers name, the classes that teacher teaches is then displayed in a drop-down list. Then once a class is selected, the available dates and time are displayed in a calendar. Once a date/time is selected I need to know all of the selections that were made -- the teacher, the class, and the date and time.
How would you recommend keeping track of these selections? I'm using ajax, and although the form fields are hidden at times, i can still access their values. The one exception is the teacher selection. Since it's just a link, I pass the selected value to a method, but then I've lost the selection. I could store the selection in a hidden field, but is that really the best solution?
Thanks!
I would use a session store on a controller action. Then once it's complete extract all the gathered information.