如何在Kendoui获得田野类型?

时间:2015-01-27 14:07:10

标签: kendo-ui kendo-datasource

数据源中是否存在类似dataSource.getFieldType(field)的方法:

var dataSource = new kendo.data.DataSource({
    // somethings here,
    schema : {
         model : {
            post_id : {type: "number" },
            post_title : {type:"string"},
            post_date : {type:"date"}
         }
    }
});

var fieldType = dataSource.getFieldType("post_title"); // it should return string

1 个答案:

答案 0 :(得分:7)

您应该将getFieldType函数定义为:

function getFieldType(dataSource, field) {
    return dataSource.options.schema.model.fields[field].type;
}

使用它将是:

var fieldType = getFieldType(dataSource, "post_title");

或者,您可以通过执行以下操作来扩展KendoUI DataSource以定义名为getFieldType的新方法:

kendo.data.DataSource.prototype.getFieldType = function(field) {
    return this.options.schema.model.fields[field].type;
}

使用它将是:

var fieldType = dataSource.getFieldType("post_title");

使用DataSource扩展程序检查此版本:

$(document).ready(function() {
  kendo.data.DataSource.prototype.getFieldType = function(field) {
    return this.options.schema.model.fields[field].type;
  }

  $("#show").on("click", function() {
    var ds = $("#grid").data("kendoGrid").dataSource;
    alert("Freight: " + ds.getFieldType("Freight"));
  });


  $("#grid").kendoGrid({
    dataSource: {
      type: "odata",
      transport: {
        read: "http://demos.telerik.com/kendo-ui/service/Northwind.svc/Orders"
      },
      schema: {
        model: {
          fields: {
            OrderID: { type: "number" },
            Freight: { type: "number" },
            ShipName: { type: "string" },
            OrderDate: { type: "date" },
            ShipCity: { type: "string" }
          }
        }
      },
      pageSize: 20,
      serverPaging: true,
      serverFiltering: true,
      serverSorting: true
    },
    height: 550,
    pageable: true,
    columns: [
      {
        field:"OrderID",
        filterable: false
      },
      "Freight",
      {
        field: "OrderDate",
        title: "Order Date",
        format: "{0:MM/dd/yyyy}"
      },
      {
        field: "ShipName",
        title: "Ship Name"
      },
      {
        field: "ShipCity",
        title: "Ship City"
      }
    ]
  });
});
html { 
  font-size: 12px; 
  font-family: Arial, Helvetica, sans-serif;
}
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.common.min.css" />
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.default.min.css" />
<script src="http://cdn.kendostatic.com/2014.3.1316/js/jquery.min.js"></script>
<script src="http://cdn.kendostatic.com/2014.3.1316/js/kendo.all.min.js"></script>

<button id="show" class="k-button">Show Freight type</button>
<div id="grid"></div>