HELP!

Accessing other Viabl.ai Data Structures via JavaScript

Viabl.ai Array Objects

Use the following notation to access a viabl.ai array:

#<arrayname>.elementVal(<index1>, <index2>, <value>) method to access Viabl.ai Arrays:

Where index1 and index2 refer the first and second dimension of the array and value is for setting the value of the array element. Omit index2 or set it to 0 for a one dimensional array

Examples:

Setting the value of other Object types

#NumArray.elementVal(3, 20);               // setting the third element of a SINGLE dimensional array to 20
#StrArray.elementVal(3, 2, "Fred");        // setting element 3,2 of a TWO dimension array to "Fred"

Getting the value of other object types

var n = #NumArray.elementVal(3);      // getting the third element of a SINGLE dimensional array
var t = #StrArray.elementVal(3, 2);   // getting element 3,2 of a TWO dimension array

An alternative method of accessing arrays

var loop;
for (loop = 1; loop <= 10; loop = loop + 1) {
  #strArr.val()[loop] = "Empty";
} 

Viabl.ai Dialog Objects

You can access Dialog Object as well as the Dialog Control properties via the prop Object property

#DialogName.prop(ControlName,ControlPropertyName,ControlPropertyValue); 

For example to change the caption on a Label control named "grade_title":

#MyDialog.prop("grade_title", "caption", "New Grade Caption"); 

To access properties of the actual Dialog Object, pass a blank string as the first (Control Property Name) parameter.

for example to change the Dialog's title:

#MyDialog.prop("", "caption", "New Dialog Title"); 
Remember when setting property values that the type of the property should be of the correct type e.g. setting a caption would require a string, setting the read-only property (of a numeric input for example) would expect a Boolean

The (executed) Hierarchy Object

Regardless of the the name of your Viabl.ai design-time Hierarchy Object, use the .toJSON method to access the nodes of the last executed Hierarchy Object

let myexecutedHierarchy = hierarchy.toJSON(); // retrieve the last executed hierarchy

Example:

A common example of using Hierarchies is for generatring a "Bill of Materials". This is where a complex product can configured and costed using an additional property of "cost" on each of the objects. Please see here on how to add this to an object.

// Calculate the total cost (rollup)

let builtHier = hierarchy.toJSON(); // retrieve the completed hierarchy

xpertrule.messageJSON(builtHier); // debug

function scan(nodeList) {
    let levelCost = 0; // total cost of all items in this node list (including any sub-items)
    for (let c = 0; c < nodeList.length; c++) {
        let item = nodeList[c];
        let itemCost = 0;
        // selected (list or boolean) item cost. Requires custom INSTANCE PROPERTY of "cost" which is set to "include in getValues"
        if ($.isPlainObject(item.instProps) && typeof item.instProps.cost == "number") {
            itemCost += item.instProps.cost;
        }
        // fixed cost of item (all types). Requires custom OBJECT PROPERTY of "cost" which is set to "include in getValues"
        if ($.isPlainObject(item.props) && typeof item.props.cost == "number") {
            let fixCost = item.props.cost;
            if (typeof item._value == "number") {
                // special case for numeric questions, multiply the fixed cost by the actual value
                fixCost *= item._value;
            }
            itemCost += fixCost;
        }
        // *** recurse for any children (adding up the sub cost(s))
        if ($.isArray(item.children)) {
            let subCost = scan(item.children);
            itemCost += subCost;
        }
        // store the item's cost (including any sub items) in the node
        item._rollup = itemCost;
        // increment level cost
        levelCost += itemCost;
    }
    return levelCost;
}
let totalCost = scan(builtHier);

Related Topics

On This Page