Qlik Community

New to Qlik Sense

Discussion board where members can get started with Qlik Sense.

Not applicable

How to programatically make selections

Hi,

In a mashup scenario, I'm aware that you can use code such as this to get the current selections:

app.getList("CurrentSelections", function(reply){


});


What I want to do is set (or make) some selections programatically.


Let's say I have a simple app with a dimension of 'Country' and measure 'Population'.  I wish to automatically set the selections on a bar chart to 'China' and 'India' when the app opens.


Can this be done?  I've spent a long time examining the API(s), but haven't found anything.


Many thanks for any guidance on this!

Shane.

Tags (2)
12 Replies
mwoolf
Honored Contributor II

Re: How to programatically make selections

Use the onopen trigger under document properties.

Set the Select in Field action for Country = China and then the ToggleSelect action to India

Not applicable

Re: How to programatically make selections

Hi Michael,

thanks for the reply.  I'm using a Qlik Sense mashup, and I can't find 'onopen' anywhere at http://betahelp.qliktech.com/daily/en-us/developer/index.html#search-onopen.

What I want to do is open one mashup from another, 'transferring' any selections.  So it must be dynamic.

Thanks,

Shane.

Employee
Employee

Re: How to programatically make selections

Hi Shane - let me see if I can get one of our API Guru's to help.

Mike

Regards,
Mike Tarallo
Qlik
Not applicable

Re: How to programatically make selections

Thanks

Not applicable

Re: How to programatically make selections

I'll answer this in two parts so I don't produce a wall of text. I'm assuming you are using the mashups API. First, I'll talk about selections.

There are several functions in the API you can use for selections - look under qlik.app.field:

* select

* selectAll

* selectAlternative

* seelctExcluded

* selectMatch

* selectExcluded

* selectmatch

* selectExcluded

* selectPossible.

Here's some code from an application I've built from the API.

               if (menuCountryName.length < 100) {

                    if (countryName != menuCountryName) {

                        app.field("English short name").selectMatch(menuCountryName, false);

                    }

                }

I have a drop down list of countries (in jQueryUI) and the user can select a country from the list. If they choose a country other than the current selection, then I call the Qlik Sense API to select the new country. My drop down menu was populated by data pulled from Qlik Sense so I know the country names are consistent between my code and Qlik Sense.

A little later on in my code I want to select some years from a range of years. I load my selected years into an array (with each array entry being a year). Here's how I select my years:

app.field("Year").select(selection, false,false);

Mike

Not applicable

Re: How to programatically make selections

I'll deal with the second part now, running code during an initialization.

If I've understood your email correctly, you want to run some code once and once only when your mashup application starts. If I've misunderstood, please let me know.

I don't think you need an onopen function because of the way JavaScript works with the api. Let's take a very simple example. Let's say I want an alert to appear once only when I start my web application. Here's a code snippet to show you how I did that:

require(["js/qlikview"], function (qlikview) {

    var qvobjects = {};

    qlikview.setOnError(function (error) {

        alert(error.message);

    });

    require(["jquery", "jqueryui"], function ($) {

        // callbacks

        // open app and get objects

        var app = qlikview.openApp("World business atlas.qvf", config);

        $(".qvobject").each(function () {

            var qvid = $(this).data("qvid");

            app.getObject(this, qvid).then(function (object) {

                qvobjects[qvid] = object;

            });

        });

        alert("Calling this once!");

        // Create Lists

        // ------------

        // Fill the country name list. We don't know a priori how long the list is, so we'll send a query and get

        // the length from that query

        app.createList(

        {

            "qDef": { "qFieldDefs": ["English short name"] },

            "qInitialDataFetch": [{qTop: 0, qLeft: 0, qHeight: 1, qWidth: 1}]

        },

...

The callbacks are called each time something changes in Qlik Sense, so for example my createList is called each time my list changes in Qlik Sense. BUT code that is not callback code is called once and once only. So my alert function is called only once.

I could replace my alert function with some code like this:

app.field("My country name").selectMatch("India", false);

app.field("My country name").selectMatch("China", false);

this would preselect China and India when my charts are shown.

You might need to be careful where in your code these lines appear, but hopefully I've explained the general idea.

In short, because of the way JavaScript works, you don't need an onopen function or anything like it.

If I haven't answered your question, please let me know.

Mike

Not applicable

Re: How to programatically make selections

I haven't tried this out yet, but thank you so much for taking the time to answer my question in a detailed and helpful way.

I'll post a reply when I've tested it out!

Not applicable

Re: How to programatically make selections

The 'select' method takes an array of numbers, whereas my 'Country Name' and 'Population' example gives me a list of countries (strings) for its 'current selections.

How do I get the numbers?  If I arbitrarily pass in [0,1,2], Burundi, Comoros and Djibouti are selected - which doesn't seem to make any sense

Not applicable

Re: How to programatically make selections

I had a different but related problem. In my case, I wanted to select years rather than countries, but I think the basic idea is the same. ui.values[0] is my start year and ui.values[1] is my stop year. Here's my code.

                    var selection = [];

                    for (i = 0; i < temp.length; ++i)

                    {

                        if (temp >= ui.values[0] && temp <= ui.values[1])

                        {

                            selection.push(i);

                        }

                    }

                    app.field("Year").select(selection, false,false);

You could do the same thing, but with your countries. So it might go something like:

countries[0]="Ireland"; countries[1]="Germany";

for ()

      if temp in countries

          selection.push(i)

If this doesn't work, please let me know.

Community Browser