/
currentSelections.js
116 lines (103 loc) · 4.2 KB
/
currentSelections.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
////////////////////////////////////////////////////
//Version: 1.2
//Author: Richard Byard
//Usage: Current Selections
//Date: 13 Sept 2017
////////////////////////////////////////////////////
// Array of field to label translations
// If a field matches the field parameter than it will be replaced with the label parameter.
var fieldsTranslations = [{
Label: "",
Field: ""
}];
var mySelectedFields; // Global selected fields
var app; // Global app
define([
// Load the properties.js file using requireJS
// Note: If you load .js files, omit the file extension, otherwhise
// requireJS will not load it correctly
'jquery',
'qlik',
// './properties/properties',
// './properties/initialProperties',
/***********************
This is used to reference specific CSS V2.0 upwards.
QlikSense set the CSS class qv-object-[extension name] on your visualizations
and your CSS rules should be prefixed with that.
************************/
'css!./currentSelections.css'
],
function($, qlik, styleSheet) {
'use strict';
//Inject Stylesheet into header of current document
$('<style>').html(styleSheet).appendTo('head');
return {
//Define the properties tab - these are defined in the properties.js file
// definition: props,
//Define the data properties - how many rows and columns to load.
// initialProperties: initProps,
//Not sure if there are any other options available here.
snapshot: {
cantTakeSnapshot: true
},
//Allow export for printing
support : {
export: true
},
//paint function creates the visualisation. - this one makes a very basic table with no selections etc.
paint: function($element, layout) {
app = qlik.currApp(this);
var selectionState = app.selectionState;
var selections = app.getList("CurrentSelections", function(reply) {
mySelectedFields = reply.qSelectionObject.qSelections;
var mySelectedFieldsLength = mySelectedFields.length,
html = "";
html += '<table id="mySelections" class="qv-object-currentSelections"><tr><th>Field</th><th>Count</th><th>Values</th></tr>';
for (var i = 0; i < mySelectedFieldsLength; i++) {
html += "<tr onclick='removeMySelection(" + i + ");'><td>" + translateField(mySelectedFields[i].qField) + '</td><td>' + mySelectedFields[i].qSelectedCount + ' of ' + mySelectedFields[i].qTotal + '</td><td>' + mySelectedFields[i].qSelected + '</td></tr>';
}
html += '</table>';
$element.empty();
$element.append(html);
});
/* catch(err){
$element.empty();
$element.append('<div>Something went wrong: ' + err + '</div>');
} */
// to allow for printing
return qlik.Promise.resolve();
}
}
}
);
function removeMySelection(index) {
app.clearAll();
var mySelectedFieldsLength = mySelectedFields.length;
var selectedFields = mySelectedFields.slice();
for (var i = 0; i < mySelectedFieldsLength; i++) {
if (i == index) {
continue;
}
var field = selectedFields[i].qField;
var fieldSelectionInfoLength = selectedFields[i].qSelectedFieldSelectionInfo.length;
var names = [];
for (var j = 0; j < fieldSelectionInfoLength; j++) {
var name = selectedFields[i].qSelectedFieldSelectionInfo[j].qName;
if (isNaN(name)) {
names[j] = name;
} else {
names[j] = parseInt(name);
}
}
app.field(field).selectValues(names, false, true)
}
}
function translateField(field) {
var size = fieldsTranslations.length;
for (var i = 0; i < size; i++) {
if (fieldsTranslations[i].Field == field) {
return fieldsTranslations[i].Label;
}
}
return field;
}