2

情况:

  • 剑道数据源

    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 数据如下:

    {
        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.如果我想定义一个“计算”属性:OrderedDateRelative,它应该是今天(2013-02-25)和订单创建日(2013-02-20)之间的天数,例如:“5天以前”,我如何在客户端实现这一点?

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

问题2——更新——

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

谢谢,

院长

4

3 回答 3

7

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

  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

于 2013-02-25T15:20:00.693 回答
2

这是在 Kendo Grid 中使用计算字段的一种方法。

var crudServiceBaseUrl = "http://demos.telerik.com/kendo-ui/service",
dataSource = new kendo.data.DataSource({
    transport: {
        read: {
            url: crudServiceBaseUrl + "/Products",
            dataType: "jsonp"
        }
    },
    pageSize: 20,
    schema: {
        model: {
            total: function (item) {
                return this.UnitPrice * this.UnitsInStock;
            }
        }
    }
});

$("#grid").kendoGrid({
    dataSource: dataSource,
    pageable: true,
    height: 550,
    sortable: true,
    filterable: true,
    toolbar: ["create"],
    columns: [
        { field: "UnitPrice", title: "Unit Price"},
        { field: "UnitsInStock", title: "Units In Stock", width: "120px" },
        { field: "total()", title: "Total" }]
});
于 2014-08-27T19:23:20.683 回答
1

下面是一个在网格中使用它的示例。然后它还可以对列进行排序。

$("#grid").kendoGrid({ 
    dataSource: {
        data: [
            { first: "John", last: "Doe" }, 
            { first: "Jane", last: "Doe" }
        ],
        schema: {
          model: {
            // Calculated field
            fullName: function() {
              return this.first + " " + this.last;
            },
            fields: {
               first: { type: "string" },
               last: { type: "string" }
            }
          }
        }
    },
    columns: [
        {
            // Trigger function of the Calculated field
            field: "fullName()",
            title: "Fullname"
        },
        {
            field: "first",
            title: "firstname"
        }
    ]
});
于 2016-01-19T12:43:54.463 回答