ブラウザウィンドウのサイズが変更されたときにjqGridのサイズを変更する方法はありますか? ここで説明されている方法を試しましたが、その手法は IE7 では機能しません。
12 に答える
フォローアップとして:
この投稿に示されている以前のコードは、信頼性が低いため、最終的に放棄されました。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"
}
});
// 以下に定義されているその他のグリッド...
もし、あんたが:
- 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;
}
リンクのコードから借用すると、次のようなことを試すことができます。
$(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% 幅に設定されている場合、コンテナーが拡張されると自動的に拡張されますが、使用しているプラグインに私が知らない複雑なものがない限り。
主な答えは私にとってはうまくいきましたが、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);
});
スタック オーバーフロー愛好家の皆さん、こんにちは。私はほとんどの回答を楽しんでおり、いくつかに賛成票を投じましたが、奇妙な理由でIE 8で機能したものはありませんでした...しかし、これらのリンクに遭遇しました...この男は、仕事。jquery UI に追加してプロジェクトに含め、テーブルの名前と div をスローします。
http://stevenharman.net/blog/archive/2009/08/21/creating-a-fluid-jquery-jqgrid.aspx
これは動作します..
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?
});
autowidth: true
私にとって完璧に機能しました。ここから学びました。
<script>
$(document).ready(function(){
$(window).on('resize', function() {
jQuery("#grid").setGridWidth($('#fill').width(), false);
jQuery("#grid").setGridHeight($('#fill').height(),true);
}).trigger('resize');
});
</script>