restazurejqgridodatajqgrid-inlinenav

How one could use server side sorting and paging with Azure Mobile Services


I am using jqGrid (inlineNav) with data from azure service and interested in learning how one could use server side sorting and paging with Azure Mobile Services.

Please share thoughts around this.


Solution

  • Windows Azure Mobile Services provides REST API which can be used to get/insert/edit/delete data of the the tables which you configured for the corresponding access (see the documentation). Query records operation request uses HTTP GET verb. It supports Open Data Protocol (OData) URI options $orderby, $skip, $top and $inlinecount which could be used to fill jqGrid.

    $("#list4").jqGrid({
        url : 'https://mohit.azure-mobile.net/tables/Schedules',
        datatype: "json",
        height: "auto",
        colModel: [
            { name: "RouteId", width: 50 },
            { name: "Area", width: 130 }
        ],
        cmTemplate: {editable: true, editrules: { required: true}},
        rowList: [10, 20, 30],
        rowNum: 10,
        prmNames: { search: null, nd: null },
        ajaxGridOptions: {
            contentType: "application/json",
            headers: {
                "X-ZUMO-APPLICATION": "myKey"
            }
        },
        serializeGridData: function (postData) {
            if (postData.sidx) {
                return {
                    $top: postData.rows,
                    $skip: (parseInt(postData.page, 10) - 1) * postData.rows,
                    $orderby: postData.sidx + " " + postData.sord,
                    $inlinecount: "allpages"
                };
            } else {
                return {
                    $top: postData.rows,
                    $skip: (parseInt(postData.page, 10) - 1) * postData.rows,
                    $inlinecount: "allpages"
                };
            }
        },
        beforeProcessing: function (data, textStatus, jqXHR) {
            var rows = parseInt($(this).jqGrid("getGridParam", "rowNum"), 10);
            data.total = Math.ceil(data.count/rows);
        },
        jsonReader: {
            repeatitems: false,
            root: "results",
            records: "count"
        },
        loadError: function (jqXHR, textStatus, errorThrown) {
            alert('HTTP status code: ' + jqXHR.status + '\n' +
                'textStatus: ' + textStatus + '\n' +
                'errorThrown: ' + errorThrown);
            alert('HTTP message body (jqXHR.responseText): ' + '\n' + jqXHR.responseText);
        },
        pager: "#pager1",
        sortname: "Area",
        viewrecords: true,
        caption: "Schedule Data",
        gridview: true
    });
    

    Some comments to the above code: