【问题标题】:KendoUI: one multi-level JSON dataSource for multiple gridsKendoUI:一个用于多个网格的多级 JSON 数据源
【发布时间】:2014-03-10 20:19:28
【问题描述】:

我有一个如下所示的 JSON 数据源:

var productDataSource = new kendo.data.DataSource({
    transport: {
        read: {
            url: 'http://...',
            dataType: "json"
        }
    },
    pageSize: 10
});

并返回如下内容:

{
   "ProdSet1":[
      {
         "Name": "Product 1-1",
         "Price": 20,
         "Quantity": 50,
         "Change": 4
      },
      {
         "Name": "Product 1-2",
         "Price": 14,
         "Quantity": 74,
         "Change": 5
      }
   ],
   "ProdSet2":[
      {
         "Name": "Product 2-1",
         "Price": 15,
         "Quantity": 12,
         "Change": 2
      }
   ]
}

然后我有多个网格使用这个数据源:

$("#prodSet1").kendoGrid({
    dataSource: productDataSource,
    columns: [
        { field: "ProdSet1[0].Name", title: "Name" },
        { field: "ProdSet1[0].Price", title: "Price" },
        { field: "ProdSet1[0].Quantity", title: "Quantity" },
        { field: "ProdSet1[0].Change", title: "Change" }
    ]
});

$("#prodSet2").kendoGrid({
    dataSource: productDataSource,
    columns: [
        { field: "ProdSet2[0].Name", title: "Name" },
        { field: "ProdSet2[0].Price", title: "Price" },
        { field: "ProdSet2[0].Quantity", title: "Quantity" },
        { field: "ProdSet2[0].Change", title: "Change" }
    ]
});

但是{ field: "ProdSet1[0].Name" ...} 不起作用。

如何参考正确的产品数据?

【问题讨论】:

    标签: javascript json kendo-ui kendo-grid kendo-datasource


    【解决方案1】:

    由于集合在返回对象中命名,您可以将 schema.data 属性设置为每个 ProdSet,并将网格绑定到它。

    我会使用 datasource.read() 手动从数据源中获取数据

    var datafromService = productDataSource.read();
    

    文档...http://docs.telerik.com/kendo-ui/documentation/api/framework/datasource#methods-read

    然后将每个网格绑定到该 datafromService,每个网格都指定要绑定到的 JSON 对象内的集合。

    $("#prodSet1").kendoGrid({
      dataSource: {
        data: datafromService,
        schema: {
          data: 'ProdSet1' 
        }
      },
      columns: [
        { field: "Name", title: "Name" },
        { field: "Price", title: "Price" },
        { field: "Quantity", title: "Quantity" },
        { field: "Change", title: "Change" }
      ]
    });
    

    和

    $("#prodSet2").kendoGrid({
      dataSource: {
        data: datafromService,
        schema: {
          data: 'ProdSet2' 
        }
      },
      columns: [
        { field: "Name", title: "Name" },
        { field: "Price", title: "Price" },
        { field: "Quantity", title: "Quantity" },
        { field: "Change", title: "Change" }
      ]
    });
    

    现在它们将绑定到同一组数据,只是显示来自 JSON 数据的不同集合。

    查看示例...http://jsbin.com/dokub/1/edit

    如果您需要完整的 CRUD 操作,那就进入另一袋猫。

    【讨论】:

    • 是的。完全有道理。谢谢。
    • 嘿。我无法让这个工作。一旦我创建了一个真正的 Kendo 数据源,它就会以“无法将切片方法应用于未定义”而中断。它可能与 json 数据结构(对象与数组)有关?更新jsbin:jsbin.com/munoz/4/edit
    • 是的,您不能将数据源的数据属性设置为剑道数据源。尝试创建一个像 jsbin.com/dokub/2/edit 这样的数据源,然后调用 dataSource.read() 应该会返回您的 JSON 对象。
    • 我搞定了。谢谢。我不得不做架构:{数据:函数(数据){返回[数据]; } }。哈克,但有效。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2011-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-01
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多