7

I have a jQueryUI dialog (#locDialog) which has a jqGrid ($grid) inside it. When the Dialog opens (initially, but it gets called whenever it opens), I want the $grid to resize to the size of the $locDialog. When I do this initially, I get scrollbars inside the grid (not inside the dialog).

If I debug the code, I see the width of the $grid is 677. So, I call setGridWidth() again and check the width and now I have 659, which is 18px less, which is the size of the scroll area for the jqGrid (Dun-dun-dun..)

When I rezie the dialog, I resize the grid as well, and everything is happy - no scrollbars, except where necessary.

My dialog init code:

$locDialog = $('#location-dialog').dialog({
    autoOpen: false,
    modal: true,
    position: ['center', 100],
    width: 700,
    height:500,
    resizable: true,
    buttons: {
                "Show Selected": function() {alert($('#grid').jqGrid('getGridParam','selarrrow'));},
                "OK": function() {$(this).dialog('close');},
                "Cancel": function() {$(this).dialog('close');}
             },
    open: function(event, ui) {
        $grid.setGridHeight($(this).height()-54); 
          // No idea why 54 is the magic number here
        $grid.setGridWidth($(this).width(), true);
    },
    close: function(event, ui) {

    },
    resizeStop: function(event, ui) {
        $grid.setGridWidth($locDialog.width(), true);
        $grid.setGridHeight($locDialog.height()-54);
    }
});

I am curious if anyone has seen this before. Really, it isn't the end of the world if I initially have unnecessary scrollbars at first, but it is just odd that when I call setGridWidth initially, it doesn't take into account the scroll area of 18px.

As far as the magical number 54, that is the number I had to subtract from the height of the dialog value to get the grid to render without unnecessary scrollbars.


I think it may be a timing issue, though this doesn't make a lot of sense. Perhaps I should call an event once the grid is completely loaded. This may ensure it calculates its width correctly.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
Dan
  • 724
  • 2
  • 9
  • 23

1 Answers1

15

There are some cases, where jqGrid calculate the width a little incorrect. Mostly I have problems with grid width, but in some cases on IE6 also with the height. So I have to write a small function to fix the problem.

var fixGridWidth = function (grid) {
    var gviewScrollWidth = grid[0].parentNode.parentNode.parentNode.scrollWidth;
    var mainWidth = jQuery('#main').width();
    var gridScrollWidth = grid[0].scrollWidth;
    var htable = jQuery('table.ui-jqgrid-htable', grid[0].parentNode.parentNode.parentNode);
    var scrollWidth = gridScrollWidth;
    if (htable.length > 0) {
        var hdivScrollWidth = htable[0].scrollWidth;
        if ((gridScrollWidth < hdivScrollWidth))
            scrollWidth = hdivScrollWidth; // max (gridScrollWidth, hdivScrollWidth)
    }
    if (gviewScrollWidth != scrollWidth || scrollWidth > mainWidth) {
        var newGridWidth = (scrollWidth <= mainWidth)? scrollWidth: mainWidth;  // min (scrollWidth, mainWidth)
        // if the grid has no data, gridScrollWidth can be less then hdiv[0].scrollWidth
        if (newGridWidth != gviewScrollWidth)
            grid.jqGrid("setGridWidth", newGridWidth);
    }
};

var fixGridHeight = function (grid) {
    var gviewNode = grid[0].parentNode.parentNode.parentNode;
    //var gview = grid.parent().parent().parent();
    //var bdiv = jQuery("#gview_" + grid[0].id + " .ui-jqgrid-bdiv");
    var bdiv = jQuery(".ui-jqgrid-bdiv", gviewNode);
    if (bdiv.length) {
        var delta = bdiv[0].scrollHeight - bdiv[0].clientHeight;
        var height = grid.height();
        if (delta !== 0 && height && (height-delta>0)) {
            grid.setGridHeight(height-delta);
        }
    }
};

var fixGridSize = function (grid) {
    this.fixGridWidth(grid);
    this.fixGridHeight(grid);
};

In this code "main" is the id of parent div inside of which the grid will be created. In the code I test (scrollWidth > mainWidth) whether the width of "main" allow increasing of jqGrid width.

Correct place to call this function is inside of loadComplete:

loadComplete: function() {
    var gr = jQuery('#list');
    fixGridSize(gr);
}

and inside of "done", if you use 'columnChooser' if use use Query('#list').jqGrid('columnChooser');

(in this example I use also 'gridResize'.)

Earlz
  • 62,085
  • 98
  • 303
  • 499
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • I will definitely give this a look, Oleg - many thanks for the code. I'm not too worried about it at this point as the problem only occurs right when the user opens a dialog. Once they resize/reload, it fixes itself. Again, likely a timing issue. Thanks again :D – Dan Apr 26 '10 at 12:17
  • @Oleg: Can you look at something similar [question](http://stackoverflow.com/questions/8292341/horizontal-scroll-bar-in-jqgrid-when-needed) –  Nov 28 '11 at 06:47
  • I know this is an old answer but... I've tried to use this code but it doesn't work properly. I've used @Oleg's `fixGridWidth` function and yes, it resizes de grid. But columns remain with the same size so there is half of the grid empty, columns don't resize. Do you know how to resize columns too? – Isthar Sep 18 '14 at 08:46
  • @Isthar: You should post new question where you describe detailed what you do and what problem you have. – Oleg Sep 18 '14 at 09:20
  • @Oleg: can you help me in one point. I have used the same code. and I'm also calling `fixGridSize(gr);` on `window.onresize` event, width is getting auto calculated that good but **height is getting reduced** when I do _Zoom Out_ (i.e. on `CTRL -`). – Raunak Gupta Oct 05 '16 at 13:17
  • @RaunakGupta: Sorry, but the answer is written more as 6 years ago. The problem not exist in current versions on jqGrid. I recommend you to try the current code of [free jqGrid](https://github.com/free-jqgrid/jqGrid) - the fork of jqGrid, which I develop. If you would still have any problems you can ask new question and include the demo, which reproduces the problem. You should describe the test case too, including the information about the web browser, which you use in the tests. – Oleg Oct 05 '16 at 13:24
  • thanks @Oleg, for such a quick response, I'm using the latest commercial version, I made a dirth fix, and i change one line at it is working except Zoom 50%. `var height = 800;//grid.height();` – Raunak Gupta Oct 05 '16 at 13:28
  • 1
    @RaunakGupta: Sorry, I develop alternative fork, which I can be used free jqGrid completely free of charge. I can't provide any support of Gurrido jqGrid JS. You can try (at least temporary) to replace URLs to jqGrid files to URLs from CDN (see [the wiki](https://github.com/free-jqgrid/jqGrid/wiki/Access-free-jqGrid-from-different-CDNs)) and verify whether your problem is *common* for both forks of jqGrid. In the case I could try to help you to fix the problem. An example of the demo is http://jsfiddle.net/OlegKi/andm1299/26/ – Oleg Oct 05 '16 at 13:38
  • 1
    @RaunakGupta: See [here](http://free-jqgrid.github.io/getting-started/index.html) some more examples of the usage free jqGrid. – Oleg Oct 05 '16 at 13:43
  • @Oleg: yup in your case it is working fine, I think I messed up totally at my end I think I put many ingredients from many version, and today app has been made live; So client asked for fix then I'll move totally to your example. **thank you** very much. – Raunak Gupta Oct 05 '16 at 14:09