1

我想显示一组数据,我通过使用 dojo.xhrget() 方法对服务器进行异步调用来检索这些数据。我正在通过 URL 检索数据,并且我希望每次用户在内容窗格中单击时,都会在网格中显示一组新值,而无需刷新整个页面。问题是数据没有显示在网格中。我通过 xhrget() 错误方法收到错误。

我的脚本的代码是::

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

现在,这里 alert(data) 和http://localhost:8080/2_8_2012/jsp/GetJson.jsp的输出是相同的,即 ::

[{"ID":1,"Names":"Shantanu","Email":"shantanu.tomar@gmail.com"},{"ID":2,"Names":"Mayur","Email":"mayur.sharma@gmail.com"},{"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">

我没有得到什么问题。你能帮我弄清楚为什么我会收到错误警报吗?谢谢。

4

1 回答 1

2

Pratik Chandra 正确地提到了这个问题——数据网格被填充而没有设置任何存储。我建议将您的数据网格更改为以编程方式填充

所以你的声明:

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

需要改为:

<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 属性。请参阅:使用异步数据更新 Dojo Dijit FilteringSelect 的存储以了解 clearOnClose

于 2012-03-19T14:12:59.800 回答