I am putting together a PO Request manager.
I have created one model: PORequests
Then created a ONE to MANY relationship with another model: Items
Table on the left is the datasource: PORequests. The table on the right is PORequests:Items (relation)
So when you click on PORequest #1 on the left, you see only the items associated with that specific PO request.
Then when a user changes the quantity or cost of the item, the onValueEdit runs this code, creating an entry for the subtotal (a field in the Items model).
widget.datasource.item.Subtotal = widget.datasource.item.Quantity * widget.datasource.item.Cost;
All that works great, however now I want to add the subtoals for all the items and have it fill the Total field in the PORequest model.
How can I tell App Maker to add up all the subtotals for all items in PORequest #1?
Thank you for the help!
Update:
So I'm a little bit closer. I was able to get a label to display the total of subtotals. Stole the following Client Script from the Corporate Store template:
/**
* Locale constant that is used for currency formatting.
*/
var CURRENT_LOCALE = 'en-US';
/**
* Calculates and formats total cost of all POR items.
* @param {Array<CartItem>} PORItems - list of user's POR items.
* @return {string} formatted total cost of all POR items.
*/
function getSubtotalTotal(PORItems) {
var cost = PORItems.reduce(function(result, item) {
return result + item.Subtotal;
}, 0);
return '$' + cost.toLocaleString(CURRENT_LOCALE, {minimumFractionDigits: 2});
}
Then I put getSubtotalTotal(@datasource.items) as the text for the label.
So now I just need to figure out how to tell App Maker to take that result from the script and add it to the Total field in the PORequests datasource.