Topic
9 replies Latest Post - ‏2014-08-15T21:30:23Z by farns
farns
farns
40 Posts
ACCEPTED ANSWER

Pinned topic Access WEF variable from client javascript

‏2014-08-14T16:21:26Z |

I need to display/hide a field on a data entry form based upon 1) value entered in another field on the form 2) value in WEF variable. 

I have created the following builders:

  • Visibility Setter, Client-Side   (manual control for dependent field)
  • Html Event Action - created for onChange event for field in form that determines if should hide/show dependent field  - calls javascript
  • JavaScript, Client-Side - code to determine if hide/show dependent field.  Code must evaluate both the value entered in other form field AND value in stored in WEF variable.

How can I access the variable from javascript?  I have not been able to find any indication of how this might be done.

 

 

  • mburati
    mburati
    2538 Posts
    ACCEPTED ANSWER

    Re: Access WEF variable from client javascript

    ‏2014-08-14T16:30:15Z  in response to farns

    Nancy,

    If you search in the above search box for "variable from javascript" there are a number of threads on this topic.

    One of the options I see suggested there is to use the ${Variables/variableName} syntax within your Client Javascript builder js code itself,  if the variable value is known at the time the Javascript will be rendered into the HTML page.

     

    I hope that info helps,
    ..Mike Burati 
    The postings on this site are my own and do not necessarily represent the positions, strategies, or opinions of IBM.
    • farns
      farns
      40 Posts
      ACCEPTED ANSWER

      Re: Access WEF variable from client javascript

      ‏2014-08-15T17:25:59Z  in response to mburati

      I was not able to retrieve the WEF variable using the ${Variables/variableName} command when the command was placed in the HTML Event Action builder.  However, when the command was moved to the client-side javascript builder, I was able to successfully retrieve the value of a WEF string variable successfully.  It was necessary to put the javascript on the page.

      Thank you so much for your help!

      • farns
        farns
        40 Posts
        ACCEPTED ANSWER

        Re: Access WEF variable from client javascript

        ‏2014-08-15T17:45:38Z  in response to farns

        I was able to return a variable that was declared as a string.  However, I do not seem to be able to retrieve a variable that is declared as an xml variable.  I changed a testing variable from string to xml.  I no longer get any data.

        • mburati
          mburati
          2538 Posts
          ACCEPTED ANSWER

          Re: Access WEF variable from client javascript

          ‏2014-08-15T17:59:22Z  in response to farns

          Are you just trying to get a string value out of an XML element in an XML var or are you trying to retrieve the entire XML structure?   If the former then I would think that should work.   The latter may be resolving the call into an XML getText call and there may be no text node on the XML element you're specifying.   If you can supply more info about the variable and which part(s) you're trying to use in JS and how, that may help (and/or a simple sample snippet with no proprietary/conffidential code that shows what you're trying to do).

           

          I hope that info helps,
          ..Mike Burati 
          The postings on this site are my own and do not necessarily represent the positions, strategies, or opinions of IBM.
          • farns
            farns
            40 Posts
            ACCEPTED ANSWER

            Re: Access WEF variable from client javascript

            ‏2014-08-15T18:26:17Z  in response to mburati

            I have an xml variable. For testing purposes I have hard coded the xml.  In the java script I need to show/hide form fields depending on whether there are any entries in the xml variable.  If there are entries, then I will just need to read the value of each entry.

            What I currently have:

            • varTest -  variable (xml) <Records><Record>Thing1</Record><Record>Thing2</Record></Records>
            • javascript, client-side  var xml =" ${Variables/varTest}"; alert(xml); 

            When I change the variable varTest to a String and put in some default string, the string value is printed.  However, when I change varTest to an xml variable and put in some default xml, I seem to get nothing at all.

            All I need to be able to do is retrieve Thing1 and Thing2.

            • mburati
              mburati
              2538 Posts
              ACCEPTED ANSWER

              Re: Access WEF variable from client javascript

              ‏2014-08-15T18:34:58Z  in response to farns

              I don't have a similar model set up to see what it's doing but my guess is that it's trying to get the text value of the variable element you're specifying and that top level element has no text node children, just xml element children, so it's coming back null.

               

              It's easier to work with JSON and JS objects in JS anyway, so if I were doing this I would probably try using the XML to JSON converter builder against that variable and then use an indirect reference to the method that builder generates in my Client Javascript builder to populate a JS variable with the results of that XML to JSON conversion and then use the JS variable to get at the individual row values.

               

              I hope that info helps,
              ..Mike Burati 
              The postings on this site are my own and do not necessarily represent the positions, strategies, or opinions of IBM.
              • farns
                farns
                40 Posts
                ACCEPTED ANSWER

                Re: Access WEF variable from client javascript

                ‏2014-08-15T19:12:38Z  in response to mburati

                I have created the XML to JSON Converter builder and the method has been generated.  How do I call that method in client-side javascript?  I am not certain what you mean by indirect reference. 

                • mburati
                  mburati
                  2538 Posts
                  ACCEPTED ANSWER

                  Re: Access WEF variable from client javascript

                  ‏2014-08-15T20:34:52Z  in response to farns

                  I meant like the indirect ref you would see if you tried to return a value from an action list and then picked a method call to the generated method from the picker (or the right hand side of an assignment).

                  I put your above XML into an XML Variable.

                  I added an XML to JSON converter builder against that variable and named it x2j

                  I then added a ClientJavascript builder with the following sample JS and can see and walk through your XML data via JS:

                  var data = ${MethodCall/x2jConvertToJSON};
                  alert("Got converted object: " + JSON.stringify(data) );
                  alert("Got records object: " + data.Records);
                  alert("Record 1 (data.Records.Record[0]) has value: " + data.Records.Record[0]);

                  I should also point out that you should be sure that the data makes sense to be used in the context you are going to use it via Javascript and that if it came from user inputs, that you validated that it only contained value(s) that the application expects to be there (eg, if it's a boolean true/false then don't allow anything but that) to avoid malicious data being submitted by one user and ending up back in the page in a Javascript object used by another user.

                   

                  I hope that info helps,
                  ..Mike Burati 
                  The postings on this site are my own and do not necessarily represent the positions, strategies, or opinions of IBM.

                   

                  Updated on 2014-08-16T00:56:29Z at 2014-08-16T00:56:29Z by mburati
                  • farns
                    farns
                    40 Posts
                    ACCEPTED ANSWER

                    Re: Access WEF variable from client javascript

                    ‏2014-08-15T21:30:23Z  in response to mburati

                    It works! 

                    It is doing exactly what I want it to do! 

                    I would never has realized on my own that I should use the XML to JSON builder.  I would have continued to try to retrieve the xml. This saved so much time.  I cannot even begin to tell you how much help your comments provided. 

                    Thank you!  Thank you!  Thank you!!!!   Great way to end for the weekend.