Skip to main content
Announcements
Have questions about Qlik Connect? Join us live on April 10th, at 11 AM ET: SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Read QV-Variables via JavaScript

Hello everybody,


my problem is: if I define a variable in the desktop client (for instance: vTest = localTime()) i'm not able to access the interpreted content of that variable in the Ajax-client. I just can read "localTime()" as a String from the VariableMgr. The idea is to send the function-name "localTime()" to the QV-Server via Ajax (or to use the QMS-WebService API) and get the interpreted value as a response. Is that possible? Or even better: I send the name of the variable ("vTest") to the QV-Server and get the calculated content.

I can't find proper methods in the QMS-API nor "JS-API" 😞

->Workaround:  I could define input boxes dynamically via JS, access it, get the calculated content and remove the input box, but it's way too slow!

Can anybody help me out here?

Thanks in advance,

Lars

10 Replies
jonathandienst
Partner - Champion III
Partner - Champion III

Lars

If you put a leading = in the variable definition, you should get the interpreted value in both Ajax and the local client. You can do this in the variable dialog, and in script you would say:

Set vTest = =localtime();  // that's not a misprint

Then use it in expressions as $(vTest) or '$(vTest)'.

Hope that helps

Jonathan

Logic will get you from a to b. Imagination will take you everywhere. - A Einstein
Not applicable
Author

Hello Jonathan,

thanks for your reply!

Unfortunately it's not working. Let me explain you my intention in more detail:

I am able to define variables and I can use them in expressions. But I can't use them in the JS-Code of an extension object. In pseudo-code i want to do sth like this:

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

     var vTest = QlikView.giveMeTheInterpretedVariable("vTest");

     alert(vTest); // displays "08.05.2012 09:43:45"

},false);

Is there a functionality like this? Or some workaround?

Thanks + best regards,

Lars

ErikWetterberg

Hi Lars,

One way is to use the Text tag in your Definition.xml:

<Text Label="Variable:" Initial=""/>

In the property dialog, set it up like '=vTest'.

You extension will get the value in Layout.Text0.text (if it is your fist Text tag).

Alternatively you can set the variable name in the Definition.xml:

<Text Label="Variable:" Initial="" Expression="=vTest"/>

There is also a javascript call to get all variables:

Qv.GetDocument("").GetAllVariables(callback);

where callback is a javascript function to handle the result.

Erik

Not applicable
Author

Hi Erik,

you just made my day - that simple 🙂

Qv.GetDocument("")... worked.

Thank you very much!

Lars

Not applicable
Author

Hi Eric (and everyone else),

at the first moment that solution seemed to work. But now it's not working anymore 😞
Is it possible that this is a bug in QV11SR1 (can't imagine though)?

My Steps once more in detail:

I have a QVW with the fields YEAR (such as "2012") and MONTH (such as "6").

I created a variable "vKeyDate" with the definition "=MakeDate(Year,Month)"

   -> works fine within a textbox or an inputfield (output: "01.06.2012")

In my Script.js of the extension I created a JS function:

var doc = Qv.GetCurrentDocument();

    doc.GetAllVariables(function(data){

        $.each(data, function(i, object){

            console.log(object);

        });

    });

The returned (relevant) object looks like this:
{

     isconfig: "false",

     isnum: "false",

     isreserved: "false",

     name: "vKeyDate",

     text: "",

     value: "=MakeDate(Year,Month)" // <-- I want this to be interpreted

}

Can you confirm that this steps should work in SR1?

Best Regards,
Lars

Alexander_Thor
Employee
Employee

I'm getting the same results.

Not applicable
Author

Hi Alexander,

do you think that there is any solution for this? It's really a big problem for us right now 😞
I had a small e-mail conversation with Ingemar Carlo last week and he gave me the tip to try the SetOnUpdateComplete Function of the qv-document and call the GetAllVariables function inside. His assumption was that the variables are not yet calculated when they are fetched.

But that results in an infinite loop, because the GetVariable function triggers the OnUpdate event of the document.... I fixed it like this:

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

      var doc = Qv.GetCurrentDocument();

      var varsRetrieved = false;

      doc.SetOnUpdateComplete(function(){

           if(!varsRetrieved){

                Qv.GetDocument("").GetAllVariables(function(variables){

                     for(var i = 0; i < variables.length; i++){

                           alert(variables.name + " = " + variables.value);

                     }

                });

                varsRetrieved = true;

           }

      });

  }, false);

But in the end it's still not working, regardless of the moment the variables are fetched.

Nachricht geändert durch lars.bayer

Not applicable
Author

Do you use the LET command (not the SET) in the script? It works for me if I creat an variable

Let vTest = Today();

I get 2012-07-06 in my java script

/Gustav

Not applicable
Author

one more thing you prorbably have to put it last in your script, after det data is loaded.