Trying to total columns for relation datasource

删除回忆录丶 提交于 2019-12-05 20:42:12

Okay, so here is what I did:

1. I created two Google Drive Table models: PORequests and Items.

PORequests has fields like: PORequest Number, Vendor and the Total*.

*Note: The Total field had to be a String and not a number.

Items has fields like: ItemName, Quantity, Cost and Subtotal.

2. I created a ONE to MANY relationship with the two datasources (PORequests as the owner).

3. I created a page with two Tables.

Table 1's datasource = PORequests Table 2's datasource = PORequests: Items (relation)*

This way if I click PORequest #1 in Table 1 I only see Items associated with that POR.

*Note: I set the Subtotal field to "non-editable/label", so no one accidentally changes it manually.

4. Then I created a Client Script:

/**
 * 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);
  var total = cost + app.datasources.PORequests_HideArchived.item.Tax + app.datasources.PORequests_HideArchived.item.Shipping;
  return '$' + total.toLocaleString(CURRENT_LOCALE, {minimumFractionDigits: 2});
}

5. Then I set the onValueEdit for the Quantity and Cost fields to:

widget.datasource.item.Subtotal = widget.datasource.item.Quantity * widget.datasource.item.Cost;

var subtotalTotal = getSubtotalTotal(app.datasources.PORequests.relations.Items.items);
app.datasources.PORequests.item.Total = subtotalTotal;

This tells App Maker to first: multiply the cost by the quantity and put that value in the Subtotal field.

Then it tells App Maker to: use the getSubtotalTotal script to add all the Subtotal values and put THAT value in the Total field of the PORequest datasource.

Hope all that helps someone in the future.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!