首页 文章

尝试为关系数据源提供总列数

提问于
浏览
2

我正在组建一个PO请求管理器 .

我创建了一个模型: PORequests

然后与另一个模型创建了一对多的关系: Items

左边的表是数据源:PORequests . 右边的表是PORequests:Items(relation)

因此,当您单击左侧的PORequest#1时,您只会看到与该特定PO请求关联的项目 .

然后,当用户更改项目的数量或成本时,onValueEdit将运行此代码,为小计创建条目(Items模型中的字段) .

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

所有这些都很有效,但是现在我想为所有项目添加子组件并让它填充PORequest模型中的Total字段 .

如何告诉App Maker为PORequest#1中的所有项目添加所有小计?

感谢您的帮助!


更新:

所以我离我更近一点了 . 我能够得到一个 label 来显示小计的总数 . 从公司商店模板中窃取以下客户端脚本:

/**
 * 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});
}

然后我将 getSubtotalTotal(@datasource.items) 作为标签的文本 .

所以现在我只需要弄清楚如何告诉App Maker从脚本中获取结果并将其添加到PORequests数据源的Total字段中 .

1 回答

  • 0

    好的,所以这就是我做的:

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

    PORequests有以下字段:PORequest Number,Vendor和Total * .

    *注意:Total字段必须是String而不是数字 .

    项目包含以下字段:ItemName,Quantity,Cost和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.

    表1的datasource = PORequests表2的datasource = PORequests:Items(relation)*

    这样,如果我单击表1中的PORequest#1,我只会看到与该POR关联的项目 .

    *注意:我将小计字段设置为“不可编辑/标签”,因此没有人意外地手动更改它 .

    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;
    

    这将首先告诉App Maker:将成本乘以数量并将该值放在“小计”字段中 .

    然后它告诉App Maker:使用getSubtotalTotal脚本添加所有Subtotal值并将THAT值放在PORequest数据源的Total字段中 .

    希望所有这些都可以帮助将来的某个人 .

相关问题