剑道数据源:如何定义“计算"从远程 odata 源读取的数据的属性 [英] Kendo DataSource: How to define "Computed" Properties for data read from remote odata source

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

问题描述

情况:

  • 剑道数据源

  • 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属性定义一个计算字段?比如:OrderDetailInfoCAndD,对于每个OrderDetail,其值应该是: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"?

    谢谢,

    院长

    推荐答案

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

    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

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

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆