-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateCountriesTable.js
86 lines (77 loc) · 2.54 KB
/
createCountriesTable.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
/** "createCountriesTable.js"
* is responsible for creating the Countries Participation Data view.
*/
import { getStorageData } from "./utils.js";
import { extractLabelFromDsId } from "./utils.js";
import { sessionKeys } from "./sessionStorageKeys.js";
/**
* Initializes a DataTable with the provided countries data and table headers.
* @param {Array} countriesDataForRows - The data to populate the table rows.
* @param {Array} dfIds - The filtered data frame IDs to generate table headers.
* @returns {DataTable} - The initialized DataTable object.
*/
export function initializeCountriesTable(countriesDataForRows, dfIds) {
const placeholder = document.querySelector("#countries-table > table");
const tableHeaders = generateTableHeader(dfIds);
const table = new DataTable(placeholder, {
data: countriesDataForRows,
columns: tableHeaders,
paging: true,
pageLength: 50,
searching: true,
autoWidth: true,
});
function updateCountriesTable(tableRows) {
table.clear();
table.rows.add(tableRows);
table.draw();
setColumnVisibility();
}
function setColumnVisibility() {
table.columns().data().each(function (value, index) {
if (index < 1) return; // Skip the first column
// Check if all values in the column are empty or undefined
var isEmptyColumn = value.every(function (cellValue) {
return cellValue === "" || cellValue === undefined;
});
if (isEmptyColumn) {
table.column(index).visible(false); // Hide the column
} else {
table.column(index).visible(true); // Show the column
}
});
}
return updateCountriesTable;
}
/**
* Generates the table header for a given set of filtered data frame IDs.
* @param {Array} dfIds - An array of filtered data frame IDs.
* @returns An array of table headers with titles, data, default content, and class names.
*/
function generateTableHeader(dfIds) {
const dsLinkingDf = getStorageData(sessionKeys.dsLinkingDf);
const dsIdLookup = dsLinkingDf.reduce((lookup, { dfId, dsId }) => {
lookup[dfId] = dsId;
return lookup;
}, {});
const artefacts = dfIds.map(dfId => ({
title: extractLabelFromDsId(dsIdLookup[dfId]),
data: dfId,
defaultContent: "",
}));
const headers = [
{
title: "Country code",
data: "countryCode",
},
...artefacts,
];
const headerRows = headers
.map(
(header) =>
`<th class="${header.data === 'countryCode' ? '' : 'rotate-180'}">${header.title}</th>`
)
.join("");
$("#countries-table > table > thead").append(`<tr>${headerRows}</tr>`);
return headers;
}