3

显示/隐藏 Dojo 数据网格的最佳方式是什么?

我正在编写一个页面,该页面在显示数据网格之前需要一些用户输入。因此,理想情况下,页面的网格部分在页面加载时应该是空白/空的。一旦用户提供了特定的输入,网格应该出现在指定的位置。

我尝试设置 style="display:none" 然后编写 js 来设置 display="block" 并在 none 和 block 之间切换,但它只显示网格的轮廓。

如果我省略 style="display:none" 网格会在加载时显示,我可以随后隐藏/取消隐藏它,而不会出现任何问题。

我很困惑为什么当我在加载时设置 display=none 时它​​的行为会有所不同。解决此问题的方法或替代方法是什么?

非常感谢任何见解。

<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Dojo Test</title>

        <link rel="stylesheet" href="/cv/static/css/demo.css" media="screen">
        <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.7.1/dijit/themes/claro/claro.css">
    <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.7.1/dojo/dojo.js" data-dojo-config="isDebug: true, async: true"></script>
        <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.7.1/dojo/resources/dojo.css">

        <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.7.1/dojox/grid/resources/Grid.css">
        <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.7.1/dojox/grid/resources/claroGrid.css">

        <style type="text/css">

        #target-grid{
                width: 950px;
                height: 350px;
                /*visibility:hidden;*/
                display: none;
                position:relative; left:0; top:0; z-index:10;
               border:1px solid #ebebeb; border-top:1px solid #f2f2f2;
               -webkit-border-radius:5px; -moz-border-radius:5px; border-radius:5px;
               behavior:url('/media/css/PIE.htc');
               float: left;


        }   
        </style>




        <script>

        var myStore, dataStore, grid;

        require([

            "dojo/store/JsonRest",

            "dojo/store/Memory",

            "dojo/store/Cache",

            "dojox/grid/DataGrid",

            "dojo/data/ObjectStore",

            "dojo/query",

            "dojo/domReady!"

        ], function(JsonRest, Memory, Cache, DataGrid, ObjectStore, query){

            myStore = Cache(JsonRest({target:"http://127.0.0.1:8080/cv/insight/data/2/"}), Memory());

            grid = new DataGrid({

                store: dataStore = ObjectStore({objectStore: myStore}),

                structure: [

                    {name:"State Name", field:"name", width: "200px"},

                    {name:"Abbreviation", field:"id", width: "200px", editable: true}

                ]

            }, "target-node-id"); // make sure you have a target HTML element with this id


                            grid.startup();

            query("#save").onclick(function(){

                dataStore.save();

            });

        });

    </script>





        <script>
        require(["dijit/form/Button", "dojo/_base/Deferred", "dojo/_base/xhr", "dojo/_base/array", "dojo/dom-construct", "dojo/dom","dojo/domReady!"],
                            function(Button,Deferred, xhr, baseArray, domConstruct, dom) {
            var button = new Button({
                label: "Click Me!",
                onClick: function(){

                                        .......





                                    }
            }, "btn");
            button.startup();

            var button2 = new Button({
                iconClass:"dijitIconNewTask",
                showLabel:false,
                label: "Click Me!", // analogous to title when showLabel is false
                onClick: function(){




                                        var ele = dom.byId("target-grid");

                                        if(ele.style.display == "block") {

                                                dojo.setStyle("target-grid", {"display": "none"});
                                                //dojo.style(ele.domNode, 'display', 'none');

                                        }
                                        else {


                                                dojo.setStyle("target-grid", {"display": "block"});
                                               //dojo.style(ele.domNode, 'display', 'block');
                                                //ele.startup();
                                                ele.resize();


                                        }
                                    }

            }, "btn2");

            button2.startup();
        });

    </script>
        </script>

</head>
<body class="claro">
    <h1>Demo</h1>
            <button id="btn"></button>
            <button id="btn2"></button>
    <ul id="userlist"></ul>
            <div id="result1"></div>

            <div id="target-grid"></div>



</body>

4

3 回答 3

3

我认为设置显示属性是正确的。如果我没记错的话,你也应该在网格上调用 resize 。如果网格被隐藏,它的启动方法会被调用吗?您可能也需要这样做。

// to hide grid
dojo.style(grid.domNode, 'display', 'none');

// to show grid
dojo.style(grid.domNode, 'display', '');
// grid.startup(); // needed???
grid.resize();

回应评论

我使用了以下html。如果没有 resize 调用,我会看到网格的轮廓。通过调整大小调用它可以工作。

<div dojoType="dojo.data.ItemFileReadStore" jsId="jsonStore" url="dojox/grid/tests/support/countryStore.json" ></div>

<table dojoType="dojox.grid.DataGrid"
    jsid="grid" id="grid" 
    store="jsonStore" query="{ name: '*' }" rowsPerPage="20" rowSelector="20px"
    style="width: 400px; height: 200px; display:none;">
    <thead>
        <tr>
            <th field="name" width="300px">Country/Continent Name</th>
            <th field="type" width="auto">Type</th>
        </tr>
    </thead>
</table>

我更新了您发布的代码,以便在代码中创建商店数据并且还更改了以下内容

}, "target-node-id"); // make sure you have a target HTML element with this id
//to
}, "target-grid"); // make sure you have a target HTML element with this id

ele.resize();
// to
grid.resize();

它奏效了。

于 2012-02-29T18:27:29.867 回答
0

对此有一个技巧:

我使用了一个字段集,然后在显示时在网格上调用了一个 sort()。似乎工作。

<fieldset id="target-grid1" style="display:none"> 
  <div id="target-grid"></div> 
</fieldset> 

然后在您的切换过程调用中:

dojo.setStyle("target-grid", {"display": "block"}); 
grid.sort();
于 2012-03-01T14:33:24.317 回答
0

您可以将其最初放置在屏幕外,然后在您希望它显示时将其移动到屏幕上(Dojo 使用工具提示来实现)。

于 2012-03-23T18:09:55.000 回答