【问题标题】:Kendo DataSource: How to set filters before fetch without sending two httprequestsKendo DataSource:如何在不发送两个 httprequest 的情况下在 fetch 之前设置过滤器
【发布时间】:2013-03-16 03:22:37
【问题描述】:

环境:

  • 剑道版本:2013.1.319
  • 数据源:

    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
    });
    
  • 获取数据:

    productsDataSource.filter([{ field: "Id", operator: "eq", value: 5 }]); //这将发送一个http请求

    productsDataSource.fetch(函数 (e) { tempDataStorage = e.items; //处理数据的更多逻辑; });

  • 问题:

    1. 需要使用dataSource的fetch方法进行数据处理(widget初始化、数据绑定...等);
    2. 在 fetch 之前设置过滤器时避免发送两个 httprequest;
    3. 需要在运行时更改过滤条件。

【问题讨论】:

  • 你有想过这个吗?
  • 我认为不建议更改 _filter。 Telerik 的团队应该提供一种更好的方法来在绑定操作之前操作过滤器数组。 filter 方法会导致第二次服务器操作,这根本不好。目前,更改 _filter 似乎是唯一的解决方案,但是请注意,他们将来可以更改此变量的名称,然后您的应用程序可能会中断。

标签: kendo-ui datasource odata


【解决方案1】:
productsDataSource._filter = { logic: 'and', filters: [
{ field: "Id", operator: "eq", value: 5 }]};

我发现这行得通。将内部属性设置为完整的过滤器对象。然后您可以在之后调用 fetch 。但是,我还没有找到一种在不触发提取的情况下更改页面大小的方法。

【讨论】:

    【解决方案2】:

    您可以在DataSource 配置中使用filter。这应该只发出一个具有您在DataSource 配置中指定的过滤条件的请求。

    【讨论】:

    • 嗨,问题是我必须在运行时更改过滤条件。配置只能设置一次,对吗?
    • 您在新条件下使用datasource.read`` whenever you want and as soon as you set a new filter condition Kendo UI invokes transport.read`设置过滤器配置。
    • dataSource中唯一有回调函数的方法是fetch,所以我选择它。调用 dataSource.read() 后 dataSource.data() 仍然为空,这成为一个问题,因为在那之后我无法立即初始化我的小部件。无论如何我可以在调用 fetch 方法之前重置过滤器并且不自动发送 httprequest(在 fetch 之前)?谢谢。
    • 如何/在哪里使用数据源? Grid 的小部件具有在初始化时不从数据源加载的选项(如 autoBind
    • 像 DropDownList 和“源绑定 + kendo-template”这样的小部件。 Widgets 是可以的,因为不需要改变过滤条件;对于“source binding + kendo-template”,我必须在 fetch 回调中更新 source 属性(在 viewModel 中)。
    【解决方案3】:

    使用productsDataSource._filter = [{ field: "Id", operator: "eq", value: 5 }];在dataSource中设置_filter字段,然后在准备好后使用productsDataSource.read();手动发起远程数据请求

    【讨论】:

      【解决方案4】:

      即使这是一个老问题,它也会出现在谷歌搜索结果中。所以虽然不知道对剑道版本是否有效:2013.1.319,但是目前有一个方法

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

      这可以在一次调用中设置多个选项,如排序、过滤分页等,并返回一个承诺。

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

      【讨论】:

        【解决方案5】:

        将事件监听器绑定到初始化小部件的数据源,然后使用过滤方法。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-09
          • 1970-01-01
          • 2013-05-11
          • 2017-12-24
          相关资源
          最近更新 更多