我正在组建一个PO请求管理器。
我创建了一个模型: PORequests
然后与另一个模型建立了一对多的关系:项目
左边的表是数据源: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中的所有项目添加所有小计?
感谢您的帮助!
更新
所以我离我更近一点了。我能够获得标签来显示小计的总数。从公司商店模板中窃取以下客户端脚本:
/**
* 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字段中。
答案 0 :(得分:0)
好的,这就是我做的:
<强> 1。我创建了两个Google Drive Table模型:PORequests和Items。
PORequests有以下字段:PORequest Number,Vendor和Total *。
*注意:“总计”字段必须是字符串而不是数字。
项目包含以下字段:ItemName,Quantity,Cost和Subtotal。
<强> 2。我与两个数据源建立了一对多的关系(PORequests作为所有者)。
第3。我创建了一个包含两个表的页面。
表1的数据源= PORequests 表2的数据源= PORequests:项目(关系)*
这样,如果我点击表1中的PORequest#1,我只会看到与该POR相关的项目。
*注意:我将小计字段设置为&#34;不可编辑/标签&#34;,因此没有人意外地手动更改它。
<强> 4。然后我创建了一个客户端脚本:
/**
* 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。然后我将数量和成本字段的onValueEdit设置为:
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字段中。
希望所有这些都可以帮助将来的某个人。