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

前端 未结 3 1029
自闭症患者
自闭症患者 2021-01-15 06:11

Situation:

  • kendo DataSource

    var ordersDataSource = new kendo.data.DataSource({
        type: \"odata\",
        transport: {
            read: {
                    
    
    
            
相关标签:
3条回答
  • 2021-01-15 06:33

    Below an example to use it in a grid. It can then also sort the column.

    $("#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"
            }
        ]
    });
    
    0 讨论(0)
  • 2021-01-15 06:41

    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

    0 讨论(0)
  • 2021-01-15 06:53

    Here is a way to use calculated field in 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" }]
    });
    
    0 讨论(0)
提交回复
热议问题