javascript - 使用 dojo.xhrget() 方法在 Dojogrid 中显示数据

标签 javascript html ajax dojo dojox.grid.datagrid

我想显示一组数据,我使用 dojo.xhrget() 方法通过对服务器的异步调用检索这些数据。我正在通过 URL 检索数据,我希望每次用户在内容 Pane 中单击时,网格中都会显示一组新值,而无需刷新整个页面。问题是数据没有显示在网格中。我通过 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">

我不明白是什么问题。你能帮我弄清楚为什么我会收到错误警报吗?谢谢。

最佳答案

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 属性。请参阅:Updating Dojo Dijit FilteringSelect's store with asynchonous data了解 clearOnClose

关于javascript - 使用 dojo.xhrget() 方法在 Dojogrid 中显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9768294/

相关文章:

javascript - 在 php 的动态下拉列表中使用 ajax 时未找到

javascript - 在不支持的 api 调用上抛出错误

html - CSS 表格图片有空格

php reCaptcha提交后不会返回确认

javascript - 使用 javascript 参数隐藏和显示 DIV

php - Ajax 从 mySQL 数据库中检索数据并创建表

javascript - 如何在 javascript 中进行漂亮而简单的文件上传?

javascript - xhr.getAllResponseHeader 返回源代码

javascript - 如何在免费的 jqgrid 中用 remapColumnsByName 替换 remapColums

javascript - 每次按下按钮时都会重新创建相同的动画。 Angular.js