10

Environment:

  • kendo version: 2013.1.319
  • dataSource:

    productsDataSource = new kendo.data.DataSource({
        type: "odata",
        transport: {
            read: "http://www.mydomain.com/odata.svc/products",
            dataType: "json",
            contentType: "application/json"
        }
        schema: {
            type: "json",
            data: function(data){
                return data.value;
            },
            total: function(data){
                return data['odata.count'];
            },
            model: product
        },
        pageSize: 50,
        serverPaging: true,
        serverFiltering: true,
        serverSorting: true
    });
    
  • Get data:

    productsDataSource.filter([{ field: "Id", operator: "eq", value: 5 }]); //this will send an httprequest

    productsDataSource.fetch(function (e) { tempDataStorage = e.items; //more logic to dealing with the data; });

  • problems:

    1. need to use the fetch method of the dataSource for data processing(widgets initialization, data binding...etc);
    2. avoid sending two httprequests when setting filters before fetch;
    3. the filter condition need to be changed at runtime.
Dean
  • 1,281
  • 3
  • 15
  • 23
  • I think change the _filter is not recommended. The telerik´s team should provide a better way to manipulate the filters array before the bind operation. the filter method causes a second server operation, that´s not good at all. for now, changing the _filter seems to be the unique solution, but, pay attention, they can change the name of this variable in the future and you application can break then. – André Sobreiro Apr 17 '14 at 16:32

5 Answers5

5
productsDataSource._filter = { logic: 'and', filters: [
{ field: "Id", operator: "eq", value: 5 }]};

I've found this to work. Set the internal property to a full filter object. You can then call fetch afterwards. I've not yet found a way to change the page size without triggering a fetch however.

Daniel Revell
  • 8,338
  • 14
  • 57
  • 95
4

You can user filter in the DataSource configuration. This should issue only one request with the filtering conditions that you specify in the DataSource configuration.

OnaBai
  • 40,767
  • 6
  • 96
  • 125
  • Hi, the problem is that I have to change filter conditions at runtime. configuration can only be set one time, am i right? – Dean Mar 26 '13 at 11:21
  • You set filter configuration using `datasource.read`` whenever you want and as soon as you set a new filter condition Kendo UI invokes `transport.read` with the new conditions. – OnaBai Mar 26 '13 at 12:02
  • The only method in dataSource that has a callback function is fetch, that's why I choose it. the dataSource.data() is still empty after dataSource.read() is called, this becomes a problem because i cannot initialize my widgets right after that. is there anyway that i can reset the filters before i call the fetch method and do not send a httprequest automatically(before fetch)? Thanks. – Dean Mar 26 '13 at 13:22
  • How / Where do you use the datasource? Widget as `Grid` have options (as `autoBind`) for not loading from the datasource when initialized – OnaBai Mar 26 '13 at 13:41
  • Widgets like DropDownList and "source binding + kendo-template". Widgets is OK because there no need to change filter conditions; for "**source** binding + kendo-template", I have to update the **source** property(in a viewModel) in the fetch callback. – Dean Mar 26 '13 at 13:50
  • Sorry! still not getting what you are trying to do. If you need to do some processing to received data just implement the logic in `schema.parse` or `schema.data`. If you need to change what this function does... just do it: implement some sort of logic that changes what it does... Check [this](http://jsfiddle.net/OnaBai/VJhEd/1/) example where I change parse processing and then apply a filter condition. – OnaBai Mar 26 '13 at 16:17
3

Set the _filter field in the dataSource using productsDataSource._filter = [{ field: "Id", operator: "eq", value: 5 }]; and then manually initiate the request for remote data when you are ready using productsDataSource.read();

johnktims
  • 581
  • 2
  • 7
  • 14
1

Even though it is an old question, it comes in google results. So even though I don't know if it is valid for kendo version: 2013.1.319, but there is currently a method

dataSource.query({
  sort: { field: "ProductName", dir: "desc" },
  page: 3,
  pageSize: 20
}); 

This can set multiple options like sort, filter paging etc in a single call and returns a promise.

http://docs.telerik.com/kendo-ui/api/javascript/data/datasource#methods-query

Jose Tuttu
  • 418
  • 5
  • 15
0

Bind event listener to datasource which initializes widget and then use filter method.

datasource.one('requestEnd', function(){
   // initialize or/and bind widget
});
datasource.filter({ /*your filter*/ })
KubaKubikula
  • 370
  • 2
  • 14