Search code examples
jqgridfree-jqgrid

jqGrid beforeProcessing stop loading and ReloadGrid


This is my example: enter link description here

How to reload automatically?

beforeProcessing: function (data, status, xhr) { 
    if (data.rows === '') {
        $('#jqGridPreparate').jqGrid('clearGridData');
        return false;
    }
    if (data.inputCol) {
        $("td.ui-search-input input#id_prep").val('');
        $("td.ui-search-input input#id_opisanie").val(data.inputCol);
        var rplc = $.parseJSON($("#jqGridPreparate")[0].p.postData.filters);
        for (var i=0; i < rplc.rules.length; i++) {
            if (rplc.rules[i].field === 'prep') {
                rplc.rules[i].field = 'opisanie';
            }
        }
        $.extend($("#jqGridPreparate")[0].p.postData,{filters:JSON.stringify(rplc)});
        $("#jqGridPreparate")[0].triggerToolbar(); // not WORK
    }
}

Solution

  • I'm not sure that I full understand what you want to implement. I can guess that you want to apply the filter if some additional information will returned by from the server.

    It seems that you implemented some custom agreement between jqGrid and the server about returned data. rows part of the server response should be array of items. In your case the server could set empty string as the value of rows instead. You try to use inputCol property of the server response to force filtering of previously loaded data. You makes some custom modifications of existing filter, you fill some fields of the filter toolbar and finally you try to use triggerToolbar. The code is very unclear and it contains a lot of assumptions which you don't described directly. It's not clear for me whether you want to start local filtering or jqGrid should send new request to the server with modified filter.

    In any way, if you want to start some kind of reloading/filtering inside of other callback function, you shuld take in consideration some basic rules. First of all the method triggerToolbar or triggering reloadGrid works synchronously. It means that jqGrid first makes triggerToolbar, which makes reloadGrid, till the end and then will be executed the next line of code after $("#jqGridPreparate")[0].triggerToolbar();. Because of that it's strictly recommended to place all calls of triggerToolbar or reloadGrid inside of setTimeout body. It allows to process the current callback til the end and only then to make reloading. If the response from the server contains your custom information instead of typical jqGrid data then you should return false from beforeProcessing to stop the standard processing.

    If you can construct postData.filters then you don't need to use triggerToolbar to apply the filter. Instead of that you need just set search: true parameter of jqGrid additionally and just trigger reloadGrid.

    The corresponding code could be about the following:

    beforeProcessing: function (data, status, xhr) {
        var $self = $(this), p = $self.jqGrid("getGridParam");
        if (data.rows === "") {
            $self.jqGrid("clearGridData");
            return false; // don't process the data by jqGrid
        }
        if (data.inputCol) {
            var i, rplc = $.parseJSON(p.postData.filters);
            for (i = 0; i < rplc.rules.length; i++) {
                if (rplc.rules[i].field === "prep") {
                    rplc.rules[i].field = "opisanie";
                }
            }
            p.postData.filters = JSON.stringify(rplc);
            setTimeout(function () {
                // apply the filter
                p.search = true;
                $self.trigger("reloadGrid", [{page: 1}]);
            }, 50);
            return false; // don't process the data by jqGrid
        }
    }