JQGrid: Resize Grid Width After Column Resized
Asked Answered
D

2

6

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!

Denney answered 6/3, 2012 at 22:43 Comment(0)
B
9

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:

resizeStop: function () {
    $(this.bDiv).find('>div:first>table.ui-jqgrid-btable:first')
                .jqGrid('setGridWidth', this.newWidth);
}

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 no width 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 empty width option will be overwritten during the grid creating. My suggestion would be to save the original value of width option in the widthOrg option during the grid creating. So one will be able to test the original value of width 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 of resizeStop to the following

resizeStop: function () {
    var $grid = $(this.bDiv).find('>div:first>table.ui-jqgrid-btable:first'),
        shrinkToFit = $grid.jqGrid('getGridParam', 'shrinkToFit');

    $grid.jqGrid('setGridWidth', this.newWidth, shrinkToFit);
}

and changing of two lines of internal showHideCol method of jqGrid. It's first changing of the line

cw = this.widthOrg? this.widthOrg: parseInt(this.width,10);

to

cw = this.widthOrg && $t.p.shrinkToFit ? this.widthOrg: parseInt(this.width,10);

and changing of another line

$($t).jqGrid("setGridWidth",$t.p.shrinkToFit === true ? $t.p.tblwidth : $t.p.width );

to

$($t).jqGrid("setGridWidth", $t.p.tblwidth);

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 option shrinkToFit: 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 of resizeStop callback to about the following:

resizeStop: function () {
    var $self = $(this),
        shrinkToFit = $self.jqGrid("getGridParam", "shrinkToFit");

    $self.jqGrid("setGridWidth", this.grid.newWidth, shrinkToFit);
}
Barrelhouse answered 7/3, 2012 at 10:26 Comment(15)
Oleg - Thank you so much for your reply!!! This works perfectly. I understand your point as well. I notice that when one column is resized, all other columns are resized proportionally. To be able to resize a column without resizing the other columns, I found that 'shrinkToFit' needs to be set to false. This, however, breaks the grid shrinking/expanding when removing/adding columns. Soooo, I can override the 'done' option of the column chooser, but I'm not sure how to calculate the grid width there.Denney
@icats: Do you mean optional second paremeter shrink of setGridWidth or the shrinkToFit option of jqGrid? Could you describe more exactly which settings you jqGrid has (especially the values of shrinkToFit and forceFit which you use). Could you additionally describe exactly which behavior you want to have in the grid after 1) one column will be resized by the user (the width on the column will be reduced or increased) 2) one column will be hidden or shown by the column chooser.Barrelhouse
Oh, I'm sorry for my terrible description! My grid options were virtually the same as yours (default shrinkToFit, forceFit, etc as I did not define them). But, I added shrinkToFit: false in order for column resizing to not expand/shrink other columns proportionally. Adding shrinkToFit: false broke the grid automatically resizing after adding/removing columns.Denney
Basically, I would just like the grid to be the size of the sum of the column widths at all times. So, adding columns or expanding columns should increase the grid width, and removing columns or shrinking columns should decrease the grid width. Also, the initial width of the grid should just be the sum of the widths of the visible columns (with the column widths I defined in the colModel). Thank you so much for your responses!Denney
Oh, and I would like the expanding/shrinking of columns to not affect the widths of the other columns :)Denney
...done: function(perm) { (perm) { this.jqGrid("remapColumns", perm, true); var gridWidth = ???; this.jqGrid("setGridWidth",gridWidth);} } - I suspect the done option code on the column chooser would look something like this in order to fix the "bug" where when the grid option shrinkToFit is set to false, the grid does not shrink/expand when columns are removed/added. I'm just not sure how to perform the equivalent of the code you have in resizeStop here. I'll keep digging!Denney
in the 'done' option of the column chooser, I added the following code: this.jqGrid("remapColumns", perm, true); var gridWidth = $(this)[0].clientWidth; this.jqGrid('setGridWidth', gridWidth); I'm not sure if using $(this)[0].clientWidth to find the sum of the widths of the columns is the best approach (is this cross-browser compatible/upgrade safe?), but, this does fix the issue where when setting grid option shrinkToFit = false the width of the grid does not resize when adding/removing columns (with my code added in done the grid will shrink/expand when column was removed/added)Denney
@icats: Try to increase the size of some column in your solution (resize it) and then hide the resized column with respect of the columnChooser. Do you have unchanged size of all other columns? I think currently that there are some bugs in resizing of jqGrid in case of shrinkToFit: false. So one should first fix the bugs and then the solution will be very easy. I will try to examine the problem later.Barrelhouse
After resizing & then hiding the resized column, the size of all other columns remains unchanged (which is what I desire), and the grid shrinks to the width of the remaining columns. However, I am noticing two bugs when resizing another column after the above steps: the little drag gray line indicator is off the same amount I shrunk the column I removed (see i.imgur.com/vgxZS.jpg). And, sometimes the code you gave above to execute on resizeStop will calculate the grid width to be too small (by the size I shrunk the column I removed), & the horizontal scroll bars show.Denney
again, thank you so much for your help! That was a good bug to find with my solution that I did not think about :)Denney
@icats: You are welcome! I took a look in the code of jqGrid and made two small fixes. Could you look at the demo which uses the fixed code and shrinkToFit: false almost the same demo, but without shrinkToFit: false and confirm that all works correctly?Barrelhouse
I tried the things I did to cause the 'bugs' I mentioned above (and played around with it for a while performing various resizing/adding/dropping columns) and both grids function as I would expect them to!! The first demo with the shrinkToFit: false behaves in exactly the same manner I was looking for! Thank you so much! Would you recommend minimizing this fixed code and using it directly? Or, can you think of a good way for me to make it easy for future devs of my application to upgrade JQGrid and maintain this functionality (I will leave them plenty of notes:))?Denney
@icats: I updated my answer and I hope that I answered on all your questions.Barrelhouse
Your solution works perfectly! I took your suggestion of using the original JQGrid minimized code and overwrite only the showHideCol method with respect of $.jgrid.extend. Thank you so much for your help (very very much)!!!!Denney
@icats: You are welcome! It's a pleasure to answer on good questions.Barrelhouse
P
0

I make a function

var grid_setAutoWidth = function() {

    var columnas = $(this).jqGrid('getGridParam', 'colModel');

    var anchoTotal = 0;
    for (var i = 0; columnas[i]; i++) {
        anchoTotal += columnas[i].width;
    }

    anchoTotal += 50;
    $(this).jqGrid('setGridWidth', anchoTotal);

};

... and then, in the grid params

shrinkToFit: false,
loadComplete: grid_setAutoWidth,
resizeStop: grid_setAutoWidth,

I think that is the easyest way

Photochronograph answered 15/8, 2013 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.