Skip to content

Commit 838f444

Browse files
committed
Add support for column auto resize
Signed-off-by: Itay Dafna <[email protected]>
1 parent 691208a commit 838f444

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

ipydatagrid/datagrid.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,14 @@ class DataGrid(DOMWidget):
238238
Dict to specify custom column sizes
239239
The keys (strings) indicate the names of the columns
240240
The values (integers) indicate the widths
241+
auto_fit_columns : Bool (default: True)
242+
Specify whether column width should automatically be
243+
determined by the grid
244+
auto_fit_params : Dict (default: {"area":"all", "padding":30, "numCols":None})
245+
Specify column auto fit parameters. Supported parameters:
246+
1) area: where to resize column widths - 'row-header', 'body' or 'all' (default)
247+
2) padding: add padding to resized column widths (15 pixels by default)
248+
3) numCols: cap the number of columns to be resized (None)
241249
grid_style : Dict of {propertyName: string | VegaExpr | Dict}
242250
Dict to specify global grid styles.
243251
The keys (strings) indicate the styling property
@@ -331,6 +339,9 @@ class DataGrid(DOMWidget):
331339
editable = Bool(False).tag(sync=True)
332340
column_widths = Dict({}).tag(sync=True)
333341
grid_style = Dict(allow_none=True).tag(sync=True, **widget_serialization)
342+
auto_fit_columns = Bool(False).tag(sync=True)
343+
auto_fit_params = Dict(dict(area="all", padding=30, numCols=None),
344+
allow_none=False).tag(sync=True, **widget_serialization)
334345

335346
def __init__(self, dataframe, **kwargs):
336347
# Setting default index name if not explicitly

js/datagrid.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,13 +469,48 @@ export class DataGridView extends DOMWidgetView {
469469
this.grid.editable = this.model.get('editable');
470470
});
471471

472+
this.model.on_some_change(
473+
['auto_fit_columns', 'auto_fit_params'],
474+
() => {
475+
this.handleColumnAutoFit();
476+
},
477+
this,
478+
);
479+
480+
if (this.model.get('auto_fit_columns')) {
481+
this.handleColumnAutoFit();
482+
}
483+
472484
return this.updateRenderers().then(() => {
473485
this.updateGridStyle();
474486
this.updateGridRenderers();
475487
this.pWidget.addWidget(this.grid);
476488
});
477489
}
478490

491+
private handleColumnAutoFit() {
492+
// Check whether we need to auto-fit or revert to base size.
493+
const shouldAutoFit = this.model.get('auto_fit_columns');
494+
if (!shouldAutoFit) {
495+
this.grid.baseColumnSize = this.model.get('base_column_size');
496+
// Terminate call here if not auto-fitting.
497+
return;
498+
}
499+
500+
// Retrieve user-defined auto-fit params
501+
let { area, padding, numCols } = this.model.get('auto_fit_params');
502+
503+
// Data validation on params
504+
area = area ?? 'all';
505+
padding = padding ?? 30;
506+
numCols = numCols ?? undefined;
507+
console.log(area, padding, numCols);
508+
509+
// Call resize function
510+
//@ts-ignore
511+
this.grid.grid.fitColumnNames(area, padding, numCols);
512+
}
513+
479514
private updateRenderers() {
480515
// Unlisten to previous renderers
481516
if (this.default_renderer) {

0 commit comments

Comments
 (0)