Kendo DataSource: How to define "Computed" Properties for data read from remote odata source

Dean picture Dean · Feb 25, 2013 · Viewed 8.1k times · Source

Situation:

  • 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
    })
    
  • 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
            }
        ]
    }
    

Question 1:

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?

Answer to Question1: http://jsbin.com/ojomul/7/edit

Question 2 --UPDATE--

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"?

Thanks,

dean

Answer

Atanas Korchev picture Atanas Korchev · Feb 25, 2013

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");
        }
      }
    }
  });

Here is a live demo: http://jsbin.com/ojomul/1/edit