How to get total record count from datasource?

1 Answer 4792 Views
Data Source
Mike
Top achievements
Rank 1
Mike asked on 10 Apr 2012, 08:59 PM
I am trying to get the total record count from a datasource using:

var totalCount = myDataSource.total();

The datasource has more than 1 record in it and I am able to display the records in a ListView, but the above variable is always zero.

Can someone tell me if I'm using the right command?
Ryan
Top achievements
Rank 1
commented on 10 Apr 2012, 09:04 PM

The ListView will invoke the transport and read the data.  You are probably trying to access count before the datasource has actually been fetched.

console.log('Fetching...');
 
ds.fetch(function(){
  console.log('Actually fetched');
  console.log(ds.total());
  console.log(ds.totalPages());
});
Mike
Top achievements
Rank 1
commented on 10 Apr 2012, 09:09 PM

Here's the order I'm using:

1. Create datasource (transport and read)
2. Create ListView, and bind to datasource with specified template
3. create var to get recordCount using myDataSource.total()
4. Display recordCount

Let me know if I need to use a different order.

thx.

1 Answer, 1 is accepted

Sort by
0
Accepted
Ryan
Top achievements
Rank 1
answered on 10 Apr 2012, 11:05 PM
There's nothing wrong with what you are doing.  Just know that dataSource.total() won't be populated until it has been fetched.  Putting this item after the ListView bind doesn't mean it will happen in that order.  The transport is happening asynchronously.  The datasource/listview does not currently doesn't expose an event for onAfterDatabound or the like.  Just use my fetch code instead.  Datasource is smart enough not to query the data twice unless it needs to.
Mike
Top achievements
Rank 1
commented on 10 Apr 2012, 11:12 PM

Fantastic!!! That worked perfectly!

Thanks for the help!  
Ryan
Top achievements
Rank 1
commented on 12 Apr 2012, 11:18 AM

I've found that you can also create a function for datasource's change event.  In my use it is a better way to track changes for dynamic data than using a manual fetch.

Fires when data is changed

Example

var dataSource = new kendo.data.DataSource({
    change
: function(e) {
       
// handle event
   
}
});

To set after initialization

dataSource.bind("change", function(e) {
   
// handle event
});
Sanjay
Top achievements
Rank 1
commented on 08 Jan 2015, 09:55 PM

Sorry but this never worked for me
var dataSource2 = new kendo.data.DataSource({
dataType: "json",
serverFiltering: true,
transport: {
read: "http://opendata.cbs.nl/ODataApi/odata/37296ned/TypedDataSet"
},
schema: {
data: function (data) {
return data.value;
}


},
pageSize: 10


});
dataSource2.fetch(function () {
alert(dataSource2.total()); // displays "0" :(
alert(dataSource2.totalPages()); // displays "0" :(
    });
Dimo
Telerik team
commented on 13 Jan 2015, 03:14 PM

Hi Sanjay,

You can make the following test:

1. Go to http://demos.telerik.com/kendo-ui/grid/remote-data-binding

2. Execute the following code in the browser's Javascript console:

var dataSource2 = $("#grid").data("kendoGrid").dataSource;
dataSource2.fetch(function () {
   alert(dataSource2.total());
   alert(dataSource2.totalPages());
});

In your case, are you sure that your schema is correct and there is a valid server response? If you need further assistance, please send a runnable example.

Regards,
Dimo
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
Mike
Top achievements
Rank 1
Answers by
Ryan
Top achievements
Rank 1
Share this question
or