2012-03-19 10 views
1

我想顯示一組數據,我使用dojo.xhrget()方法通過對服務器的異步調用來檢索數據。我通過URL檢索數據,我希望每次用戶單擊內容窗格時,都會在網格中顯示一組新的值,而無需刷新整個頁面。問題是數據沒有在網格中顯示。我正在通過xhrget()錯誤方法收到錯誤。使用dojo.xhrget()方法在Dojogrid中顯示數據

我的腳本的代碼::

<script> 
function populateGrid(){ 
    dojo.xhrGet({ 
     url: "http://localhost:8080/2_8_2012/jsp/GetJson.jsp", 
     load: fillGrid, 
     error:handleError, 
     preventCache:true 
     }); 
} 

function fillGrid(data, ioArgs) 
{ 
    alert(data); 
     var newData = { 
      identifier: "ID", 
      items: data 
    }; 

    var dataStore = new dojo.data.ItemFileReadStore({data: newData, id:"dataStoreId"}); 
    var gridStructure =[[ 

          { field: "ID", 
           name: "ID_Emp", 
           width: "20%", 
           classes:"firstName" 
          }, 
          { 
           field: "Names", 
           name: "Name", 
           width: "20%", 
           classes: "firstName" 
          }, 
          { field: "Email", 
           name: "Mail", 
           width: "20%", 
           classes:"firstName" 
          } 

        ] 
       ]; 

    var grid = dijit.byId("grid.DataGrid");  
    grid.setStore(dataStore); 
    grid.startup(); 
} 

function handleError() { 
    alert("An error occurred while invoking the service."); 
} 
</script> 

現在,這裏警報(數據)和http://localhost:8080/2_8_2012/jsp/GetJson.jsp的輸出是相同的,即::

[{"ID":1,"Names":"Shantanu","Email":"[email protected]"},{"ID":2,"Names":"Mayur","Email":"[email protected]"},{"ID":26,"Names":"Rohit"}] 

我xhr.get功能工作在數據檢索方面很好。即當我更新數據庫中的值。我確實得到了具有該更新值的警報(數據)輸出,而無需再次刷新整個頁面。但數據不顯示在數據網格中。

我收到警報

An error occurred while invoking the service. 

http://localhost:8080/2_8_2012/jsp/GetJson.jsp的代碼::

<%@ page language="java" contentType="application/json; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
    <%@ page import="MyPackage.PopulateTextbox" %> 
<% 
String temp1; 
PopulateTextbox obj = new PopulateTextbox(); 
temp1 = obj.method(); 
%> 
<%=temp1 %> 

的標記代碼::

<div id="grid.DataGrid" data-dojo-type="dojox.grid.DataGrid" title="Simple Grid" data-dojo-props= "autoWidth:true, structure: gridStructure" style="width:900px; height:200px;"></div> 

<div id="contentpaneid" dojoType="dijit.layout.ContentPane" title="Pending Activities" style="background-image: url('http://localhost:8080/2_8_2012/images/17.png');" onclick="populateGrid"> 

我沒有得到什麼問題。你能幫我解答爲什麼我會收到錯誤提示。謝謝。

+0

我試圖顯示什麼是錯誤..和我GT這個:: 404網格是未定義的。 – 2012-03-19 10:53:33

+2

我從你最近的一個問題中得到了這個問題,從你把它鏈接到哪裏。在閱讀完您的問題後,我想,您正試圖在數據網格實際檢索之前將數據綁定到您的數據網格。所以,在不知不覺中你將你的網格綁定到null/undefined對象,這可能會產生你看到的錯誤。不確定,但你只需確認一次,如果是這樣的話。 – MrClan 2012-03-19 12:42:11

+0

謝謝Pratik .. :) – 2012-03-20 06:25:30

回答

2

Pratik Chandra正確地提到了這個問題 - 數據網格正在填充而沒有設置任何存儲。我建議改變你的DataGrid中以編程方式填充

所以你的宣言:

<div id="grid.DataGrid" data-dojo-type="dojox.grid.DataGrid" 

neeeds被改爲:

<div id="mygrid" ></div> 

,然後更改行:

var grid = dijit.byId("grid.DataGrid"); 

到:

var grid = new dojox.grid.DataGrid({ 
       id: "grid", 
       jsid: "grid", 
       store: dataStore, 
       structure: gridStructure, 
       style: 'width:900px;height:300px;' 
      }, dojo.byId("mygrid")); 
grid.startup(); 

另請注意,無論何時要刷新網格中的數據,都不需要重新填充網格,只需使用新值更新數據存儲,並且數據網格將自動使用新數據進行刷新:-) Dojo照顧那部分。

要清理數據存儲中的現有數據並使用新數據填充數據,請在IFRS上設置clearOnClose屬性。請參閱:Updating Dojo Dijit FilteringSelect's store with asynchonous data瞭解clearOnClose

+0

感謝Vijay的代碼。我已經嘗試過了,我正在獲取網格中的數據。但是,當我點擊第二次,我沒有得到在網格中更新的數據。警報(數據)正在更新。但是網格中的數據沒有得到更新。我必須使用fetch()相關的語句嗎?如果可以的話,請指出確切的位置,以便在何處放置提取語句。 ?謝謝 – 2012-03-20 06:24:33

+0

這裏是我的Itemfilereadstore語句。我對代碼所做的其他更改與你告訴我的一樣。var dataStore = new dojo.data.ItemFileReadStore({data:newData,id:「dataStoreId」,clearOnClose:true}); – 2012-03-20 06:27:27

+0

所以你的onclick函數應該是這個樣子: 功能onCpaneClick(){VAR 推遲= dojo.xhrGet( { 網址: 「」, handleAs: 「JSON」, 負載:功能(數據){ dataStore.data = newdata; dataStore.close(); grid.refresh();} , 錯誤:功能(錯誤){// 手柄錯誤 }); – 2012-03-20 14:02:55