dijit.byId("grid") - TypeError: grid is undefined or grid.setStore is not a function

4205
5
01-25-2013 05:25 AM
WalterEralio
New Contributor III
Getting the error trying to set a datagrid.  grid is defined as:

<table data-dojo-type="dojox.grid.DataGrid" data-dojo-id="grid" id="grid" data-dojo-props="rowsPerPage:'5', rowSelector:'20px'">
      <thead>
        <tr>
           <th field="BUS_FID">ID</th>
   <th field="L_F_ADD">From</th>
   <th field="R_T_ADD">To</th>
   <th field="PREFIX">Prefix</th>
   <th field="PRETYPE">Pretype</th>
   <th field="NAME">Name</th>
   <th field="POSTAL_L">Zip</th>
   <th field="STATE00_L">State</th>
   <th field="COUNTY00_L">County</th>
   <th field="MCD00_L">MCD</th>
   <th field="PLACE00_L">Place</th>
        </tr>
      </thead>
</table>


both:  dojo.require("dojox/grid/DataGrid");
        dojo.require("dojo/data/ItemFileReadStore");

(btw: like in the example http://help.arcgis.com/en/webapi/javascript/arcgis/jssamples/#sample/find_map_datagrid  .. error occurs on trying to access grid since dijit.byId('grid') is undefined)

       //Create data store and bind to grid.
        store = new dojo.data.ItemFileReadStore({ data:data });
        var grid = dijit.byId('grid');
        grid.setStore(store);

PS:  store and data have the right values

I managed to get the structure with: dijit.byId(document.getElementById("grid") but then the grid.setStore(store) function gives me grid.setStore is not a function
0 Kudos
5 Replies
CraigMcDade
Occasional Contributor III
can you add a more complete code sample of what you have? Is your

//Create data store and bind to grid.
store = new dojo.data.ItemFileReadStore({ data:data });
var grid = dijit.byId('grid');
grid.setStore(store);


within the showResults(results) function?
0 Kudos
derekswingley1
Frequent Contributor
You're using the legacy module loader (dojo.require), but you're giving it AMD-style paths. Try:
dojo.require("dojox.grid.DataGrid");
dojo.require("dojo.data.ItemFileReadStore");


Also, since you're creating your grid in markup, you need to set dojoConfig.parseOnLoad to true. Put this before your script tag that references the JS API:
<script>var dojoConfig = { parseOnLoad:true }</script>


Running dijit.byId('grid') in the console in sample you linked returns a reference to the grid.
0 Kudos
DianaBenedict
Occasional Contributor III
I would reccomend that you ensure that the HTML is set up correctly so that the grid renders correctly. Sometimes things fail and you may not even know that the grid never really did get instantiated correctly.  Here is a sample of how I create the grid in-line HTML code, basically it is a similar example to what ESRI suggestes. this came out of some testing that I was doing. Note that I am using "dojoType="dojox.grid.DataGrid" jsid="grid" slightly different than you are.

<div dojotype="dijit.layout.ContentPane" region="left" style="width:300px">
              <table dojoType="dojox.grid.DataGrid" jsid="grid" id="grid" rowsPerPage="5" rowSelector="20px" style="height:300px; width:250px">
               <thead>
                  <tr>
                    <th field="OBJECTID" width="20px">ID</th>
                    <th field="PERMANENT_IDENTIFIER" width="100px">PermID</th>
                    <th field="META_PROCESSID" width="100px">MetaProcessID</th>
                  </tr>
                </thead>
              </table>
            </div>

To gain access to your Dojo widget you should only have to require the appropriate dojo libraries/modules (if you are not using the AMD notation)

dojo.require("dojox.grid.DataGrid");
dojo.require("dojo.data.ItemFileReadStore");

Then you can get the widget by using the following code in your JS page or script block.
var grid = dijit.byId('grid');

Note that you will need to make sure that all your HTML renders correctly in order for everything to work correctly. I have encountered situations where I thought it was the code for the Grid for example but it turns out that the HTML before that was failing and therefore not all the objects were getting set as I expected. Use something like firebug to ensure that there are no load errors on your page.

Good Luck and hope this helps in some sort of way.

Diana
0 Kudos
WalterEralio
New Contributor III
I would reccomend that you ensure that the HTML is set up correctly so that the grid renders correctly. Sometimes things fail and you may not even know that the grid never really did get instantiated correctly.  Here is a sample of how I create the grid in-line HTML code, basically it is a similar example to what ESRI suggestes. this came out of some testing that I was doing. Note that I am using "dojoType="dojox.grid.DataGrid" jsid="grid" slightly different than you are.

<div dojotype="dijit.layout.ContentPane" region="left" style="width:300px">
              <table dojoType="dojox.grid.DataGrid" jsid="grid" id="grid" rowsPerPage="5" rowSelector="20px" style="height:300px; width:250px">
               <thead>
                  <tr>
                    <th field="OBJECTID" width="20px">ID</th>
                    <th field="PERMANENT_IDENTIFIER" width="100px">PermID</th>
                    <th field="META_PROCESSID" width="100px">MetaProcessID</th>
                  </tr>
                </thead>
              </table>
            </div>

To gain access to your Dojo widget you should only have to require the appropriate dojo libraries/modules (if you are not using the AMD notation)

dojo.require("dojox.grid.DataGrid");
dojo.require("dojo.data.ItemFileReadStore");

Then you can get the widget by using the following code in your JS page or script block.
var grid = dijit.byId('grid');

Note that you will need to make sure that all your HTML renders correctly in order for everything to work correctly. I have encountered situations where I thought it was the code for the Grid for example but it turns out that the HTML before that was failing and therefore not all the objects were getting set as I expected. Use something like firebug to ensure that there are no load errors on your page.

Good Luck and hope this helps in some sort of way.

Diana



It was exactly this.. 2 ids with same name previously on the same html...
another error came up on the STORE now (on the ItemFileReadStore,js TypeError: _4e is undefined  but I'll have to check the code before going on)

THANKS TO ALL!!
0 Kudos
Dave_J_Zhang
New Contributor

I got the same problem, any luck on you end?

Dave

0 Kudos