|
1 | 1 | import {pushData} from "../session"; |
2 | 2 |
|
3 | 3 | const tpl = `<div> |
4 | | -<div class="ag-theme-{{theme}} ag-grid" style="width: 100%; height: {{height}}"></div> |
| 4 | +<div class="ag-theme-{{theme}} ag-grid" style="width: 100%; height: {{height}}"> |
| 5 | + <div class="grid-loading">⌛️ Loading Datatable...</div> |
| 6 | +</div> |
5 | 7 | <div class="ag-grid-cell-bar"></div> |
6 | 8 | <div class="ag-grid-tools"> |
7 | 9 | <div class="grid-status"> |
@@ -239,6 +241,7 @@ export let Datatable = { |
239 | 241 | spec.field_args = parse_js_func(spec.field_args, spec.js_func_key); |
240 | 242 | spec.path_args = parse_js_func(spec.path_args, spec.js_func_key); |
241 | 243 | spec.grid_args = parse_js_func(spec.grid_args, spec.js_func_key); |
| 244 | + let auto_height = spec.height == 'auto'; |
242 | 245 |
|
243 | 246 | let options = row_data_and_column_def(spec.records, spec.field_args, spec.path_args); |
244 | 247 |
|
@@ -294,17 +297,36 @@ export let Datatable = { |
294 | 297 | }, |
295 | 298 | onGridReady: (param: any) => { |
296 | 299 | grid_resolve(gridOptions); |
| 300 | + if (auto_height) { |
| 301 | + gridOptions.api.setDomLayout('autoHeight'); |
| 302 | + } |
297 | 303 |
|
298 | | - gridOptions.columnApi.autoSizeAllColumns(); |
299 | | - let content_width = 0; |
300 | | - gridOptions.columnApi.getColumns().forEach((column:any) => { |
301 | | - if(!column.getColDef().hide) |
302 | | - content_width += column.getActualWidth(); |
303 | | - }); |
304 | | - if (content_width < elem.find(".ag-grid")[0].clientWidth) { |
305 | | - // the content is smaller than the grid, so we set columns to adjust in size to fit the grid horizontally |
306 | | - gridOptions.api.sizeColumnsToFit(); |
| 304 | + let grid_elem = elem.find(".ag-grid")[0]; |
| 305 | + let on_grid_show = Promise.resolve(); |
| 306 | + if (grid_elem.clientWidth === 0) { // the grid is hidden via `display: none`, wait for it to show |
| 307 | + on_grid_show = new Promise((resolve) => { |
| 308 | + // @ts-ignore |
| 309 | + let observer = new ResizeObserver((entries, observer) => { |
| 310 | + if (grid_elem.clientWidth > 0) { |
| 311 | + observer.disconnect(); |
| 312 | + resolve(); |
| 313 | + } |
| 314 | + }); |
| 315 | + observer.observe(grid_elem); |
| 316 | + }); |
307 | 317 | } |
| 318 | + on_grid_show.then(() => { |
| 319 | + gridOptions.columnApi.autoSizeAllColumns(); |
| 320 | + let content_width = 0; |
| 321 | + gridOptions.columnApi.getColumns().forEach((column: any) => { |
| 322 | + if (!column.getColDef().hide) |
| 323 | + content_width += column.getActualWidth(); |
| 324 | + }); |
| 325 | + if (content_width < grid_elem.clientWidth) { |
| 326 | + // the content is smaller than the grid, so we set columns to adjust in size to fit the grid horizontally |
| 327 | + gridOptions.api.sizeColumnsToFit(); |
| 328 | + } |
| 329 | + }) |
308 | 330 |
|
309 | 331 | if (spec.actions.length > 0) { |
310 | 332 | elem.find('.ag-grid-tools').css('opacity', 1); |
@@ -367,6 +389,7 @@ export let Datatable = { |
367 | 389 | let ag_version = spec.enterprise_key ? 'ag-grid-enterprise' : 'ag-grid'; |
368 | 390 | // @ts-ignore |
369 | 391 | requirejs([ag_version], function (agGrid) { |
| 392 | + elem.find('.grid-loading').remove(); |
370 | 393 | new agGrid.Grid(elem.find(".ag-grid")[0], gridOptions); |
371 | 394 | if (spec.instance_id) { |
372 | 395 | // @ts-ignore |
|
0 commit comments