Adding selections

Now that we can successfully render the results of a defined dimension, the next step is to implement the feedback event: the ability for the user to make a selection.

This is achieved relatively quickly using AngularJS; all you need to do is the following:

  1. Capture the selected value in the drop-down list
  2. Set up a watcher, which is monitoring the value for changes
  3. If the value changes, apply the new selection

Beginning with step 1, the selected value can be captured using AngularJS's ng-model, placed in the select tag of the HTML:

<select ng-model="selectedValue" class="lui-select lui-select--gradient">
<option ng-repeat='value in dimension_values track by $index' value="{{value.element_id}}" class="state_{{value.selection_state}}">{{value.title}}</option>
</select>

If the options change, their underlying value (in this case, the element_id) will be assigned to scope.selectedValue, which can be read in the controller.

Step 2, in the controller, sets up the AngularJS watcher to watch the selectedValue for changes:

scope.$watch('selectedValue', function() {
//I am watching selectedValue for changes
});

Now that it's set up, apply selections to the data model if the selectedValue has been updated:

scope.$watch('selectedValue', function() {
if(typeof scope.selectedValue !== "undefined"){
app.field(scope.layout.field)
.select([parseInt(scope.selectedValue,10)])
}
});

Using the Field API, selections are passed to the defined field using the qElemNumber of the dimension value.

typeOf scope.selectedValue !== "undefined" checks whether the selectedValue has an actual value. The watcher will get fired on the first rendering of the object, long before the dimension has been rendered. To avoid any errors in the code, we make sure the selection only gets fired if the selectedValue actually carries an element_id.
parseInt(scope.selectedValue,10) is required in this instance to pass the correct format to the Qlik Sense engine. The array element needs to be an integer; however, if it's being retrieved via the <select> element, it's a string.parseInt() , and then it transforms the string back to an integer.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.57.228