26

All my searches turned up for sizeColumnsToFit and autoSizeColumns which is not what I want.

My grids have many columns, so it scroll horizontal which is fine

But I cannot know in advance what would be the most space needed for the widest text in a column, so want the grid to auto size all columns to accommodate for whatever is the longest line of text in the cell.

Can one do that? (pretty much like have nowrap on a html table column, not that ag-grid wrap text, it just hide what is too long)

AswinRajaram
  • 1,519
  • 7
  • 18
Nico
  • 139
  • 3
  • 6
  • 23
  • 1
    reading between the lines, i see that your use case may involve hidden columns (if the table has horizontal scroll). note that you cannot control the size of non-visible columns, unless you turn off virtualization (the grid's feature that renders only what's should be visible on screen) – Eliran Malka Feb 25 '20 at 14:49

6 Answers6

43

Grid Resizing: https://www.ag-grid.com/javascript-grid-resizing/

sizeColumnsToFit - this tries to squeeze all the columns in the view - no horizontal scrolling

autoSizeAllColumns - this tries to size all columns to to fit the data - horizontal scrolling

// If you need to resize specific columns 
var allColIds = params.columnApi.getAllColumns()
    .map(column => column.colId);
params.columnApi.autoSizeColumns(allColIds);

// If you want to resize all columns
params.columnApi.autoSizeAllColumns();
Vishnudev Krishnadas
  • 10,679
  • 2
  • 23
  • 55
Dan Obregon
  • 941
  • 9
  • 22
11

I passed through this and it took me some time to make it work the way I wanted to, by that I mean :

  • Use all the available space
  • Make each column take the width required to display its content correctly

Solution :

  • Add the width parameter for each column (requires some manual tuning to set the right values)
  • Call gridApi.sizeColumnsToFit() in the onGridReady which will resize to take the available space while making sure that columns having bigger width will take more space
const gridOptions = {
 ...
 columnDefs: [
  {
    ...,
    width: 100
  },
  {
    ...,
    width: 50
  },
  ...
 ],
 ...
 onGridReady: (event) => event.api.sizeColumnsToFit(); 
};
ibenjelloun
  • 7,425
  • 2
  • 29
  • 53
2

Simply use flex instead of width in the column definitions. The grid will adjust automatically.

user2555515
  • 779
  • 6
  • 20
1

You can find a complete example for auto resizing all column. column-sizing

Simply call autoSizeColumns() function, all columns will be resized according to content.

gridColumnApi.autoSizeColumns(allColumnIds, skipHeader);
Hassan Ajaz
  • 613
  • 8
  • 15
1

This will help if anyone is looking to do this in python streamlit.

  • Assuming gb is a AgGrid
  • By setting flex=1, the columns will automatically adjust their width based on the content and available space within the grid
  • If you have many columns and don't want to specify them one by one, you can use the configure_default_column method to set the default configuration for all columns.
gb = GridOptionsBuilder.from_dataframe(df_glb)
gb.configure_default_column(
    flex=1,
    minWidth=100,
    maxWidth=500,
    resizable=True,
)
asquare
  • 203
  • 2
  • 7
0

Please remove the width property in columnDefs array.

risingagain
  • 210
  • 2
  • 12