From f46c961daf42ddbc777279c812605d80fa98a900 Mon Sep 17 00:00:00 2001 From: Gervwyk Date: Thu, 7 Apr 2022 23:51:04 +0200 Subject: [PATCH] feat: Add sizeColumnsToFit and autoSize methods, closes #7 --- README.md | 4 ++++ src/AgGrid.js | 10 ++++++++++ src/AgGridInput.js | 10 ++++++++++ 3 files changed, 24 insertions(+) diff --git a/README.md b/README.md index b48e0c3..e64d966 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,10 @@ The block types are hosted at: ### Methods - `exportDataAsCsv`: When called, table data will be downloaded in csv format. +- `sizeColumnsToFit`: When called, size table column widths to fit all columns to table width. +- `autoSize`: When called, auto size columns. The following can be passed as the first argument of `args`. + - `skipHeader: boolean`: Do not consider header content width when auto-sizing columns. + - `columnIds: string[]`: List of `colId`s for which to calculate auto-size when called. ### AgGridAlpine Example diff --git a/src/AgGrid.js b/src/AgGrid.js index 161dbb3..915730c 100644 --- a/src/AgGrid.js +++ b/src/AgGrid.js @@ -45,6 +45,16 @@ class AgGrid extends React.Component { this.props.methods.registerMethod('exportDataAsCsv', (args) => this.gridApi.exportDataAsCsv(args) ); + this.props.methods.registerMethod('sizeColumnsToFit', () => this.gridApi.sizeColumnsToFit()); + this.props.methods.registerMethod('autoSize', ({ skipHeader, colIds }) => { + const allColumnIds = colIds || []; + if (!colIds) { + this.gridColumnApi.getAllColumns().forEach((column) => { + allColumnIds.push(column.getId()); + }); + } + this.gridColumnApi.autoSizeColumns(allColumnIds, skipHeader); + }); } onRowClick(event) { diff --git a/src/AgGridInput.js b/src/AgGridInput.js index cadb795..e271ba2 100644 --- a/src/AgGridInput.js +++ b/src/AgGridInput.js @@ -47,6 +47,16 @@ class AgGridInput extends React.Component { this.props.methods.registerMethod('exportDataAsCsv', (args) => this.gridApi.exportDataAsCsv(args) ); + this.props.methods.registerMethod('sizeColumnsToFit', () => this.gridApi.sizeColumnsToFit()); + this.props.methods.registerMethod('autoSize', ({ skipHeader, colIds }) => { + const allColumnIds = colIds || []; + if (!colIds) { + this.gridColumnApi.getAllColumns().forEach((column) => { + allColumnIds.push(column.getId()); + }); + } + this.gridColumnApi.autoSizeColumns(allColumnIds, skipHeader); + }); } onRowClick(event) {