81

ブラウザウィンドウのサイズが変更されたときにjqGridのサイズを変更する方法はありますか? ここで説明されている方法を試しましたが、その手法は IE7 では機能しません。

4

12 に答える 12

53

フォローアップとして:

この投稿に示されている以前のコードは、信頼性が低いため、最終的に放棄されました。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>
于 2009-10-15T21:12:18.367 に答える
36

自動サイズ変更:

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);
                });
            }
        }
于 2009-10-22T21:54:22.930 に答える
8

これは私にとってうまく機能しているようです

$(window).bind('resize', function() {
    jQuery("#grid").setGridWidth($('#parentDiv').width()-30, true);
}).trigger('resize');
于 2010-09-29T22:07:58.553 に答える
7

私はレイアウトに 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"
    }
});

// 以下に定義されているその他のグリッド...

于 2010-11-11T21:23:56.447 に答える
5

もし、あんたが:

  • have shrinkToFit: false(平均固定幅列)
  • 持ってるautowidth: true
  • 流体の高さは気にしない
  • 水平スクロールバーがある

次のスタイルを使用して、流動的な幅のグリッドを作成できます。

.ui-jqgrid {
  max-width: 100% !important;
  width: auto !important;
}

.ui-jqgrid-view,
.ui-jqgrid-hdiv,
.ui-jqgrid-bdiv {
   width: auto !important;
}

ここにデモがあります

于 2015-02-09T12:58:49.660 に答える
4

リンクのコードから借用すると、次のようなことを試すことができます。

$(window).bind('resize', function() { 
    // resize the datagrid to fit the page properly:
    $('div.subject').children('div').each(function() {
        $(this).width('auto');
        $(this).find('table').width('100%');
    });
});

このようにして、window.onresize イベントに直接バインドしています。これは、実際に質問から求めているもののように見えます。

グリッドが 100% 幅に設定されている場合、コンテナーが拡張されると自動的に拡張されますが、使用しているプラ​​グインに私が知らない複雑なものがない限り。

于 2009-05-17T19:18:12.013 に答える
3

主な答えは私にとってはうまくいきましたが、IEでアプリが非常に応答しなくなったので、提案されたようにタイマーを使用しました. コードは次のようになります ($(#contentColumn)は JQGrid が配置されている div です)。

  function resizeGrids() {
    var reportObjectsGrid = $("#ReportObjectsGrid");
    reportObjectsGrid.setGridWidth($("#contentColumn").width());
};

var resizeTimer;

$(window).bind('resize', function () {
    clearTimeout(resizeTimer);
    resizeTimer = setTimeout(resizeGrids, 60);
});
于 2011-04-05T14:06:54.730 に答える
3

スタック オーバーフロー愛好家の皆さん、こんにちは。私はほとんどの回答を楽しんでおり、いくつかに賛成票を投じましたが、奇妙な理由でIE 8で機能したものはありませんでした...しかし、これらのリンクに遭遇しました...この男は、仕事。jquery UI に追加してプロジェクトに含め、テーブルの名前と div をスローします。

http://stevenharman.net/blog/archive/2009/08/21/creating-a-fluid-jquery-jqgrid.aspx

http://code.google.com/p/codeincubator/source/browse/#svn%2FSamples%2Fsteveharman%2FjQuery%2Fjquery.jqgrid.fluid%253Fstate%253Dclosed

于 2011-08-26T13:01:05.640 に答える
1

これは動作します..

var $targetGrid = $("#myGridId");
$(window).resize(function () {
    var jqGridWrapperId = "#gbox_" + $targetGrid.attr('id') //here be dragons, this is     generated by jqGrid.
    $targetGrid.setGridWidth($(jqGridWrapperId).parent().width()); //perhaps add padding calculation here?
});
于 2013-09-03T12:30:24.190 に答える
1
autowidth: true

私にとって完璧に機能しました。ここから学びました。

于 2010-12-04T11:13:27.747 に答える
0
<script>
$(document).ready(function(){    
$(window).on('resize', function() {
    jQuery("#grid").setGridWidth($('#fill').width(), false); 
    jQuery("#grid").setGridHeight($('#fill').height(),true); 
}).trigger('resize');      
});
</script>
于 2013-05-09T07:19:25.520 に答える