Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to specify ag-grid column width in percentage?

Right now, the only way I see to specify column grid width is to add a width property to column definition like:

columnDefs: any[] = [
  { 
    headerName: 'ID',
    field: 'id',
    width: 50,
    type: 'numericColumn'
  }
];

But as we can see in the following example, the column of the grid is not taking the full width of screen display.

https://stackblitz.com/edit/ag-grid-bss-test-nnojxg?file=app%2Fapp.component.ts

I want to be able to set the width in percentage for each column but I'm not able to find how to do this.

Using width: 10% is not working.

Is there any workaround for this ?

like image 762
HDJEMAI Avatar asked May 14 '18 18:05

HDJEMAI


People also ask

How do you set column width dynamically in Ag grid?

Turn column resizing on for the grid by setting resizable=true for each column. To set resizing for each column, set resizable=true on the default column definition. The snippet below allows all columns except Address to be resized by explicitly setting each column.

How do you set width of column of AG grid in react?

React Data Grid: Column Sizing. All columns can be resized by dragging the top right portion of the column.

How do you change the width of Ag grid?

Under normal usage, your application should set the width and height of the grid using CSS styles. The grid will then fit the width you provide and use scrolling inside the grid to allow all rows and columns to be viewed.

How do you set Ag grid dynamically height?

To allow the grid to auto-size it's height to fit rows, set grid property domLayout='autoHeight' . When domLayout='autoHeight' then your application should not set height on the grid div, as the div should be allowed flow naturally to fit the grid contents.


1 Answers

No, it is not possible by default. You can only set the width, minWidth and maxWidth in absolute numbers. The easiest thing for having dynamic widths in AgGrid is to use this.gridOptions.api.sizeColumnsToFit();, so they take the smallest width (according to specified widths in the colDef) for the existing values in their cells.

You can try calculating width manually (onInit) based on window-width, and then re-initialize AgGrid. But I don't recommend that because when the user resizes the window you would need to calculate again (in Window-Resize-HostListener) and re-initialize the grid. It might be possible with debounce-time (so you don't reinitialize every millisecond while the user is dragging his browser-corner), but it is kind of hacky and in the end you will get a lot of very hard-to-debug and hard-to-maintain code.

like image 106
Phil Avatar answered Oct 07 '22 06:10

Phil