问题
I am programming in php,
I want to take an array
I have (which is extracted from mysql result set), convert it to JSON
and then use it in dojox.grid.DataGrid
.
I got an idea from this link:
I used the following on the array
(in a file called getJSON.php
)
echo $ajax = "{identifier: 'db_id', 'items':".json_encode($array)."}";
Then I try doing this (in my main page):
var store = new dojo.data.ItemFileWriteStore({ url: 'getJSON.php' });
Everything else is exactly as the Dojo documentation specifies.
The grid shows up, but doesn't load the data and instead writes Sorry, an error occurred
Does anyone know the reason? Hopefully I gave you enough to go on.
回答1:
i don't use ItemFileWriteStore for that ! They changed a lot since Dojo 1.6 , so maybe you looked at something not up to date.
Try this code:
// Load the neccessary components (This is dojo with AMD ) !
require(["dojo/aspect",'dojo/_base/lang', 'dojox/grid/DataGrid' ,'dojo/dom' , 'dojo/store/JsonRest','dojo/data/ObjectStore', 'dojo/domReady!'],
function(aspect,lang, DataGrid, dom,JsonRest,ObjectStore){ // Map components to vars...
var store = new JsonRest({ target: "getJSON.php" // Use a URL that you can open up in a browser. }); /*layout for the grid, you will have to adapt this to your columns !!!*/ var layout = [[ {'name': 'Filename', 'field': 'documentName', 'width': '300px'}, {'name': 'Size', 'field': 'fileSize', 'width': '100px'}, {'name': 'Id', 'field': 'id', 'width': '200px'} ]]; dataStore=ObjectStore({objectStore: store}); // Transform to Objectstore ! /*Now we create a new grid*/ var grid = new DataGrid({ id: 'grid', store:dataStore, // Connect the store autoWidth:false, structure: layout, // Connect the layout rowSelector: '0px'}); grid.placeAt("yourTargetDivId"); // Has to be an existing DOM Element with id ! grid.startup(); // START IT !
});
Please try this code by echoing something simple like this first:
echo '[{"id":"1","fileSize":"100kb","documentName":"Lucian !"}, {"id":"2","fileSize":"900kb","documentName":"Pew Pew !"}]';
And after that with your own JSON...
来源:https://stackoverflow.com/questions/14803818/loading-encoded-json-into-dojo-grid