javascript - Dojo DataGrid 高度调整大小不起作用

标签 javascript dojo dojox.grid.datagrid

我正在尝试使用 Dojo DataGrid 对象进行简单的“调整大小”行为。 “宽度”按预期工作正常。但是“高度”不起作用,并且网格仅显示初始渲染值。

这是重现问题的 HTML 文件的超链接。

https://www.ibm.com/developerworks/community/files/form/anonymous/api/library/45f7c930-197b-4a26-88c1-2445045c3b0b/document/3db02339-113a-44b3-828b-3c149b1a2f86/media/teste_datagrid.htm

这是源代码:

<!DOCTYPE html>
<!-- https://dojotoolkit.org/reference-guide/1.10/dojox/grid/DataGrid.html -->
<html >
<head>
<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js">    </script>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.10.4/dijit/themes/claro/claro.css">
<style type="text/css">
@import "http://ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojox/grid/resources/claroGrid.css";
/*Grid needs an explicit height by default*/
#gridDiv {
height: 100%;
width: 100%;
}
</style>
<script>dojoConfig = {async: true, parseOnLoad: false}</script>
<script>
require(['dojo/_base/lang', 'dojox/grid/DataGrid', 'dojo/data/ItemFileWriteStore', 'dojo/dom', 'dijit/form/Button', 'dojo/domReady!'],
function(lang, DataGrid, ItemFileWriteStore, dom, Button){
/*set up data store*/
var data = {
  identifier: "id",
  items: []
};
var data_list = [
  { col1: "normal", col2: false, col3: 'But are not followed by two hexadecimal', col4: 29.91},
  { col1: "important", col2: false, col3: 'Because a % sign always indicates', col4: 9.33},
  { col1: "important", col2: false, col3: 'Signs can be selectively', col4: 19.34}
];
var rows = 60;
for(var i = 0, l = data_list.length; i < rows; i++){
    data.items.push(lang.mixin({ id: i+1 }, data_list[i%l]));
}
var store = new ItemFileWriteStore({data: data});
/*set up layout*/
var layout = [[
  {'name': 'Column 1', 'field': 'id', 'width': '100px'},
  {'name': 'Column 2', 'field': 'col2', 'width': '100px'},
  {'name': 'Column 3', 'field': 'col3', 'width': '200px'},
  {'name': 'Column 4', 'field': 'col4', 'width': '150px'}
]];
/*create a new grid*/
var grid = new DataGrid({
    id: 'grid',
    store: store,
    structure: layout,
    rowSelector: '20px'});

    /*append the new grid to the div*/
    grid.placeAt("gridDiv");

    /*Call startup() to render the grid*/
    grid.startup();

// Create a button programmatically:
var myButton = new Button({
    label: "Click me!",
    onClick: function(){
        dom.byId("parentDiv").style.height = "400px";
        dom.byId("parentDiv").style.width = "400px";
        if (grid){
            grid.resize();
    //grid.update();
        dom.byId("result1").innerHTML = ("h: "+dom.byId("parentDiv").style.height+"/ w:"+dom.byId("parentDiv").style.width);
            alert("Was grid resized ?");
    }
    }
}, "progButtonNode").startup();
});
</script>
</head>
<body class="claro">
<div id="parentDiv">
    <div id="gridDiv"></div>
</div>
<button id="progButtonNode" type="button"></button>
<div id="result1"></div>
</body>
<script language="javascript">
document.getElementById("parentDiv").style.height = "200px";
document.getElementById("parentDiv").style.width = "200px";
document.getElementById("parentDiv").style.backgroundColor = "blue";
</script>
</html>

谢谢

CWLO。

最佳答案

DataGrid 在最初渲染时会缓存其父内容框的大小,并且不会自动检测更改,因此请为网格指定一个大小:

grid.resize({ w: 400, h: 400 });

如果你想更笼统一点:

var parentDiv = dom.byId('parentDiv');
grid.resize({ w: parentDiv.clientWidth, h: parentDiv.clientHeight });

通过将grid._parentContentBoxHeight设置为null然后调用grid.resize()来删除缓存的值也可以,但这很漂亮黑客。

关于javascript - Dojo DataGrid 高度调整大小不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31819910/

相关文章:

dojo - 第 1 页上的复选框 "ticked"反射(reflect)在 EnhancedGrid 的第 2 页中

xpages - 如何返回 XPage 上 Dojo DataGrid 中可编辑列的值?

javascript - Dojo:小部件实例共享相同的变量

JavaScript:根据特定属性检查对象数组是否没有重复项?

jquery - Dojo 1.6 中相当于 jQuery(html) 的是什么?

dialog - 如何以编程方式创建带有 dojox.grid.DataGrid 的 dijit.Dialog

Javascript ES6 - 类内部的枚举像静态枚举一样在外部使用

javascript - 什么是测试 JQuery 的良好测试环境

javascript - AngularJS:暂时禁用数据绑定(bind)。

javascript - 为什么我的 onclick 不起作用?