Setting column width in angular ui grid
Asked Answered
B

6

10

I'm using angular ui-grid and have a long grid with almost 30 columns, I want to set fixed width for columns so that at least column header can be seen easily. Is there a way to set, say 10% width for all the columns, or I have to do it for each column one by one.

ui-grid

After setting the min-width on ui-grid-header-cell many columns got combined into one

.ui-grid-header-cell {
    min-width: 150px !important;
}

After setting min-width property

Baziotes answered 27/6, 2016 at 8:50 Comment(0)
C
10

If you are looking for fixed width, you can use the width property in the colomnDefs like this:

columnDefs : [
            { field : 'pageIcon', displayName : 'Page Icon', width : 25},
            { field : 'pageName', displayName : 'Page Name', width : 100},
            { field : 'count',    displayName : 'Count',     width : '30%'}
]
Cinchonize answered 20/7, 2017 at 8:0 Comment(2)
BTW, Percentage works as well, so width:30% will workCinchonize
Only not width:30% but width:'30%' <- passed as stringSikorsky
I
6

After you have set your column definitions, you can add the following

for (var i = 0; i < $scope.gridOptions.columnDefs.length; i++) {
    $scope.gridOptions.columnDefs[i].width = '10%';
}

This will set all of the columns to the width you select. With 30 columns at 10% the user will use the horizontal scrollbar to see all the columns, as you prefer. I didn't change any CSS.

Ils answered 28/6, 2016 at 21:28 Comment(0)
T
5

The best way is to use the GridOptions minimumColumnSize property:

$scope.gridOptions.minimumColumnSize = 100;

Zero performance impact and no oddities with the CSS methods.

Reference page, Ctrl-F minimumColumnSize: http://ui-grid.info/docs/#!/api/ui.grid.class:GridOptions

Tricycle answered 9/7, 2018 at 13:32 Comment(0)
C
2

Try by the following function by calling before ui-grid data source binds.

 function SetColumnWidth() {
        var cols = _.filter($scope.gridOptions.columnDefs, function (c) {
            return c.visible != false;
        });
        var maxWidth = (document.documentElement.clientWidth) / cols.length;
        for (var i = 0; i < cols.length; i++) {
            cols[i].maxWidth = parseInt(maxWidth);
        }
    }

set column width to width: "*" and set minWidth as per convinient.

This solution is work for any screen as, especially large resolution screen. It will manage to equally divide maxWidth of all columns and cover the whole area of the screen.

Cay answered 10/8, 2018 at 7:55 Comment(0)
B
0

you can add a style to your css

.ui-grid-header-cell {

    min-width: 200px !important;
    max-width: 300px !important;

}
Ballad answered 27/6, 2016 at 8:56 Comment(1)
I used .ui-grid-header-cell { min-width: 150px !important;} but, column height have changed now and many columns are merged into one. Please see the edited questionBaziotes
Y
0

If you will add width using for loop or like ways it may slow down rendering of your grid, So I would suggest to do it via CSS. Because I had same problem with 15,000 rows and 80 columns and I tried adding width using loop and it seems to slow down rendering of grid. So, Try adding below code in your CSS, it will solve your purpose.

.ui-grid-header-cell {
 min-width: 200px !important;
 max-width: 300px !important;
}

.ui-grid-cell {
min-width: 200px !important;
max-width: 300px !important;
}
Yemane answered 10/7, 2017 at 8:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.