Facility Control

 View Only
  • 1.  Pushing new structs into a struct array

    Posted 09-19-2016 02:25

    I've read the Dashboard CustomPanel Development Guide and been through some of the Dashboard U tutorial videos. I've built most of what I need in Dashboard, but am stuck on this.

    What I can't figure out is how to add new STRUCT to the STRUCT_ARRAY programmatically. I'm reading a TCP client using a listener and processing that input, and am executing plenty of other ogscript based on input from that listener.

    So I have STRUCT and STRUCT_ARRAY params:

    <param constrainttype="STRUCT" name="Race Definition" oid="raceDefinition" structtype="raceInfo" type="STRUCT" widget="36">
    <value>
    <subparam name="Race Name" suboid="name" type="STRING" value="Test"/>
    <subparam name="Id" suboid="identifier" type="STRING" value="Test"/>
    </value>
    </param>
    <param constrainttype="STRUCT" name="Race List" oid="raceList" structtype="raceInfo" templateoid="raceDefinition" type="STRUCT_ARRAY"/>

    And in ogscript I know I can iterate through the structs in the struct_array - something like:


    var races = params.getAllValues("raceList");
    for(var index = 0; index < races.length; index++) {
    ogscript.debug(races[index].name);
    }


    How do I create a new STRUCT, and push it into STRUCT_ARRAY?




  • 2.  RE: Pushing new structs into a struct array

    Posted 09-19-2016 17:55

    The trick is to create a new JSON object with the structure value in it and set it to the end of the list.

    The other problem you're having is that your structure is read-only so it cannot be added to. You need to add access="1" to your definition.

    var structCount = params.getElementCount('raceList');
    
    if (structCount == 1 && params.getValue('raceList.0.identifier', 0) == "") // If this is the first element and it has not been set yet
    {
       structCount = 0;
    }
    
    var newValue = {
       "name": "Name " + structCount,
       "identifier": "ID " + structCount
       };
    
    params.setValue('raceList', structCount, newValue);

    Here is a more complete custom panel example that shows adding elements to a structure and then using setAllValues to reset it:

    <abs contexttype="opengear" style="">
       <meta>
          <params>
             <param access="1" constrainttype="STRUCT" name="Race Definition" oid="raceDefinition" structtype="raceInfo" type="STRUCT" widget="36">
                <value>
                   <subparam access="1" name="Race Name" suboid="name" type="STRING" value="Test"/>
                   <subparam access="1" name="Id" suboid="identifier" type="STRING" value="Test"/>
                </value>
             </param>
             <param access="1" constrainttype="STRUCT" name="Race Definition" oid="raceList" structtype="raceInfo" templateoid="raceDefinition" type="STRUCT_ARRAY" widget="table">
                <value>
                   <subparam suboid="name" value=""/>
                   <subparam suboid="identifier" value=""/>
                </value>
             </param>
          </params>
       </meta>
       <param element="0" expand="true" height="308" left="17" oid="raceList" showlabel="false" top="17" width="448"/>
       <button buttontype="push" height="53" left="18" name="Add" top="337" width="297">
          <task tasktype="ogscript">var structCount = params.getElementCount('raceList');
    
    if (structCount == 1 &amp;&amp; params.getValue('raceList.0.identifier', 0) == "") // If this is the first element and it has not been set yet
    {
       structCount = 0;
    }
    
    var newValue = {
       "name": "Name " + structCount,
       "identifier": "ID " + structCount
       };
    
    params.setValue('raceList', structCount, newValue);</task>
       </button>
       <button buttontype="push" height="54" left="318" name="Clear" top="337" width="140">
          <task tasktype="ogscript">var empty = [{"name":"", "identifier":""}];
    params.setAllValues('raceList', empty);</task>
       </button>
    </abs>

    #DashBoard