我有以下格式的JSON数据:
var jsonData = [{date:'August 19, 2004',open:100.01,high:104.06},{date:'August 19, 2004',open:100.01,high:104.06},{date:'August 19, 2004',open:100.01,high:104.06}];如何在Dojox.grid.DataGrid中打印此数据
<body class=" claro ">
<span dojoType="dojo.data.ItemFileReadStore" jsId="store1" url="data.json">
</span>
<table dojoType="dojox.grid.DataGrid" store="store1"
style="width: 100%; height: 100%;">
<thead>
<tr>
<th width="150px" >
Title of Movie
</th>
<th width="150px">
Year
</th>
<th width="150px" >
Producer
</th>
</tr>
</thead>
</table>
</body>发布于 2011-07-03 21:58:57
请记住,ItemFileReadStore (实际上是所有类型的商店)需要特定格式的数据。你告诉我你有一个jsonData变量:
var jsonData = [{date:'August 19, 2004',open:100.01,high:104.06},{date:'August 19, 2004',open:100.01,high:104.06},{date:'August 19, 2004',open:100.01,high:104.06}];这不是ItemFileReadStore想要的格式。ItemFileReadStore需要一个至少有两个属性的对象:标识符和项。因此,将您的数据更改为:
var jsonData = {identifier: "id",
items: [
{id: 1, date:'August 19, 2004',open:100.01,high:104.06},
{id: 2, date:'August 19, 2004',open:100.01,high:104.06},
{id: 3, date:'August 19, 2004',open:100.01,high:104.06}
]};如您所见,它现在是一个具有所需属性的对象。identifier属性告诉商店使用名为"id“的属性来唯一地区分商品。您的对象没有唯一的属性,所以我在所有项上添加了id属性。您可能还有其他一些可以使用的属性。
接下来,既然您的数据在一个变量中,为什么要告诉您的ItemFileReadStore从一个名为data.json的URL获取数据呢?取而代之的是:
<span dojoType="dojo.data.ItemFileReadStore" jsId="store1" data="jsonData"></span>最后,你的网格本身。表头需要与商店中项目的属性相对应。例如,您有date、open和high,因此可以在每个th上使用field属性
<table dojoType="dojox.grid.DataGrid" store="store1"
style="width: 100%; height: 500px;">
<thead>
<tr>
<th width="150px" field="date">Title of Movie</th>
<th width="150px" field="open">Year</th>
<th width="150px" field="high">Producer</th>
</tr>
</thead>
</table>我在表格中添加了"height: 500px“,但这对你来说可能不是必要的。
https://stackoverflow.com/questions/6562818
复制相似问题