Facility Control

 View Only
  • 1.  Populate dropdown with XML values

    Posted 08-12-2017 20:13

    I've been looking into the XML example from DashBoard U now for like... a week..

    And while I can easily read and parse the values from the XML document, I cannot for the LIFE of me figure out how to create a dropdown, that is populated with the names of the team, and with the value pointing to another xml document.

    XML example

    ...
    <team name="teamname1" xml="teamname1.xml" />
    ...

    Script so far:

    var filepath = 'xml/lag.xml';
    ogscript.debug('Reading datafile: ' + filepath);
    
    var XMLDoc = ogscript.parseXML(filepath);
    if (XMLDoc == null) {
       ogscript.debug('Failed to read XML file: ' + filepath);
    } else {
       ogscript.debug('Parsing XML...');
       var found=XMLDoc.getElementsByTagName('team');
       ogscript.debug('Search found ' + found.length + ' entries');
       params.setValue(0xb, 0, found.length);
       for (var i = 0; i < found.length; i++) {
          var node = found.item(i);
          var team = node.getAttribute('navn');
          var xml = node.getAttribute('xml');
          ogscript.debug(i + ": " + team + " - " + xml);
          params.setValue(0xb, i, team);
       }
    }

    any working example would be greatly appreciated...



  • 2.  RE: Populate dropdown with XML values

    Posted 08-12-2017 20:53
    It HAS to be possible to create a READ ONLY DROPDOWN from a string parameter??? I CANNOT understand why this is disabled???
    All I get are x-number of text fields going horizontal based on the amount of teams?!?! I'm sorry, but this makes NO sense at all!

    I'm gonna go as far out to say that this is bugged in the current version of DashBoard.
    When setting up a paramter with the following settings:

    • Name: leagueTeam
    • OID: 0x11
    • Manu(s): No menu(s) selected
    • Precision: 0
    • Type: String Array (17)
    • Constraint: Unconstrained (0)
    • Widget hint: Editable dropdown
    • At creation it only has one value, through the script above (altered the OID above from 0xb to 0x11) I end up with 4 values in the parameter after parsing the XML.
    There is no way to actually set up a dropdown parameter for the string. It's missing the arrow on the right hand side. A scroll="true" does not help, and any other code to enable/disable a dropdown or anything of the sorts just makes the 4 values appear as 4 text fields horizontally where the dropdown was supposed to be.

    A hint? A clue? A sollution? A confirmation this is a bug? Anything? The phrase "Dashboard is so easy, even the marketing guy can do it!" seems to put a HELL of alot of credit on the marketing guy to be downright honest!
    #DashBoard


  • 3.  RE: Populate dropdown with XML values

    Posted 08-13-2017 11:47

    So... After another 4 hours of banging my head against this one.
    I ended up making a video of it. Just please point out if I'm stupid or just downright misunderstood something.

    But I cannot get this to work!
    https://www.youtube.com/watch?v=6cd_ZbSSssM&feature=youtu.be


    Please enlighten me! Or if this truly is not working as inteded... please do let me know.
    Current DashBoard version: 8.3.1


    #DashBoard


  • 4.  RE: Populate dropdown with XML values

    Posted 08-13-2017 14:05
    Hi there,

    Drop down parameters are created using constraints on integer values.

    Creating a dropdown from an XML file is done in the following steps:

    -Create an integer parameter that you are going to want to use as your dropdown
    -Parse the XML file (it sounds like you have that working).

    (I haven't done this in over a year, so this is from memory, I'll spend some time digging up the details on this one to double check that my memory is correct).
    -As you parse the XML values, typically I have put these into an array.
    -Update the constraint on your parameter from that array.

    And, yes, while many many things in DashBoard are very easy, I would say that this is an advanced operation and is absolutely trickier to implement.
    #DashBoard


  • 5.  RE: Populate dropdown with XML values

    Posted 08-13-2017 14:38

    Here is a small panel that reads a simple XML file and creates a drop down out of it:
    Note the use of the dynamic constraint functions.

    <abs contexttype="opengear" style="">
    <meta>
    <params>
    <param access="1" constrainttype="INT_CHOICE" name="Constraint From XML" oid="Constraint_From_XML" precision="0" type="INT32" value="0" widget="combo">
    <constraint key="0">Nothing</constraint>
    </param>
    </params>
    <ogscript handles="onload">var newConstraint = params.createIntChoiceConstraint(["Nothing"]);
    params.replaceConstraint('Constraint_From_XML', newConstraint);</ogscript>
    </meta>
    <param expand="true" height="90" left="32" oid="Constraint_From_XML" top="48" width="626"/>
    <button buttontype="push" height="95" left="673" name="Load" top="47" width="183">
    <task tasktype="ogscript">var xmlDocument = ogscript.parseXML('data.xml'); //Parse the XML
    var nodeList = xmlDocument.getElementsByTagName("team"); //Get the tags we want to include in the constraint

    var constraintData = []; //Placeholder for the new constraint data

    if (constraintData.length == 0) //Placeholder if we don't have anything (OPTIONAL)
    {
    constraintData.push({"key": 0, "value": "Nothing"});
    }

    for (var i = 0; i < nodeList.getLength(); i++) //Loop through each tag we found
    {
    var node = nodeList.item(i); //Get the node
    constraintData.push({"key": parseInt(node.getAttribute("ID")), "value": node.getAttribute("name")}); //Push the key/value pair
    }


    var constraint = params.createIntChoiceConstraint(constraintData); //Create the constraint
    params.replaceConstraint("Constraint_From_XML", constraint); //Replace the constraint</task>
    </button>
    </abs>

    The File data.xml is as follows:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <teams>
    <team ID="1" name="Red Team"/>
    <team ID="2" name="Blue Team"/>
    <team ID="3" name="Green Team"/>
    <team ID="3" name="Your Team"/>
    </teams>

    #DashBoard


  • 6.  RE: Populate dropdown with XML values

    Posted 08-13-2017 22:20
    First of all... Thank you! It works!

    I'm pretty sure I can use and tweak this further into what I need for the second dropdown and further info.
    I would still say something is really wrong with the way the string value dropdown works. But what you gave me here does the job!

    I do believe I could also remove the ID from the XML document and instead "generate" the id through the var i = 0 to simply increase the ID through every instance it finds a tag named "team".
    Not important, but as it might be others also included in updating the XML files. And the less attributes I can get away with, the better.

    The only piece of code I dont really get is the part you put before the .
    var newConstraint = params.createIntChoiceConstraint(["Nothing"]);
    params.replaceConstraint('Constraint_From_XML', newConstraint);


    From what I can gather, all that does is set a initial value ("Nothing") on load?
    Or am I missing something important here
    #DashBoard


  • 7.  RE: Populate dropdown with XML values

    Posted 08-13-2017 23:07
    Scratch that.
    Was wondering how to access the string value now instead of the int. value.
    Though appereantly, the script palette is actually quite usefull!
    #DashBoard


  • 8.  RE: Populate dropdown with XML values

    Posted 08-14-2017 14:52

    You can access the string value with params.getValueAsString instead of params.getValue.

    As for your other questions:
    RE: Why the onload
    The onload in the example is used to reset the constraint to empty each time the panel is loaded (so you can see the demo).

    RE: Why the String combo boxes are editable
    There is a mechanism to use a string constraint where the combo box is not editable but, in DashBoard world, this has traditionally been left to INT_CHOICE lists. There are actually good reasons behind this but it has more to do with DashBoard's legacy as the control system for external devices talking openGear Protocol where, for non-editable lists, it was far more efficient to send a number back to the device instead of a string.

    We only introduced the string combo box because people started requesting editable combo box fields


    An alternate approach to your initial question:
    If you want to create a non-editable string-based parameter, this example will work for you. The feature itself is relatively new in DashBoard.

    <abs contexttype="opengear" style="">
       <meta>
          <params>
             <param access="1" constraintstrict="true" constrainttype="STRING_STRING_CHOICE" maxlength="-1" name="Constraint From XML" oid="Constraint_From_XML" type="STRING" value="Nothing" widget="combo">
                <constraint key="Nothing">Nothing</constraint>
             </param>
          </params>
          <ogscript handles="onload">var newConstraint = params.createStringStringChoiceConstraint(["Nothing"]);
    newConstraint.setStrict(true);
    params.replaceConstraint('Constraint_From_XML', newConstraint);</ogscript>
       </meta>
       <param expand="true" height="90" left="32" oid="Constraint_From_XML" top="48" width="626"/>
       <button buttontype="push" height="95" left="673" name="Load" top="47" width="183">
          <task tasktype="ogscript">var xmlDocument = ogscript.parseXML('data.xml');         //Parse the XML
    var nodeList = xmlDocument.getElementsByTagName("team"); //Get the tags we want to include in the constraint
    
    var constraintData = [];   //Placeholder for the new constraint data
    
    if (constraintData.length == 0)  //Placeholder if we don't have anything (OPTIONAL)
    {
       constraintData.push("Nothing");
    }
    
    for (var i = 0; i &lt; nodeList.getLength(); i++)           //Loop through each tag we found
    {
       var node = nodeList.item(i);                          //Get the node
       constraintData.push(node.getAttribute("name"));  //Push the key/value pair
    }
    
    
    var constraint = params.createStringStringChoiceConstraint(constraintData);   //Create the constraint
    constraint.setStrict(true); //This is the part that makes it non-editable
    params.replaceConstraint("Constraint_From_XML", constraint);         //Replace the constraint</task>
       </button>
    </abs>

    Hope this helps!

    James


    #DashBoard


  • 9.  RE: Populate dropdown with XML values

    Posted 08-15-2017 11:37
    I appreciate all the help!

    I guess I have more deconstruction of the code to go through. :)
    #DashBoard