2011-07-03 29 views
0

我JSON數據格式如下:灌裝Dojox.grid.DataGrid用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> 
+0

我不明白這個問題..請清除它。 – 2011-07-03 12:28:41

回答

4

Remeber是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} 
       ]}; 

正如你所看到的,這是現在所需要的屬性的對象。標識符屬性告訴商店使用名爲「id」的屬性來唯一地區分這些項目。您的對象沒有獨特的屬性,所以我在所有項目上添加了id屬性。你可能有一些其他的財產可以使用。

接下來,既然你有你的數據在一個變量,爲什麼你告訴你的ItemFileReadStore從一個名爲data.json的URL獲取數據?相反,請執行:

<span dojoType="dojo.data.ItemFileReadStore" jsId="store1" data="jsonData"></span> 

最後,您的網格本身。表頭需要對應於商店中商品的屬性。你有,例如日期,開放度高,因此使用每個thfield屬性:

<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> 

我補充說:「高度:500像素」表中,但可能不適合你是必要的。

+0

非常感謝。 – Pawan