4 Replies Latest reply: Jan 27, 2012 10:04 AM by Albin Carnstam RSS

    Extension / Javascript API / Acessing Variable

      Hello everyone,

      I'm working on an extension for QlikView 10 SR1. I'm trying to read the value of a variable with the Javascript API, and of course later I will change the value of the variable.

      In the Documention there are two function mentioned "SetVariable" and "GetVariable" for namespace "QvaPublic". Although I know it only applicable for inputboxes.

      But if I reference one inputbox and execute the function "GetVariable" I receive the error, that the object does not support this function. Does anyone has accomplished this ?

      This is the js code I'm currently using

       


      Qva.AddExtension('variable', function() {
      // refrence to inputbox
      var myInputBox = this.GetQvObject( "IB01", function(){} );
      // Show the object name
      alert( myInputBox.Name );
      // try to get the variable
      try {
      var myVar = myInputBox.GetVariable(); // <<-- here is the error
      alert(myVar.value);
      }
      catch(err) {
      alert(err.description);
      }
      }


        • Re: Extension / Javascript API / Acessing Variable

          Qva.AddExtension('variable', function() {

          // refrence to inputbox

          var myInputBox = this.GetQvObject( "IB01", function(){

               alert(this.GetVariable()["value"]);

          } );

          // Show the object name

          alert( myInputBox.Name );

          }

          • Re: Extension / Javascript API / Acessing Variable

            Hi,

             

            to print it instead of alerting it you can do this.

             

            decide where you want it and create an element and set an ID-attribute for it. Add the element to your document as well. Later on when your document is loaded and the callback-function is called you can access the element and insert the variable there. I'll add the code into your example.

             

            Qva.AddExtension('variable', function() {

             

                 p = document.createElement("p");

                 p.setAttribute("id", "ptest");

             

                 // refrence to inputbox

                 var myInputBox = this.GetQvObject( "IB01", function() {

                                var variable = this.GetVariable();

                      document.getElementById("ptest").innerHTML = variable.value;

                 });

             

                 this.Element.appendChild(p);

            }

             

             

             

            /Albin

              • Re: Extension / Javascript API / Acessing Variable
                Paulo Hersan

                Hey guys, good discussion here. I'm having some issues trying to retrieve a variable from QlikView using the JavaScript API for an Extension object.

                 

                So this code works, and I get alerted that the variable myInstallDate is equal to what I set it on the front-end.

                 

                                              var myInstallDate;

                                              myInputBox = this.GetQvObject("IB01", function(){

                                                                  myInstallDate = this.GetVariable()["value"];

                                                                  alert(myInstallDate);    <==== gets updated everytime I change the variable, which is what I want.

                                              });

                 

                However, I want to use that value on another piece of my code. As soon as I move the alert outside of that function, the variable myInstallDate becomes undefined. That means the scope of the variable is limited to that function, correct?

                 

                                              var myInstallDate;

                                              myInputBox = this.GetQvObject("IB01", function(){

                                                                  myInstallDate = this.GetVariable()["value"];

                                              });

                                             alert(myInstallDate);   <===== UNDEFINED

                 

                I've tried playing around with where I'm setting the variable (in other words, I was trying to make it a global variable to see if it would remain set) but I haven't had any luck. Any suggestions?

                 

                I just want to retrieve a Date that is set dynamically by the user on the front end, and then use that Date in my JavaScript code for logical statements. What's the best way?

                  • Re: Extension / Javascript API / Acessing Variable

                    The reason you can't access it directly afterwards is because it doesn't exist yet. The extension object uses the ajax technology and is asynchronous. All objects in the qlikview application must be loaded before the extension object can be certain it can get a hold of the variable. So when you set the GetQvObject-method the callback function within is called upon only when all objects and the extension object itself is fully loaded and later if a variable in the input box is changed. That means that all other lines of code in the extension object are executed before that function is called and the variable is set.

                     

                    To solve this problem you can for example add a function that is called by a javascript event (onclick, onchange etc.) when the user do something with an html-element. In that function you can then use the alert for example.

                     

                    Hope this helps you in the right direction.

                     

                    Cheers!