简体   繁体   中英

Dojo DataGrid, programmatic creation

I'm trying to create a DataGrid dynamically. Declarative creation is working fine:

A source of data:

<span dojoType="dojo.data.ItemFileReadStore" 
    jsId="theStore" url="dataonly001.json">
  </span>

a simple layout:

<script type="text/javascript" >    
  var layout = [ {
            field: 'custId',
            name: 'Id',                   
        } // more items elided ...
     ]; 
</script>

The grid:

<body class="tundra" id="mainbody"> 
   <div id="grid"
     dojoType="dojox.grid.DataGrid"
     store="theStore"
     structure="layout"  
     autoWidth="true"     
 ></div>   
</body>

And I get the grid displayed nicely. Instead, I want to create the grid dynamically. For the sake of figuring out what's broken I've tried to use exactly the same layout and store, removing just the grid declaration and adding this script:

  <script type="text/javascript" djConfig="parseOnLoad: true, debugAtAllCosts: true">

  dojo.addOnLoad(function(){
        // initialise and lay out home page
        console.log("Have a store:" + theStore);
        console.log("Have a structure:" + layout);

        var grid = new dojox.grid.DataGrid({
                id : "grid",
                store : theStore,
                clientSort : "true",
                structure : layout
        });
        grid.startup();

    dojo.place(grid.domNode, dojo.body(), "first");                     
  });

 </script>

The effect that I get is a completely empty rectangle is displayed. Using FireBug I can see that the DataGrid widget is created but that it has no rows or columns. So my guess is that the datastore or layout are not being passed correctly. However, it does appear that at the point of creation the values of theStore and layout are correct.

Suggestions please, or indeed a working example of a programmatic grid might solve the problem.

First attach the grid to the DOM and create the widget instance then.

So simply instead of

grid.startup();
dojo.place(grid.domNode, dojo.body(), "first");  

write

dojo.place(grid.domNode, dojo.body(), "first");  
grid.startup();

You can even set the container node of every Dojo widget right in the constructor.

var grid = new dojox.grid.DataGrid({
    ⋮                     
}, dojo.byId('grid_container'));
grid.startup();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM