从远程odata源读取的数据的属性

从远程odata源读取的数据的属性

本文介绍了Kendo DataSource:如何定义“已计算"从远程odata源读取的数据的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:

  • kendo数据源

  • kendo DataSource

var ordersDataSource = new kendo.data.DataSource({
    type: "odata",
    transport: {
        read: {
            url: "http://localhost/odata.svc/Orders?$expand=OrderDetails"
        }
    },
    schema: {
        type: "json",
        data: function(response){
            return response.value;
        }
        total: function(response){
            return response['odata.count'];
        }
    },
    serverPaging: true,
    serverFiltering: true,
    serverSorting: true
})

  • 从odata源读取的json数据如下:

  • the json data read from the odata source is like:

    {
        odata.metadata: "xxxx",
        odata.count: "5",
        value: [
            {
                OrderId: 1,
                OrderedDate: "2013-02-20",
                OrderInfoA: "Info A",
                OrderInfoB: "Info B"
                OrderDetails: [
                    {
                        OrderDetailId: 6,
                        OrderDetailInfoC: "Info C",
                        OrderDetailInfoD: "Info D"
                    },
                    {
                        //Another OrderDetail's data
                    }
                ]
            },
            {
                // Another Order's data
            }
        ]
    }
    

  • 问题1 :

    1.如果我要定义计算的"属性: OrderedDateRelative ,它应该是从今天(2013-02-25)到创建订单的日期(2013- 02-20),例如:"5天前" ,我如何在客户端实现此目标?

    1.If I wanna define a "computed" property: OrderedDateRelative, which should be the number of days between Today(2013-02-25) and the Day the Order was Created(2013-02-20), Like: "5 days ago", HOW can i achieve this in the client side?

    回答问题1: http://jsbin.com/ojomul/7/edit

    问题2 -更新-

    2.每个订单都有其嵌套属性OrderDetails,因此可以为嵌套OrderDetails属性定义一个计算字段吗?像:每个OrderDetail的 OrderDetailInfoCAndD ,值应该类似于: OrderDetailInfoC + OrderDetailInfoD ,即"Info C Info D"?

    2.Every Order has its Nested Property OrderDetails, so is it possible to define a Calculated Field for the Nested OrderDetails Property? Like: OrderDetailInfoCAndD for each OrderDetail, and the value should be something like: OrderDetailInfoC + OrderDetailInfoD, which is "Info C Info D"?

    谢谢

    教务长

    推荐答案

    您可以通过指定数据源的模型来创建计算字段:

    You can create a calculated field by specifying the model of the data source:

      dataSource = new kendo.data.DataSource({
        data: [
          { first: "John", last: "Doe" },
          { first: "Jane", last: "Doe" }
        ],
        schema: {
          model: {
            // Calculated field
            fullName: function() {
              return this.get("first") + " " + this.get("last");
            }
          }
        }
      });
    

    这是一个实时演示: http://jsbin.com/ojomul/1/edit

    这篇关于Kendo DataSource:如何定义“已计算"从远程odata源读取的数据的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    09-02 01:12