2013-02-25 86 views
3

我想创建一个具有动态列的kendo网格,所有列都将在客户端创建。在Kendo Grid中创建动态列(Javascript)

为例:

我已经写下面的代码来创建一个网格:

columns: [{ title: 'One', width: '100px' }, { title: 'Two', width: '100px' }, {title: 'Three', width:'100px'}], 

以上:

var grid = $("#grid"); 
     grid.children().remove(); 
     grid.kendoGrid({ 
      columns: [{ title: 'One', width: '100px' }, { title: 'Two', width: '100px' }, {title: 'Three', width:'100px'}], 
      dataSource: { 
       transport: { 
        read: { 
         url: "@Url.Action("")", 
         type: "GET", 
         dataType: "json", 
         traditional: true, 
         data: { 
          itemTypeId: $("#").val(), 
          where: ["", "", "", "", ""], 
          orderBy: ["", "", ""], 
         }, 
        }, 
       }, 
       schema: { 
        data: "", 
        total: "", 
       }, 
       serverPaging: true, 
       pageSize: 4, 
       error: function (e) { 
        alert(e.errors); 
       } 
      }, 
      pageable: true, 
      resizable: true, 
      reorderable: true, 
     }) 
    } 

当我通过定义列代码工作正常。

但我想创建所有这些列在一个不工作的循环。

like: 我在JavaScript变量中保存模式,然后将其分配给剑道网格。

Var columnSchema = "{ title: 'One', width: '100px' },{ title: 'Two', width: '100px' },{ title: 'Two', width: '100px' }"; 

columns : [columnSchema] 

但它不工作。

回答

3

一个细微的变化对你的代码,它应该工作:

var columnSchema = [{ title: 'One', width: '100px' },{ title: 'Two', width: '100px' },{ title: 'Three', width: '100px' }]; 

grid.kendoGrid({ 
    // .. other properties .. 
    columns : columnSchema 
}); 

您需要定义您的columnSchema变量作为数组,而不是一个字符串(如我的示例所示),它会工作。

你也可以建立数组,例如

var columnSchema = []; 
columnSchema.push({ title: 'One', width: '100px' }); 
columnSchema.push({ title: 'Two', width: '100px' }); 
columnSchema.push({ title: 'Three', width: '100px' }); 

你可以使用push(..)一个循环,如果需要从别的地方读取信息栏里面。

只要在使用变量初始化网格之前完成此操作即可。网格创建完成后,您无法更改列。

如果您在创建网格后需要更改列,则需要先调用destroy()方法,然后再使用新的列规范调用kendoGrid(..)