我们可以在kendo ui中动态定义Schema Model

Jon*_*han 2 jquery kendo-ui kendo-grid

我正在使用KendoUi控件.我已经定义了dataSource之类的

  var dataSource = new kendo.data.DataSource({
   schema: {
    model: {
       id: "ProductID",
         fields: {
            ProductID: { type:"id" },
            ProductName: {type:"string"}
           }      
         }
       }          
     });
Run Code Online (Sandbox Code Playgroud)

现在我的问题是我们可以定义filedsarray类似的

 var arry = [{ProductID:{type:"id"}}, {ProductName:{type:"string"}}];
Run Code Online (Sandbox Code Playgroud)

现在我们可以定义dataSource了

 var dataSource = new kendo.data.DataSource({
   schema: {
    model: {
       id: "ProductID",
         fields: arry
         }
       }          
     });
Run Code Online (Sandbox Code Playgroud)

Ona*_*Bai 6

两种定义都不相同.

第一个:

fields: { 
    ProductID: { type:"id" },
    ProductName: {type:"string"}
}      
Run Code Online (Sandbox Code Playgroud)

使用关联数组(索引是ProductIDProductName),而第二个:

var arry = [{ProductID:{type:"id"}}, {ProductName:{type:"string"}}];
...
fields: arry
...
Run Code Online (Sandbox Code Playgroud)

您使用索引0和数组1.

您可以动态定义它们,但您应该定义arry为:

var arry = { ProductID:{type:"id"}, ProductName:{type:"string"} };
Run Code Online (Sandbox Code Playgroud)

要么

var arry = {};
arry.ProductID = { type: "id" };
arry.ProductName = { type : "string" };
Run Code Online (Sandbox Code Playgroud)

要么

var arry = {};
arry["ProductID"] = { type: "id" };
arry["ProductName"] = { type : "string" };
Run Code Online (Sandbox Code Playgroud)

arr需要是一个object而不是一个array