views:

29

answers:

1

I'd like to hava a Dojo dojox.grid.DataGrid with its data from a servlet.

Problem: The data returned from the servlet does not get displayed, just the message "Sorry, an error has occured".

If I just place the JSON string into the HTML, it works. ARRRRGGH.

Can anyone please help me!

Thanks Jeff Porter

Servlet code...

public void doGet(HttpServletRequest req, HttpServletResponse resp) {
    res.setContentType("json");
    PrintWriter pw = new PrintWriter(res.getOutputStream());
    if (response != null) pw.println("[{'batchId':'2001','batchRef':'146'}]");
    pw.close();
}

HtmL code...

    <div id="gridDD" dojoType="dojox.grid.DataGrid" 
    jsId="gridDD" style="height: 600x; width: 100%;" 
    store="ddInfo" structure="layoutHtmlTableDDDeltaSets">
    </div>      

    var rawdataDDInfo = ""; // empty at start
    ddInfo = new dojo.data.ItemFileWriteStore({
        data: {
            identifier: 'batchId',
            label: 'batchId',
            items: rawdataDDInfo
        }
    });
    <script>
    function doSelectBatchsAfterDate() {
    var xhrArgs = {
        url: "../secure/jsonServlet",
        handleAs: "json",
        preventCache: true,
        load: function(data) {
        var xx =dojo.toJson(data);
            var ddInfoX = new dojo.data.ItemFileWriteStore({data: xx});
            dijit.byId('gridDD').setStore(ddInfoX);
        },
        error: function(error) {
                alert("error:" + error);
        }
    }

    //Call the asynchronous xhrGet
    var deferred = dojo.xhrGet(xhrArgs);
    }
    </script>
<img src="go.gif" onclick="doSelectBatchsAfterDate();"/>
A: 

When you create the dojo.data.ItemFileWriteStore using the JSON data returned from server. You just provide the items, you still needs to specify the metadata. The correct code should be as below.

var ddInfoX = new dojo.data.ItemFileWriteStore({
    data: {
        identifier: 'batchId',
        label: 'batchId',
        items: xx
    }
});

And you don't need the dojo.toJson function which converts the JSON object to a JSON string. The dojo.data.ItemFileWriteStore requires a JSON object as the parameter, not a JSON string.

Alex Cheng
see this answerhttp://stackoverflow.com/questions/2874611/itemfilewritestore-how-to-change-the-data
jeff porter