Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ag-grid with single column, expand column to grid width

Tags:

ag-grid

I've setup an ag-grid in an Angular application. I'm trying to make the single column fill in the whole width of the grid.

enter image description here

but then I get this.

enter image description here

    <div class="row" ng-controller="ChapterCtrl" ng-init="currentUNID='9D2DFE6D50C53A98C1257F5900337F37'">
    <div class="col-sm-4">
        <div >
            <div ag-grid="gridOptions" class="ag-fresh" style="height: 800px"></div>
        </div>
    </div>

   var columnDefs = [
    {headerName: "Subject", field: "Subject"}
];

$scope.gridOptions = {
    columnDefs: columnDefs,
    angularCompileRows: true,

Do you have any hints for me? The documentation https://www.ag-grid.com/javascript-grid-width-and-height/ didn't help me.

Many thanks!

like image 671
Andrew Magerman Avatar asked Oct 20 '16 15:10

Andrew Magerman


People also ask

How do you increase column width on Ag grid?

Just like Excel, each column can be 'auto resized' by double clicking the right side of the header rather than dragging it. When you do this, the grid will work out the best width to fit the contents of the cells in the column.

How do you set column width dynamically in Ag grid?

Enable Sizing 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 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?

You can use ag-grid feature AutoHeight = true in the column Configuration. or if you want to calculate the height dynamically you should use getRowHeight() callback and create DOM elements like div and span , and add your text in it, then the div will give the OffsetHeight for it then you wont see any cropping of data/ ...


3 Answers

What I'd suggest is set the grid to have a % or use vh for its width, then the grid will grow/shrink according to the parent container.

To make the columns change size, you can hook into an event an resize each time the grid size changes:

var gridOptions = {
    onGridSizeChanged: () => {
        gridOptions.api.sizeColumnsToFit();
    }    
    ...other gridOptions
};
like image 140
Sean Landsman Avatar answered Sep 30 '22 05:09

Sean Landsman


Try 'flex' column property:

{ headerName: 'Test', flex: 1}

enter image description here

You can also use this to expand only last column.

Oficial documentation

like image 43
lachty Avatar answered Sep 30 '22 05:09

lachty


If you already know the width of the grid, then set that same width in the columnDefs:

var columnDefs = [
{headerName: "Subject", field: "Subject", width: 350//width in pixels}

If your grid is set up to have dynamic width depending on the users window size then you might be able to get the width of the table element, then put that width into the columnDefs afterwards.

Edit:

For responsive design:

1) add an id to make lookup easy:

<div id="myTable" ag-grid="gridOptions" class="ag-fresh" style="height: 800px"></div>

2) find the table then find it's width:

var colWidth = document.getElementById('myTable').clientWidth;

or if you prefer jquery:

var colWidth = $('#myTable').width()

3) set the width in the columnDefs:

var columnDefs = [
{headerName: "Subject", field: "Subject", width: colWidth}
like image 39
Jarod Moser Avatar answered Sep 30 '22 03:09

Jarod Moser