
I have the following which I am trying to adapt for my custom sheet....copied from the Wiki and partially modified.
To be honest, the syntax is beyond me, and I am unsure exactly where to substitute my attribute names.
My attributes are:
destination = "total_wgt"
section = "inventory"
fields = "inv_wgt"
multiplier = "1" (default setting)
I also have a quantity attribute called "inv_qty" which is a modifier for the individual items...
Could someone help me fill in the blanks please?
Here is my HTML
<div class="row">
<div class="field50 bold">Qty</div>
<div class="field400 bold">Item</div>
<div class="field180 center bold">Location</div>
<div class="field40 center bold">WGT</div>
<fieldset class="repeating_inventory">
<input class="field50 left" type="text" name="attr_inv_qty">
<input class="field400 left" type="text" name="attr_inv_name">
<input class="field180 left" type="text" name="attr_inv_loc">
<input class="field40 center" type="number" name="attr_inv_wgt" value="0" step="0.01">
</fieldset>
</div>
/* ===== PARAMETERS ==========
destination = the name of the attribute that stores the total quantity
section = name of repeating fieldset, without the repeating_
fields = the name of the attribute field to be summed can be a single attribute: 'weight'
or an array of attributes: ['weight','number','equipped']
multiplier (optional) = a multiplier to to entire fieldset total. For instance, if summing coins of
weight 0.02, might want to multiply the final total by 0.02. */
const repeatingSum = (total_wgt, inventory, fields, multiplier = 1) => {
if (!Array.isArray(fields)) fields = [inv_wgt];
getSectionIDs(`repeating_${inventory}`, idArray => {
const attrArray = idArray.reduce( (m,id) => [...m, ...(fields.map(field => `repeating_${inventory}_${id}_${inv_wgt}`))],[]);
getAttrs(attrArray, v => {
console.log("===== values of v: "+ JSON.stringify(v) +" =====");
// getValue: if not a number, returns 1 if it is 'on' (checkbox), otherwise returns 0..
const getValue = (section, id,field) => parseFloat(v[`repeating_${inventory}_${id}_${field}`], 10) || (v[`repeating_${inventory}_${id}_${field}`] === 'on' ? 1 : 0);
const sumTotal = idArray.reduce((total, id) => total + fields.reduce((subtotal,field) => subtotal * getValue(section, id,field),1),0);
setAttrs({[total_wgt]: sumTotal * multiplier})
});
});
};