Get Kendo Grid filtered datasource count

chamara picture chamara · Aug 28, 2014 · Viewed 7.7k times · Source

I'm using tool bar filter Dropdown in kendo grid. When user selects the dropdown I need to get the count of filtered records. Below code is not working for me

function ExamDateChange() { // function on dropdown change
        var value = this.value(),
             grid = $("#grid").data("kendoGrid");

        if (value) {
            grid.dataSource.filter({ field: "ExamID", operator: "eq", value: value });

            grid.dataSource.fetch(function () {
                var view = dataSource.view();
                alert(view.length); 

            });

        } else {

            grid.dataSource.filter({});
        }


    }

Answer

chris picture chris · Aug 28, 2014

Okay here we go,
i tried also the chage event from the dropdown but it doesnt work as you said. The event is called before the dataSource of the Grid is set.

So I think we need the callback when Grids dataSource is bound, so thaht the dataBound event from the Grid.

...
dataBound: function(){
    console.log("Grid data bound");   
    // this should do the trick    
    alert(grid.data("kendoGrid").dataSource.data().length);
},
...

Here is a rudimental fiddle of that.
I hope this is what you need.

Update:
In the case you use serverpaging, you could use the requestEnd event from the datasource.
You have to look up the server response. In the fiddle you got a "__count" property.
Updated fiddle

...
requestEnd: function (e) {
    var response = e.response;
    var type = e.type;
    alert(response.d.__count); // displays "77"
},
...