1
votes

For my Kendo Datasource resulting json has a column "TotalRecords" how can i set this total to Kendo Datasource total so that my Server paging can work. My code is as below:

 var iDataSource = new kendo.data.DataSource({
            transport: {
                read:
                    {
                        url: service_URL + "GetUnconfirmedppointments",
                        datatype: "json",
                        type: "GET"
                    },
                parameterMap: function (options) {
                    return {
                        appointmentNos: Appointment_Number.value,
                        loadNumbers: Load_Number.value,
                        purchaseOrders: Purchase_Orders.value,
                        dateFrom: Date_From.value,
                        dateTo: Date_To.value,
                        warehouse: warehouses.value,
                        carrierId: cr_carrier_id,
                        sortColum: getOrderBy(options.sort),
                        sortDirection: getOrderDir(options.sort),
                        pageSize: options.pageSize,
                        pageNumber: options.page
                    };
                }
            },
            schema: {
                model: {
                    fields: {
                        APP_APPOINTMENT_ID: { type: "number" },
                        APPOINTMENT_DATE: { type: "date" },
                        LO_LOAD_NUMBER: { type: "number" }
                    }
                }
            },
            pageSize: 5,
            total: function (response) {
               return response[0].TotalRecords;
            },
            serverPaging: true,
            serverFiltering: true,
            serverSorting: true
        });

Current implementation doesn't work, even if i provided hardcoded total record number, when this datasource is binded to grid the total count only shows the number of records returned in the json which is pagesize.

2
Basically your problem is that you have defined total outside schema instead of inside. See Burke Holland answer for more details. - OnaBai

2 Answers

6
votes

I assume your total is on each and every record? If it's not, you can just do...

schema: {
  total: "TotalRecords"
}

Ideally your JSON should be structured like this...

{ data:[...], total: 100 }

However you can parse the schema if you can't send it over from the server like that. If the total count is attached to every record, you can add a field to hold it and populate it in the parse.

schema: {
  parse: function(data) {
      // assign top level array to property
      data.data = data;
      // assign the count off one of the fields to a new total field
      data.total = data.data[0].TotalRecords;

      return data;
  }
}
2
votes

As my response has total records fields with each record therefore I just moved the below into schema and it fixed it.

total: function (response) { return response[0].TotalRecords; }