且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将编码的JSON加载到Dojo Grid中

更新时间:2022-10-17 23:19:37

我不使用ItemFileWriteStore!他们改变了很多Dojo 1.6,所以也许你看一些不是最新的东西。



尝试这段代码:


//加载必需的组件(这是与AMD的dojo)!



require([dojo / aspect,dojo / _base / lang','dojox / grid / DataGrid'
,'dojo / dom','dojo / store / JsonRest','dojo / data / ObjectStore',
'dojo / domReady! ],



函数(aspect,lang,DataGrid,dom,JsonRest,ObjectStore){//将组件映射到vars ...

  var store = new JsonRest({
target:getJSON.php//使用可以在浏览器中打开的URL
});


/ *网格布局,你将不得不适应你的列!!! * /
var layout = [[
{'name':'Filename','field':'documentName','width':'300px'},
{'name':'Size','field':'fileSize' :'100px'},
{'name':' Id','field':'id','width':'200px'}
]];

dataStore = ObjectStore({objectStore:store}); //转换为Objectstore!

/ *现在我们创建一个新的网格* /
var grid = new DataGrid({
id:'grid',
store:dataStore,// Connect商店
autoWidth:false,
结构:布局,//连接布局
rowSelector:'0px'});


grid.placeAt(yourTargetDivId); //必须是id的现有DOM元素!
grid.startup(); //开始吧

});


请尝试这个代码,首先回应一些简单的东西:


echo'[{id:1 ,fileSize:100kb,documentName:Lucian!},
{id:2,fileSize:900kb,documentName:Pew Pew!} ]';


之后用你自己的JSON ...


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.

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