12 Replies Latest reply: Aug 12, 2014 10:28 AM by Shane Porter RSS

    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.

        • Re: How to programatically make selections
          m w

          Use the onopen trigger under document properties.

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

          • 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

              • 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

                  • 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[i] >= ui.values[0] && temp[i] <= 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[i] in countries

                              selection.push(i)

                     

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

                      • Re: How to programatically make selections

                        The first country in my list is Afghanistan, and it's ID is 71.  If I pass this value into the .select method, it gets selected.

                         

                        When I say 'ID', I mean a qElemNumber value of 71.  I can't access this from the 'app.getList' call for current selections. I can see it in a visualisation I've created, but for this situation, I need to get it from the mashup API.

                          • Re: How to programatically make selections

                            Here's a fuller code sample where I tackled a similar problem:

                             

                                        $.each(yearList, function (key, value)

                                        {

                                            dummy = parseInt(value[0].qText);

                                            yearMin = yearMin < dummy ? yearMin : dummy;

                                            yearMax = yearMax > dummy ? yearMax : dummy;

                             

                                            temp[value[0].qElemNumber] = parseInt(value[0].qText);

                                        });

                             

                                        $("#country-comparison-chart-slider").slider({

                                            range: true,

                                            min: yearMin,

                                            max: yearMax,

                                            step: 1,

                                            values: [yearMin, yearMax],

                                            slide: function (event, ui)

                                            {

                                                $("#country-comparison-years").html(ui.values[0] + " to " + ui.values[1]);

                                            },

                                            stop: function (event, ui)

                                            {

                                                $("#country-comparison-years").html(ui.values[0] + " to " + ui.values[1]);

                             

                                                var selection = [];

                             

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

                                                {

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

                                                    {

                                                        selection.push(i);

                                                    }

                                                }

                             

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

                             

                                            }

                                        });

                    • 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