I would like to resize the width of the grid after a column is resized (so the width of the grid will match the sum of the widths of the columns, including the new width of the resized column). This should prevent the horizontal scrollbar from appearing as well.
This is sort of similar to this question, where in addition to the grid resizing after hiding/showing columns, I would like it to resize when expanding/shrinking columns:
If you look at the the demo in the question provided by @Oleg, you can see that the grid does not resize upon the resize of a column.
There is a resizeStop
event I could maybe use, and then use the method setGridWidth
to set the grid to the width of the sum of the widths of the columns. I'm not sure how to sum the widths of the columns though...Maybe there is something built into JQGrid that I could use to do this easily?
Thank you so much for any advice!
I personally find the question very good. I make many suggestions how to improve the algorithm of calculation of the grid width. Twice I included the behavior like you want in my suggestions, but Tony (the developer of jqGrid) removed the part of the changes.
To implement your requirements in the current version of jqGrid is very easy, but the code uses some internal grid structures. So you should verify whether the same trick will work in the next versions too. The code of the
resizeStop
callback can be the following:The demo shows live how it works.
The complexity of the common solution for the jqGrid is higher because one needs sometimes to hold specified width of the grid. My point of view here is the following: if the user specify the
width
option during the grid initialization (creating) then he want to hold the width. On the other side if nowidth
option are specified during creating of the grid one wish (like you as wish) the calculation of the total width of the grid based on the sum of the widths of all columns. In the case if some column width will be changed, the grid width should be adjusted (recalculated) too. The problem is only that the original emptywidth
option will be overwritten during the grid creating. My suggestion would be to save the original value ofwidth
option in thewidthOrg
option during the grid creating. So one will be able to test the original value ofwidth
option and to choose the best behavior of the grid resizing after the column resizing.UPDATED: After some discussion with you in comments and debugging of jqGrid code I hope that I found the solution which works correctly independent from the value of
shrinkToFit
parameter. The solution consists from changing the code ofresizeStop
to the followingand changing of two lines of internal
showHideCol
method of jqGrid. It's first changing of the lineto
and changing of another line
to
You can get modified version of jqGrid here. The first demo uses the code with
shrinkToFit: true
and the second demo uses the same code, but without the optionshrinkToFit: true
.I though in the next day how to make the fix working also in case of the usage fixed
width
option of jqGrid at the creating time and will post my suggestion to the trirand forum.If you want uses minimized version of jqGrid you can either minimize it yourself or use the original jqGrid minimized version and overwrite only the
showHideCol
method with respect of$.jgrid.extend
. See the demo.UPDATED: After changes of the value of
this
in later versions of jqGrid one should change the above code ofresizeStop
callback to about the following:I make a function
... and then, in the grid params
I think that is the easyest way