2

I've got a Dojo DataGrid defined in my HTML:

<table data-dojo-type="dojox.grid.DataGrid" data-dojo-id="mainTable">
  <thead>
    <tr>
      <th field="id" width="128px">id</th>
      <th field="foo" width="128px">foo</th>
      <th field="bar" width="128px">bar</th>
      <th field="baz" width="128px">baz</th>
    </tr>
  </thead>
</table>

and I've got a JavaScript function that tries to get a handle to this DataGrid and assign a model to it:

[...]

mainTable.setStore(new dojo.data.ItemFileWriteStore({ data : data }));

[...]

According to the Dojo docs, adding the data-dojo-id tag generates "a JavaScript variable that will be created that will hold the grid object. This can then be referenced in scripts." But my Chromium JS console complains "'mainTable' is undefined". So when is this variable created and what do I have to do before I can access it?

Caleb
  • 5,084
  • 1
  • 46
  • 65
Dan O
  • 6,022
  • 2
  • 32
  • 50
  • do you call your mainTable.setStore inside a dojo.ready block ? Are you using parseOnLoad:true ? If not, did you invoke the parser ? – Philippe Dec 28 '11 at 16:02

2 Answers2

0

data-dojo-id is used to declare the store, not the widget. This then creates a global variable of the name specified in data-dojo-id, which you can reference from javascript.

David Fraser
  • 6,475
  • 1
  • 40
  • 56
0

The id value that you are setting in your declaritave markup is the id of the domNode in the document object. The javascript that you are then trying to run is looking for a global variable by that name which is not at all the same thing as the node name in the DOM.

There are two ways to do this. One is to declare jsId="something" as part of your declarative markup. This will cause dijit to create a reference to the widget instance as a global variable of the name something. This can the the same name as your DOM node id because they are in different namespaces (the document object vs global javascript namespace).

The other way to do it is to use a lookup function in your javascript to find the javascript object via it's DOM node id like this:

var mainTable = dijit.byId("mainTable");
mainTable.setStore(...);
Caleb
  • 5,084
  • 1
  • 46
  • 65
  • if dojo-data-id simply sets the id of a domNode, then I guess I don't see the need for it - why couldn't I just set the id directly with `id="mainTable"`? Regardless, I've tried both adding a `jsId` attribute and calling `dijit.byId()` and neither work; I get a different error message now, "Cannot call method 'setStore' of undefined". – Dan O Dec 28 '11 at 15:03