Skip to main content
Announcements
Global Transformation Awards! Applications are now open. Submit Entry
cancel
Showing results for 
Search instead for 
Did you mean: 
nvrphanikumar
Creator
Creator

Mashups-Passing Variables

Based on the examples in the thread below,I was able to pass static variables from Select or Input HTML Tags to QlikSense App.

https://community.qlik.com/thread/220759

I'm now trying to pass dynamic value instead of static value.

This dynamic value is based on geo location of the user logged in.

With some sample codes available online,I was able to obtain geo codes and save it to a variable.

Cant figure out ,how to pass these dynamic variable to Qlik.

Example code;Assigned lat variable as

var lat = position.coords.latitude;

and trying to pass this dynamic variable using this statement below,I know i'm doing something wrong with bold text below but dont know how to correct it.

  $("#Accept1-button").click(function() {

   // app.variable.setNumValue('vLat',5);

app.variable.setStringValue("vLat", $("#lat").val());

  });

Can someone please suggest.

Ref Document

Variable API ‒ Qlik Sense Developers

1 Solution

Accepted Solutions
nvrphanikumar
Creator
Creator
Author

I was able to fix the issue.

I believe the issue is,Qlik only accepts input values rather than variables

Here is an explanation of how its implemented.

1)HTML and Java Script to get Geo Location

<p id="demo"></p>

<script>

var x = document.getElementById("demo");

var vlat = position.coords.latitude;

var vlon = position.coords.longitude;

function getLocation() {

    if (navigator.geolocation) {

        navigator.geolocation.getCurrentPosition(showPosition);

    } else {

        x.innerHTML = "Geolocation is not supported by this browser.";

    }

}

function showPosition(position) {

//Passing Variables

   vlat = position.coords.latitude;

   vlon = position.coords.longitude;   

  

    x.innerHTML = "Latitude: " +  vlat + //position.coords.latitude +

    "<br>Longitude: " +  vlon //position.coords.longitude;

}

</script>

2)Create Input Box to hold the values ,so it can pass to Qlik App.

Forcing the user to hit Accept Button,so we can pass variable values to input boxes

<input type="text" type="hidden" id="myLat" ></input>

<input type="text" type="hidden" id="myLong" ></input>

<button id="Accept1-button" onclick="myFunction()">Accept GeoLoc</button>

3)

<script>

function myFunction() {

document.getElementById("myLat").defaultValue = vlat ;

document.getElementById("myLong").defaultValue = vlon ;

}

</script>

4)This step passes variables to Qlik

  $("#Accept1-button").click(function() { 

  app.variable.setStringValue("vLat",$("#myLat").val());

  });

 

  $("#Accept1-button").click(function() {

app.variable.setStringValue("vLong",  $("#myLong").val());

  });

View solution in original post

7 Replies
Anil_Babu_Samineni

Looks like working

function VariableValues() {

var singleValues = $( "#single" ).val();

var multipleValues = $( "#multiple" ).val() || [];

app.variable.setStringValue("singlevalues", "$(lat)".val());

$( "p" ).html( "<b>Single:</b> " + singleValues +

" <b>Multiple:</b> " + multipleValues.join( ", " ) );

}


Note - While extend to variable # and . are ID's and Classes only. So, You may require to call outside of Variables via Double Quotes. If not working, Please make sure to explain little about needed to pas the variables.

Best Anil, When applicable please mark the correct/appropriate replies as "solution" (you can mark up to 3 "solutions". Please LIKE threads if the provided solution is helpful
nvrphanikumar
Creator
Creator
Author

Thanks Anil, Tried similar code on mine but didnt work.

Let me explain further.

1)In Qlik App,I have a Variable called vLat

2)In the mashup html ,I have the following code to find the geocodes

<body onload="javascript:getLocation()">

<p id="demo"></p>

<script>

var x = document.getElementById("demo");

var lat = position.coords.latitude;

var lon = position.coords.longitude;

function getLocation() {

    if (navigator.geolocation) {

        navigator.geolocation.getCurrentPosition(showPosition);

    } else {

        x.innerHTML = "Geolocation is not supported by this browser.";

    }

}

function showPosition(position) {

//Passing Variables

    document.getElementById("lat").innerHTML = position.coords.latitude;

    document.getElementById("lon").innerHTML = position.coords.longitude;

    x.innerHTML = "Latitude: " + position.coords.latitude +

    "<br>Longitude: " + position.coords.longitude;

}

<button id="Accept1-button">GeoLoc</button>

3)When a user clicks the button,I defined JavaScript to pass the HTML variable lat to the Qlik App variable vLat

  $("#Accept1-button").click(function() {

app.variable.setStringValue("vLat","$(lat)".val());

  });

Prior to your suggestion,My code looked like below

  $("#Accept1-button").click(function() {

app.variable.setStringValue("vLat",$("#lat").val());

  });

I think the challenge here is,the my code works for Select Tags,Per your recommendation,i changed my code to look at $(lat) for variable but that didnt work.

Anil_Babu_Samineni

From third point you can call like below, Try this way?

variable vLat

document.getElementById("Accept1-button").click(function() {

app.variable.setStringValue("vLat","$(lat)".val());

  });

Best Anil, When applicable please mark the correct/appropriate replies as "solution" (you can mark up to 3 "solutions". Please LIKE threads if the provided solution is helpful
nvrphanikumar
Creator
Creator
Author

No Luck Anil.

nvrphanikumar
Creator
Creator
Author

I was able to fix the issue.

I believe the issue is,Qlik only accepts input values rather than variables

Here is an explanation of how its implemented.

1)HTML and Java Script to get Geo Location

<p id="demo"></p>

<script>

var x = document.getElementById("demo");

var vlat = position.coords.latitude;

var vlon = position.coords.longitude;

function getLocation() {

    if (navigator.geolocation) {

        navigator.geolocation.getCurrentPosition(showPosition);

    } else {

        x.innerHTML = "Geolocation is not supported by this browser.";

    }

}

function showPosition(position) {

//Passing Variables

   vlat = position.coords.latitude;

   vlon = position.coords.longitude;   

  

    x.innerHTML = "Latitude: " +  vlat + //position.coords.latitude +

    "<br>Longitude: " +  vlon //position.coords.longitude;

}

</script>

2)Create Input Box to hold the values ,so it can pass to Qlik App.

Forcing the user to hit Accept Button,so we can pass variable values to input boxes

<input type="text" type="hidden" id="myLat" ></input>

<input type="text" type="hidden" id="myLong" ></input>

<button id="Accept1-button" onclick="myFunction()">Accept GeoLoc</button>

3)

<script>

function myFunction() {

document.getElementById("myLat").defaultValue = vlat ;

document.getElementById("myLong").defaultValue = vlon ;

}

</script>

4)This step passes variables to Qlik

  $("#Accept1-button").click(function() { 

  app.variable.setStringValue("vLat",$("#myLat").val());

  });

 

  $("#Accept1-button").click(function() {

app.variable.setStringValue("vLong",  $("#myLong").val());

  });

rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

Given that lat is a javascript variable, I think the correct syntax would be:

app.variable.setStringValue("vLat", lat);


The jquery method $("#xx") is for accessing DOM elements, not variables.

-Rob

nvrphanikumar
Creator
Creator
Author

When i code it to use the variable directly, Qlik objects in the mashup go completely blank ,they dont show any data.

app.variable.setStringValue("vLat", lat);