Stahlkocher
Stahlkocher

Reputation: 349

jqGrid: add loadonce as parameter to the AJAX-request

I have a PHP-script to handle the AJAX-Requests of many different jqGrid's.

I generate the "ORDER BY" statement with the 'sidx' and 'sord' parameters and the "LIMIT" statement with the 'page' and 'rows' parameters.

Similar to the PHP-example here.

The problem is, that in the PHP-script I can not determine if the loadonce-parameter of the current jqGrid is set or not. But only if it is not set, I have to filter the returned data (LIMIT by page and rows).

How can I force jqGrid to send an additional parameter? I dont want to change all my Grids. Is there a global way of doing it?

------ EDIT ------

With the help from this answers (here and here) i got this now.

$.extend($.jgrid.defaults, {
    postData: {
        loadingType: function() {
            var isLoadonce = $("#list1").jqGrid('getGridParam', 'loadonce');
            console.log('isLoadonce: ' + isLoadonce);
            return isLoadonce ? 'loadAll' : 'loadChunk';
        },
    },
});

This works, if the Grid has the ID "list1". How can I reference the current Grid without ID?

------ EDIT 2 ------

This seems to work. It looks to me a bit like a hack. Is there a better way?

$.extend($.jgrid.defaults, {
    serializeGridData: function(postData) {
        var isLoadonce = $(this).jqGrid('getGridParam', 'loadonce');
        var newPostData = $.extend(postData, {
            loadingType: isLoadonce ? 'loadAll' : 'loadChunk'
        });
        return $.param(newPostData);
    },
});

Upvotes: 0

Views: 377

Answers (2)

Stahlkocher
Stahlkocher

Reputation: 349

With serializeGridData, jqGrid provides an event to modify the data sent with the Request. The event is called in the context of the current Grid, so we can access the current Grid with this.

By exdending $.jgrid.defaults we can make all Grids sending their loadonce parameter as additional requestparameter without changing any Grid.

$.extend($.jgrid.defaults, {
    serializeGridData: function(postData) {
        var isLoadonce = $(this).jqGrid('getGridParam', 'loadonce');
        var newPostData = $.extend(postData, {
            loadingType: isLoadonce ? 'loadAll' : 'loadChunk'
        });
        return $.param(newPostData);
    },
});

Upvotes: 0

Mark
Mark

Reputation: 3123

To pass in an extra parameter you can add:

 postData: { ExtraDataName: ExtraDataValue },

then whenever jqGrid goes to get data it will pass that name pair to your controller.

Upvotes: 2

Related Questions