0

Is there any way to disable jqgrid local cache??? I have a page which you build some filters and based on this params I create the jqgrid.

The problem is that jqgrid doesn't change postdata params! I'm mean, on the second, third, fourth, etc search, the results is always equals the first one. My jqgrid defaults are:

jQuery.extend(jQuery.jgrid.defaults, {
        ajaxGridOptions: {
            contentType: 'application/json;',
            type: "POST",
            cache: false,
            beforeSend: function () {
                $(".loading").show(); 
            }
        },
        serializeGridData: function (postData) {
            return JSON.stringify(postData);
        },
        datatype: 'json',
        autowidth: true,
        height: '100%',
        rowNum: 10,
        rowList: [10, 20, 30],
        hidegrid: false,
        prmNames: {
            search: "isSearch",
            nd: null,
            rows: "numRows",
            page: "numPage",
            sort: "orderBy",
            order: "orderType"
        },
        viewrecords: true,
        gridComplete: function () {
            $(".loading").hide();
        },
        jsonReader: {
            root: function (obj) { return obj.d.rows; },
            page: function (obj) { return obj.d.page; },
            total: function (obj) { return obj.d.total; },
            records: function (obj) { return obj.d.rows.length; },
            repeatitems: false
        }
    });

The jqGrid creation:

$myGrid.jqGrid({
      postData: { from: jQuery.parseDate(fromQueryString), to: jQuery.parseDate(toQueryString) },
      url: "/Search.aspx/Find",
      colNames: ['Test'],
      colModel: [
         { name: 'Test', index: 'Test', sortable: false, width: 40 }
      ],
      sortname: "Date",
      sortorder: "desc",
      jsonReader: { id: "ID" },
      pager: "pagerControl",
      caption: "Results"
 });
Oleg
  • 220,925
  • 34
  • 403
  • 798
Alexandre
  • 7,004
  • 5
  • 54
  • 72

1 Answers1

1

If I understand you correctly, the page has come controls build some controls like input#from and input#to (<input> fields with ids "from" and "to") which defines the interval which you what to send to the server together with other parameters.

I suppose that you code looks like

var $myGrid = $("#list"),
    fromQueryString = "",
    toQueryString = "",
    createGrid = function () {
        $myGrid.jqGrid({
            postData: {
                from: jQuery.parseDate(fromQueryString),
                to: jQuery.parseDate(toQueryString)
            },
            url: "/Search.aspx/Find",
            colNames: ['Test'],
            colModel: [
                { name: 'Test', index: 'Test', sortable: false, width: 40 }
            ],
            sortname: "Date",
            sortorder: "desc",
            jsonReader: { id: "ID" },
            pager: "pagerControl",
            caption: "Results"
        });
    },
    myRefresh = function () {
        var fromQueryString = $("#from").val(),
            toQueryString = $("#to").val();

        createGrid();
    };

$("#from").change(myRefresh);
$("#from").change(myRefresh);
createGrid();

The problem is that the above code is wrong. The createGrid create grid only at the first time and then (inside of myRefresh) it just dose nothing after the test that the grid $myGrid is already created. You can understand this it you imagine that many parts of the grid: the title, the column headers, the pager and so on must be created only once. At the next time one need just reload the content of the grid body.

The correct way to refresh jqGrid will be call of .trigger("reloadGrid") instead of trying to create the grid multiply times. One can fix the code to the following

var $myGrid = $("#list"),
    fromQueryString = "",
    toQueryString = "",
    createGrid = function () {
        $myGrid.jqGrid({
            postData: {
                from: function () {
                    return jQuery.parseDate($("#from").val());
                },
                to: function () {
                    jQuery.parseDate($("#to").val());
                }
            },
            url: "/Search.aspx/Find",
            colNames: ['Test'],
            colModel: [
                { name: 'Test', index: 'Test', sortable: false, width: 40 }
            ],
            sortname: "Date",
            sortorder: "desc",
            jsonReader: { id: "ID" },
            pager: "pagerControl",
            caption: "Results"
        });
    },
    myRefresh = function () {
        $myGrid.trigger("reloadGrid", [{page: 1}]);
    };

$("#from").change(myRefresh);
$("#from").change(myRefresh);
createGrid();

In the case the methods from and to of the postData will be called on every grid reload. For example if the user click on the column header to change the grid sorting or if the user choose to display another page of the grid. You can read here more about the approach.

The only thing what you can change additionally is the code of serializeGridData which should test the properties of postData and call the function if needed. See here for details.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • My approach was to call .jqGrid('GridUnload') – Alexandre Dec 19 '11 at 18:52
  • @Alexandre. The usage of `GridUnload` is possible, but you problem is sure exist in the part of your code which you not posted. Where the code when you fill `fromQueryString` and `toQueryString` and where you use `GridUnload`? Moreover I find strange that you use `url: "/Search.aspx/Find"` - the URL with ASPX and not ASMX extension or ASHX. Could you extend the code which you used? – Oleg Dec 19 '11 at 20:03
  • It's webmethods, take a look http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/ – Alexandre Dec 20 '11 at 12:09