jqGrid - how to set grid to NOT load any data initially?
Asked Answered
W

3

23

How can you create a grid but not load any data?

If I omit the url option then the loadError callback is triggered.

Currently we set url:NoData.json where NoData.json is a static file with no rows in it.

Issue is in our loadComplete callback we'd like to dipslay a message if the grid contains no data - except we don't want to display this message on the initial load. Currently we handles this as follows:

//jqGrid load complete handler
function loadComp(grid) {
    if (grid.getGridParam("url") != "NoData.json" && grid.getGridParam("records") == 0) {
        setStatus("Your search did not return any results");
    }
}

This just seems a little hacky.. would like to just have the grid not load any data initially.

Any ideas?

Whaleboat answered 14/10, 2010 at 15:5 Comment(0)
W
35

You should just use datatype: 'local' initially. At the moment when you need to load the data you should change the datatype to json or xml:

$("#list").jqGrid('setGridParam',{datatype:'json'}).trigger('reloadGrid');
Woodberry answered 14/10, 2010 at 16:32 Comment(2)
Tried that - issue is with datatype:local and no url, my loadComplete callback is still called. I was hoping for an approach that would not trigger the callback - or at least a cleaner way in the callback to determine this is the initial time the grid is loaded.Whaleboat
@Marcus: of cause the loadComplete event handle will be called in all situations. If you want to show a message only in case of loading from the server you can display the message only if datatype is equal to "json" like in #3565398Woodberry
C
25

I wanted to create a grid that loads no data when the page is loaded but loads data, when the user clicks refresh or uses the search. My solution is a little bit hacky to but is very easy and works nicely.

I am using the callback event loadBeforeSend to stop the ajax request for data when the page is loaded. My callback function removes itself so it will be executed only once.

loadBeforeSend: function (xhr, settings) {
  this.p.loadBeforeSend = null; //remove event handler
  return false; // dont send load data request
}
Chivers answered 14/12, 2011 at 12:5 Comment(2)
Why this is´nt the answer I have used this on all my grids(the ones that don´t need to load)Provo
I use the following to prevent the initial load then trigger the toolbar search which has default values loaded for the initial grid search I want loaded. loadBeforeSend: function (xhr, settings) { this.p.loadBeforeSend = null; //remove event handler setTimeout(function() { $jqGrid[0].triggerToolbar(); }); return false; // dont send load data request }Duodenary
D
-2

Do not set URL when you initialize the Grid. Set URL just before loading the grid using setGridParam function.

It works for me.

Debroahdebs answered 11/1, 2016 at 22:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.