To be clear I didn't come up with this (see my previous post)...
Based on the provided code, it seems that the quantity variable is being
assigned the same value every time, which is the value of
details.quantities[index] for the first item in the list.
This is because the details.quantities list is being passed to the view as
a list of strings, and the parseInt() function is being used to convert the
string value to an integer.
To rectify this issue, you should ensure that the details.quantities list
is being passed to the view as a list of integers instead of a list of
strings. You can do this by modifying the quantities field in your
db.define_table() statement to use the list:integer type instead of
list:string.
Here's an updated version of the model definition:
db.define_table('client_order',
Field('quoted_item', 'list:string', required=True),
Field('quantities', 'list:integer', required=True))
After making this change, the details.quantities list will be passed to the
view as a list of integers,
and the parseInt() function will no longer be needed in the
calculateSubtotal() function.
You can simply access the quantity value using details.quantities[index].
Here's the updated calculateSubtotal() function:
function calculateSubtotal(input, index) { var price = parseFloat(input.value);
var quantity = details.quantities[index];
var subtotal = parseFloat(price * quantity);
document.getElementById("subtotal" + index).innerHTML = subtotal.toFixed(2);
updateGrandTotal(); }
With this change, the quantity variable should now be assigned the correct
value for each item in the list, allowing the subtotal to be calculated
correctly.
On Sunday, 26 March 2023 at 15:04:29 UTC+1 mostwanted wrote:
> I want to be able to calculate subtotal by multiplying quantity and price
> but I cant because
> *var quantity = parseInt("{{=details.quantities[index]}}");* in the
> code below carries the value of 1 and it does not change. Please assist me
> identify what could be wrong here and how can i rectify it?
>
> *Model:*
> db.define_table('client_order'),
> Field('quoted_item','list:string', required=True),
> Field('quantities','list:string', required=True))
>
> *View:*
> {{for index, (item, qty) in enumerate(zip(details.quoted_item,
> details.quantities)):}}
>
> {{=item}}
>
> {{=qty}}
>
>
> {{pass}}
>
>
> function calculateSubtotal(input, index) {
> var price = parseFloat(input.value);
> *var quantity = parseInt("{{=details.quantities[index]}}");*
> var subtotal = parseFloat(price * quantity);
> document.getElementById("subtotal" + index).innerHTML =
> subtotal.toFixed(2);
> updateGrandTotal();
> console.log(parseFloat("{{=details.quantities[index]}}"));
> }
>
>
> Regards
>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to web2py+unsubscr...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/web2py/d0d7e924-c12c-4403-aebb-ff63a7762a6fn%40googlegroups.com.