调整浏览器大小时调整 jqGrid 的大小?
IT技术
javascript
jquery
jqgrid
grid
resize
                    2021-02-01 05:42:55
                
            
        6个回答
            已经在生产中使用它一段时间了,没有任何抱怨(可能需要进行一些调整以在您的网站上看起来正确......例如,减去侧边栏的宽度等)
$(window).bind('resize', function() {
    $("#jqgrid").setGridWidth($(window).width());
}).trigger('resize');
作为后续:
这篇文章中显示的先前代码最终被放弃,因为它不可靠。按照 jqGrid 文档的建议,我现在使用以下 API 函数来调整网格大小:
jQuery("#targetGrid").setGridWidth(width);
要进行实际的调整大小,将实现以下逻辑的函数绑定到窗口的调整大小事件:
使用其父级的 clientWidth 和(如果不可用)其 offsetWidth 属性计算网格的宽度。
执行健全性检查以确保宽度变化超过 x 像素(以解决一些特定于应用程序的问题)
最后,使用 setGridWidth() 更改网格的宽度
这是处理调整大小的示例代码:
jQuery(window).bind('resize', function() {
    // Get width of parent container
    var width = jQuery(targetContainer).attr('clientWidth');
    if (width == null || width < 1){
        // For IE, revert to offsetWidth if necessary
        width = jQuery(targetContainer).attr('offsetWidth');
    }
    width = width - 2; // Fudge factor to prevent horizontal scrollbars
    if (width > 0 &&
        // Only resize if new width exceeds a minimal threshold
        // Fixes IE issue with in-place resizing when mousing-over frame bars
        Math.abs(width - jQuery(targetGrid).width()) > 5)
    {
        jQuery(targetGrid).setGridWidth(width);
    }
}).trigger('resize');
和示例标记:
<div id="grid_container">
    <table id="grid"></table>
    <div id="grid_pgr"></div>
</div>
自动调整大小:
对于 jQgrid 3.5+
        if (grid = $('.ui-jqgrid-btable:visible')) {
            grid.each(function(index) {
                gridId = $(this).attr('id');
                gridParentWidth = $('#gbox_' + gridId).parent().width();
                $('#' + gridId).setGridWidth(gridParentWidth);
            });
        }
对于 jQgrid 3.4.x:
       if (typeof $('table.scroll').setGridWidth == 'function') {
            $('table.scroll').setGridWidth(100, true); //reset when grid is wider than container (div)
            if (gridObj) {
            } else {
                $('#contentBox_content .grid_bdiv:reallyvisible').each(function(index) {
                    grid = $(this).children('table.scroll');
                    gridParentWidth = $(this).parent().width() – origami.grid.gridFromRight;
                    grid.setGridWidth(gridParentWidth, true);
                });
            }
        }
这对我来说似乎很好用
$(window).bind('resize', function() {
    jQuery("#grid").setGridWidth($('#parentDiv').width()-30, true);
}).trigger('resize');
我使用 960.gs 进行布局,所以我的解决方案如下:
    $(window).bind(
        'resize',
        function() {
                    //  Grid ids we are using
            $("#demogr, #allergygr, #problemsgr, #diagnosesgr, #medicalhisgr").setGridWidth(
                    $(".grid_5").width());
            $("#clinteamgr, #procedgr").setGridWidth(
                    $(".grid_10").width());
        }).trigger('resize');
// Here we set a global options
jQuery.extend(jQuery.jgrid.defaults, {
    // altRows:true,
    autowidth : true,
    beforeSelectRow : function(rowid, e) { // disable row highlighting onclick
        return false;
    },
    datatype : "jsonstring",
    datastr : grdata,  //  JSON object generated by another function
    gridview : false,
    height : '100%',
    hoverrows : false,
    loadonce : true,
    sortable : false,
    jsonReader : {
        repeatitems : false
    }
});
// Demographics Grid
$("#demogr").jqGrid( {
    caption : "Demographics",
    colNames : [ 'Info', 'Data' ],
    colModel : [ {
        name : 'Info',
        width : "30%",
        sortable : false,
        jsonmap : 'ITEM'
    }, {
        name : 'Description',
        width : "70%",
        sortable : false,
        jsonmap : 'DESCRIPTION'
    } ],
    jsonReader : {
        root : "DEMOGRAPHICS",
        id : "DEMOID"
    }
});
// 下面定义的其他网格...