This is a migrated thread and some comments may be shown as answers.

Can't get total from datasource

4 Answers 480 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Software
Top achievements
Rank 1
Software asked on 04 Apr 2014, 09:15 PM
I've been struggling with this all day, and I can't believe it's this hard to get the total number of records returned in a datasource!  Very frustrating...  I've read the documentation over and over and nothing seems to work for me.  I've got a json response that is structured like this: 
{ records: [], totalRecordCount: 20, queryRecordCount: 20 }

Here's my dataSource code:
return new kendo.data.DataSource({
    transport: {
        read: {
            type: "POST",
            data: request,
            cache: false,
            url: ServiceUri + "/Associates",
            dataType: "json"
        }
    },
    serverPaging: false,
    pageSize: 25,
    schema: {
        data: "records",
        total: "totalRecordCount",
        model: {
            fields: {
                Id: { type: "number" },
                Name: { type: "string" },
                Sex: { type: "string" },
                Race: { type: "string" },
                Height: { type: "string" },
                Weight: { type: "string" },
                Age: { type: "number" },
                DOB: { type: "date" },
                LastChanged: { type: "date", field: "IdentifierDate" }
            }
        }
    },
});

So when I want to get the total I pass a function to this dataSource (i.e. callback) and return it in the requestEnd event, like so:
requestEnd: function(e) {
    callback(e);
},

I put this after the schema block in the dataSource, although I don't think it should matter...?  This event never fires, or if it does I cannot break inside of it in Chrome.  So I tried using the change event in a similar manner, that never fires either.  Here's a complete function attempting to get the dataSource and retrieve the total (normally I would not include the events in the dataSource and then bind them again after the dataSource, but for brevity sake...): 
GetAssociates: function (id, pgSize, callback) {
    var request = {
        "SearchParameters": {
            ID: id
        }
    };
    return new kendo.data.DataSource({
        transport: {
            read: {
                type: "POST",
                data: request,
                cache: false,
                url: ServiceUri + "/Associates",
                dataType: "json"
            }
        },
        serverPaging: false,
        pageSize: pgSize,
        schema: {
            data: "records",
            total: "totalRecordCount",
            model: {
                fields: {
                    Id: { type: "number" },
                    Name: {type: "string" },
                    Sex: {type: "string" },
                    Race: {type: "string" },
                    Height: {type: "string" },
                    Weight: {type: "string" },
                    Age: {type: "number" },
                    DOB: {type: "date" }
                }
            }
        },
        requestEnd: function(e) {
            callback(e);
        },
        change: function(e) {
            callback(e);
        }
    }).bind('change', callback).bind('requestEnd', callback);
}

I just want to do a dataSource.total() and find out if there's anything in my dataSource, and I CANNOT.  It always returns 0!  And I can see that my response is not zero, so why does it always say zero for the total??  I believe I'm following standard async methods to make sure the response has completed, so I'm stumped and completely frustrated, once again.

I know I sound pretty whiny in this message, but it's just been a long day.  I sure would appreciate any help!

Thanks,

Eddie

4 Answers, 1 is accepted

Sort by
0
Accepted
Kiril Nikolov
Telerik team
answered on 07 Apr 2014, 08:22 AM
Hi Eddie,

Using the requestEnd event to get the total is a bit too early, as the dataSource, will not have time to calculate the data correctly and updated its total. Using the change event however, should do the job. I have created a small runnable sample, showing this functionality, please check it out and let me know if it helps:

http://trykendoui.telerik.com/@Kiril/EFAb

Regards,
Kiril Nikolov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Software
Top achievements
Rank 1
answered on 07 Apr 2014, 06:26 PM
Hi Kiril,

Thanks for your reply.  I tried that but it appears that my dataSource is not being retrieved, which is why neither the requestEnd nor change events are being fired.  I'm not sure why the dataSource isn't being retrieved though...  If I make a jQuery ajax request using the same data, same parameters, same url, just before the dataSource, I get the data I am expecting in the response.  The dataSource call begins, but never completes, and there are no exceptions thrown, no errors to catch, and I can step into any of the events for dataSource.

Here's my code:

var request = {
    "RequestParameters": {
        SystemsUserId: Global.userId,
        UniqueUserIdentifiers: Global.netName,
        IpAddress: Global.ip
    },
    "SearchParameters": {
        ID: Global.id
    }
};
 
var url = Global.ServiceUri + "/Associates";
 
$.ajax({
    type: "POST",
    async: false,           
    url: url,
    dataType: 'json',
    data: request,
    success: function (data) {
        //THIS WORKS
        return data;
    },
    error: function (error, statusText) {
        return error.statusText;
    }
});
 
return new kendo.data.DataSource({
    transport: {
        read: {
            type: "POST",
            data: request,
            cache: false,
            url: url,
            dataType: "json"
        }
    },
    serverPaging: false,
    pageSize: Global.pageSize,
    requestStart: function(e) {
        // NEVER FIRES (BREAKPOINT DOESN'T STOP)
        console("request starting");
    },
    change: function (e) {
        // NEVER FIRES (BREAKPOINT DOESN'T STOP)
        console(this.total());
    },
    requestEnd: function(e) {
        // NEVER FIRES (BREAKPOINT DOESN'T STOP)
        console(this.total());
    },
    schema: {
        data: "records",
        total: "totalRecordCount",
        model: {
            fields: {
                Id: { type: "number" },
                Name: {type: "string" },
                Sex: {type: "string" },
                Race: {type: "string" },
                Height: {type: "string" },
                Weight: {type: "string" },
                Age: {type: "number" },
                DOB: {type: "date" },
                Matches: { type: "number", field: "AssociatesMatchCount" }
            }
        }
    }
});

Anything look incorrect about this?  I've been running this all morning and I cannot get it to execute the request.

Thanks again for your help!
Eddie
0
Software
Top achievements
Rank 1
answered on 07 Apr 2014, 06:33 PM
Disregard my last post - I just realized I was never calling the fetch() method against this dataSource.  Sometimes the solutions are too obvious...  Sorry!
0
Kiril Nikolov
Telerik team
answered on 08 Apr 2014, 10:50 AM
Hi Eddie,

Glad you found the solution.

In case you have any further questions, please do not hesitate to contact us.

Regards,
Kiril Nikolov
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Data Source
Asked by
Software
Top achievements
Rank 1
Answers by
Kiril Nikolov
Telerik team
Software
Top achievements
Rank 1
Share this question
or