Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto Size all columns to fit content

Tags:

ag-grid

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)

like image 385
Nico Avatar asked Aug 13 '18 12:08

Nico


People also ask

How do I resize all columns to fit data?

Select the column or columns that you want to change. On the Home tab, in the Cells group, click Format. Under Cell Size, click AutoFit Column Width. Note: To quickly autofit all columns on the worksheet, click the Select All button, and then double-click any boundary between two column headings.


1 Answers

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();
like image 181
Dan Obregon Avatar answered Oct 20 '22 00:10

Dan Obregon