forked from axisgroup/my-table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paint.js
94 lines (78 loc) · 3.08 KB
/
paint.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
define([], function() {
return function($element, layout) {
// Backend API
var backendApi = this.backendApi;
// Get the text color value
var textColor = layout.textColor;
// Clear the previous contents of the container so we start from scratch each time
$element.html("");
// Create a table
var table = document.createElement("table");
// Create a header row
var hRow = document.createElement("tr");
// Add dimension labels
var dimensionInfo = layout.qHyperCube.qDimensionInfo;
for(var i = 0; i < dimensionInfo.length; i++) {
// Create a header cell
var hCell = document.createElement("th");
// Set the cell contents to the dimension label
hCell.innerHTML = dimensionInfo[i].qFallbackTitle;
// Add the cell to the header row
hRow.appendChild(hCell);
}
// Add measure labels
var measureInfo = layout.qHyperCube.qMeasureInfo;
for(var i = 0; i < measureInfo.length; i++) {
// Create a header cell
var hCell = document.createElement("th");
// Set the cell contents to the measure label
hCell.innerHTML = measureInfo[i].qFallbackTitle;
// Add the cell to the header row
hRow.appendChild(hCell);
}
// Add the header row to the table
table.appendChild(hRow);
var qMatrix = layout.qHyperCube.qDataPages[0].qMatrix;
// Iterate through each row of the qMatrix
for(var row = 0; row < qMatrix.length; row++) {
// Get current row data
var currentRow = qMatrix[row];
// Create a row
var tr = document.createElement("tr");
// Iterate through each column of the row
for(var col = 0; col < currentRow.length; col++) {
// Get current cell data
var currentCell = currentRow[col];
// Create a cell
var td = document.createElement("td");
// Add text value to the cell
td.innerHTML = currentCell.qText;
// Check if dimension, then add metadata
if(col < dimensionInfo.length) {
// Add a selectable class
td.className = "selectable";
// Add metadata for the selection
td.setAttribute("dim-col",col);
td.setAttribute("dim-index", currentCell.qElemNumber);
}
// Append the cell to the row
tr.appendChild(td);
}
// append the row to the table
table.appendChild(tr);
}
// Append the table to the $element
$element.append(table);
// Add click functions to ".selectable" items
$element.find(".selectable").on("click", function() {
// Get the dimension column number
var dimCol = parseInt(this.getAttribute("dim-col"));
// Get the dimension value index
var dimInd = parseInt(this.getAttribute("dim-index"));
// Call selectValues with these values
backendApi.selectValues(dimCol, [dimInd],true);
});
// Color the table
table.style.color = textColor;
};
});