首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用JSON数据填充Dojox.grid.DataGrid

用JSON数据填充Dojox.grid.DataGrid
EN

Stack Overflow用户
提问于 2011-07-03 20:20:56
回答 1查看 4.8K关注 0票数 0

我有以下格式的JSON数据:

代码语言:javascript
复制
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中打印此数据

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-03 21:58:57

请记住,ItemFileReadStore (实际上是所有类型的商店)需要特定格式的数据。你告诉我你有一个jsonData变量:

代码语言:javascript
复制
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需要一个至少有两个属性的对象:标识符和项。因此,将您的数据更改为:

代码语言:javascript
复制
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获取数据呢?取而代之的是:

代码语言:javascript
复制
<span dojoType="dojo.data.ItemFileReadStore" jsId="store1" data="jsonData"></span>

最后,你的网格本身。表头需要与商店中项目的属性相对应。例如,您有date、open和high,因此可以在每个th上使用field属性

代码语言:javascript
复制
<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“,但这对你来说可能不是必要的。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6562818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档