-
Notifications
You must be signed in to change notification settings - Fork 0
/
spreadsheet.js
93 lines (91 loc) · 2.01 KB
/
spreadsheet.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
/**
* @param {string[]} columns
* @returns {string}
*/
function generateTableHead(columns) {
return `
<thead>
<tr >
${columns.map((column) => `<th>${column}</th>`).join("")}
</tr>
</thead>
`;
}
/**
* @param {any[][]} rows
* @param {{classes: string[]}} [options]
* @returns {string}
*/
function generateTableBody(rows, { classes } = { classes: [] }) {
return `
<tbody class="${classes.join(" ")}">
${rows
.map(
(row) => `
<tr>
${row
.map(
(column) => `
<td>${column}</td>
`
)
.join("")}
</tr>
`
)
.join("")}
<tbody>
`;
}
/**
* @param {{columnNames: string[], resultRows: any[]}} data
* @returns {string}
*/
function table(data) {
const htmlTableHead = generateTableHead(data.columnNames);
const htmlTableBody = generateTableBody(data.resultRows, {
classes: ["table-group-divider"],
});
const htmlTable = `
<table class="table table-striped table-bordered">
${htmlTableHead}
${htmlTableBody}
</table>
`;
return htmlTable;
}
/**
* @param {{columnNames: string[], resultRows: any[][]}} data
* @returns {void}
*/
function grid(data) {
/**
* @type {{id: string, name: string, field: string}[]}
*/
const gridColumns = data.columnNames.map((column) => {
return {
id: column,
name: column,
field: column,
};
});
/**
* @type {{[key: string]: any}[]}
*/
const gridData = data.resultRows.map((row) => {
/**
* @type {{[key: string]: any}}
*/
const mappedRowToObject = {};
row.forEach((column, i) => {
mappedRowToObject[data.columnNames[i]] = String(column);
});
return mappedRowToObject;
});
const options = {
enableCellNavigation: true,
enableColumnReorder: true,
enableAutoResize: true,
};
new Slick.Grid("#grid", gridData, gridColumns, options);
}