This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathtable_widget.js
More file actions
166 lines (144 loc) · 4.98 KB
/
table_widget.js
File metadata and controls
166 lines (144 loc) · 4.98 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const ModelProperty = {
PAGE: "page",
PAGE_SIZE: "page_size",
ROW_COUNT: "row_count",
TABLE_HTML: "table_html",
};
const Event = {
CHANGE: "change",
CHANGE_TABLE_HTML: `change:${ModelProperty.TABLE_HTML}`,
CLICK: "click",
};
/**
* Renders the interactive table widget.
* @param {{
* model: any,
* el: HTMLElement
* }} options
*/
function render({ model, el }) {
// Main container with a unique class for CSS scoping
el.classList.add("bigframes-widget");
// Structure
const tableContainer = document.createElement("div");
const footer = document.createElement("div");
// Footer: Total rows label
const rowCountLabel = document.createElement("div");
// Footer: Pagination controls
const paginationContainer = document.createElement("div");
const prevPage = document.createElement("button");
const paginationLabel = document.createElement("span");
const nextPage = document.createElement("button");
// Footer: Page size controls
const pageSizeContainer = document.createElement("div");
const pageSizeLabel = document.createElement("label");
const pageSizeSelect = document.createElement("select");
// Add CSS classes
tableContainer.classList.add("table-container");
footer.classList.add("footer");
paginationContainer.classList.add("pagination");
pageSizeContainer.classList.add("page-size");
// Configure pagination buttons
prevPage.type = "button";
nextPage.type = "button";
prevPage.textContent = "Prev";
nextPage.textContent = "Next";
// Configure page size selector
pageSizeLabel.textContent = "Page Size";
for (const size of [10, 25, 50, 100]) {
const option = document.createElement("option");
option.value = size;
option.textContent = size;
if (size === model.get(ModelProperty.PAGE_SIZE)) {
option.selected = true;
}
pageSizeSelect.appendChild(option);
}
/** Updates the footer states and page label based on the model. */
function updateButtonStates() {
const rowCount = model.get(ModelProperty.ROW_COUNT);
const pageSize = model.get(ModelProperty.PAGE_SIZE);
const currentPage = model.get(ModelProperty.PAGE);
const totalPages = Math.ceil(rowCount / pageSize);
rowCountLabel.textContent = `${rowCount.toLocaleString()} total rows`;
paginationLabel.textContent = `Page ${(
currentPage + 1
).toLocaleString()} of ${(totalPages || 1).toLocaleString()}`;
prevPage.disabled = currentPage === 0;
nextPage.disabled = currentPage >= totalPages - 1;
pageSizeSelect.value = pageSize;
}
/**
* Increments or decrements the page in the model.
* @param {number} direction - `1` for next, `-1` for previous.
*/
function handlePageChange(direction) {
const current = model.get(ModelProperty.PAGE);
const next = current + direction;
model.set(ModelProperty.PAGE, next);
model.save_changes();
}
/**
* Handles changes to the page size from the dropdown.
* @param {number} size - The new page size.
*/
function handlePageSizeChange(size) {
const currentSize = model.get(ModelProperty.PAGE_SIZE);
if (size !== currentSize) {
model.set(ModelProperty.PAGE_SIZE, size);
model.save_changes();
}
}
/** Updates the HTML in the table container and refreshes button states. */
function handleTableHTMLChange() {
// Note: Using innerHTML is safe here because the content is generated
// by a trusted backend (DataFrame.to_html).
tableContainer.innerHTML = model.get(ModelProperty.TABLE_HTML);
updateButtonStates();
}
// Add event listeners
prevPage.addEventListener(Event.CLICK, () => handlePageChange(-1));
nextPage.addEventListener(Event.CLICK, () => handlePageChange(1));
pageSizeSelect.addEventListener(Event.CHANGE, (e) => {
const newSize = Number(e.target.value);
if (newSize) {
handlePageSizeChange(newSize);
}
});
model.on(Event.CHANGE_TABLE_HTML, handleTableHTMLChange);
model.on(`change:${ModelProperty.ROW_COUNT}`, updateButtonStates);
model.on(`change:_initial_load_complete`, (val) => {
if (val) {
updateButtonStates();
}
});
// Assemble the DOM
paginationContainer.appendChild(prevPage);
paginationContainer.appendChild(paginationLabel);
paginationContainer.appendChild(nextPage);
pageSizeContainer.appendChild(pageSizeLabel);
pageSizeContainer.appendChild(pageSizeSelect);
footer.appendChild(rowCountLabel);
footer.appendChild(paginationContainer);
footer.appendChild(pageSizeContainer);
el.appendChild(tableContainer);
el.appendChild(footer);
// Initial render
handleTableHTMLChange();
}
export default { render };