Javascript – jqGrid dynamic shrinkToFit depending on column number

javascriptjqgridjquery

I'm using jqGrid in my project with the width set to 900px and the same configuration for all grids.

If I have 4 columns with the width set for each one and the option shrinkToFit is set to true, the columns will stretch to fit the 900px.

But if I have a second grid with 14 columns and I set the width to each one, the jqGrid won't respect the column's width because shrinkToFit is set to true. If I set it to false, I solve this problem. But another problem came up. The first grid now is taking the width 150px to each column and the columns are not stretching to fit the grid anymore.

Question:
Is there a way to achieve horizontal scrollbars when the sum of the columns width is bigger than the grid width without change the configuration?

Best Answer

Sorry, but it's very difficult to understand which exactly behavior you would like to have.

First of all you have to decide whether you know which width of the columns of the grid is the best or you want to have scaling (stretching) of the columns and the column width will be interpreted just as weight. If you want the last behavior you should choose default setting shrinkToFit: true. In the case the values of the width 100, 200 and 300 means absolutely the same as the values 1, 2 and 3.

I personally have in the practice mostly grids with many columns. So the usage of default setting shrinkToFit: true on a small monitor with follow large squeezing of columns. So one will not really able to see any helpful information on the grid. So I prefer to use shrinkToFit: false in the case. I know which is responsible width in pixel for every column and set the values as width during grid creating. So the users with large monitor see all of the most important information directly. Moreover I don't specify any width parameter for the grid. The user uses scroll bars of the page if needed. Your requirements could be different and which should choose what the value is the best for you.

If you still would prefer to use shrinkToFit: true, but do use some other value of the width as the sum of all widths of the grid you have one more option. You can use setGridWidth method. It has optional second boolean parameter shrink. The usage of setGridWidth method with one parameter will make shrinking based on the value of shrinkToFit option of the grid, but explicit usage of second shrink parameter allows you to change the width without shrinking. Probably it's wnat you want to implement.

One more option I described in the answer. jqGrid don't have any method which allows to change the width of some column of the grid to the new value. In the answer I described workaround (see the code of resizeStop callback from the answer) which uses internal dragEnd method for changing the coloumn width. The demo will be used for subgrid, but the same approach one can use for every grid.