简体   繁体   中英

Get Kendo Grid filtered datasource count

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({});
        }


    }

You might use fetch but instead of using dataSource.view().length you should use dataSource.total() method.

Something like:

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 () {
            alert(view.dataSource.total()); 
        });
    } else {
        grid.dataSource.filter({});
    }
}

See it in action here: http://jsfiddle.net/OnaBai/f19k0vrt/5/ Type two dates and click "Filter" button and it will apply the filter on Birth Date and show you the total.

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"
},
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM