0

I'm aware of this answer: How do I add a cancel button to my jqgrid?, and I'm attempting to implement something similar, although without a button to trigger the cancel. Iv'e got a grid that loads on page load (a search that by default loads with no criteria) and I'd like to be able to cancel that default empty criteria search when the user actually executes a search with criteria. Since I don't need a button I'm trying to simplify the solution by merely keeping track of the xhr request in the loadBeforeSend method, and abort that xhr if it is not null as I load the grid. Code:

var gridXhr;
function getGridData() {
    var searchParms = ...;
    var colHeaders = [...];
    var colDefinitions = [...];
    if (gridXhr != null) {
        alert(gridXhr.readyState);
        gridXhr.abort();
        gridXhr = null;
    }
    $('#grid').jqGrid('GridUnload');
    $('#grid').jqGrid({
        defaults: {...},
        autowidth:true,
        url: "<%= Page.Request.Path %>/ExecuteSearch",
        mtype: 'POST',
        ajaxGridOptions: { contentType: "application/json" },
        postData: searchParms,
        datatype: "json",
        prmNames: {
            nd: null,
            rows: null,
            page: null,
            sort: null,
            order: null
        },
        jsonReader: {
            root: function (obj) { return obj.d; },
            page: function (obj) { return 1; },
            total: function (obj) { return (obj.d.length / 20); },
            records: function (obj) { return obj.d.length; },
            id: 'CatalogID',
            cell: '',
            repeatitems: false
        },
        loadonce: true,
        colNames: colHeaders,
        colModel: colDefinitions,
        caption: "Search Results",
        pager: 'searchPaging',
        viewrecords: true,
        loadBeforeSend: function (xhr) {
            gridXhr = xhr;
        },
        loadError: function (xhr, status, error) {
            gridXhr = null;
            if (error != 'abort') {
                alert("Load Error:" + status + "\n" + error);
            }
        },
        loadComplete: function() {
            gridXhr = null;
        },
        multiselect: <%= this.MultiSelect.ToString().ToLower() %>,
        multiboxonly: true
    }).setGridWidth(popWidth);
}

The problem I'm having is that subsequent executions of the jqGrid don't work. The loadError function is triggered, with an error (3rd parameter) of "abort." Do I need to do something more/different with the loadBeforeSend as done in the other answer?

Also, Oleg mentioned in his sample code myGrid[0].endReq(); and I can find no mention of that in the documentation - does that function exist?

Community
  • 1
  • 1
  • Sorry, but I can't follow you. I don't understand what you mean under "I'd like to be able to cancel that default empty criteria search when the user actually executes a search with criteria". Which "default empty criteria search" you mean? You use `loadonce: true`. So it will be done **only once** initial request to the server to `url: "<%= Page.Request.Path %>/ExecuteSearch"`. All searching, sorting and paging later will be implemented *locally* without and Ajax request to the server. So you can't cancel any Ajax request because no request will be send more. What scenario you mean? – Oleg Nov 26 '12 at 19:24
  • The grid is loaded by calling the "getGridData()" function in the document.ready function. This initial grid load is executed with no criteria on the search, returning all records. The user is presented with inputs to narrow the search, and they may populate those and click a "search" button which re-runs the getGridData function with the populated search criteria, unloading and re-generating the grid. The initial execution with no search criteria is the one I'd like to abort when the subsequent search is executed. – The Rascal King Nov 26 '12 at 20:58

1 Answers1

0

If I understand you correctly you need just change the line

datatype: "json",

to something like

datatype: isSearchParamEmpty(searchParms) ? "local" : "json",

In other words you should create grid with datatype: "local" instead of datatype: "json" in case of empty searching criteria. The grid will stay empty.

Additionally you should consider to create jqGrid only once if colDefinitions and colHeaders will stay unchanged for different searching criteria. Instead of recreating of the grid you could change postData only and call trigger("reloadGrid").

Oleg
  • 220,925
  • 34
  • 403
  • 798