Kendo DataSource: How to set filters before fetch without sending two httprequests

本秂侑毒 提交于 2020-01-03 07:23:21

问题


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.

回答1:


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.




回答2:


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.




回答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();




回答4:


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




回答5:


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*/ })


来源:https://stackoverflow.com/questions/15635245/kendo-datasource-how-to-set-filters-before-fetch-without-sending-two-httpreques

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!