0

我正在用 php 编程,我想取一个array我拥有的(从 mysql 结果集中提取),将其转换为JSON然后在dojox.grid.DataGrid.

我从这个链接得到了一个想法:

我在array(在一个名为的文件中getJSON.php)使用了以下内容

echo $ajax = "{identifier: 'db_id', 'items':".json_encode($array)."}";

然后我尝试这样做(在我的主页中):

var store = new dojo.data.ItemFileWriteStore({ url: 'getJSON.php' });

其他一切都与 Dojo 文档指定的完全相同。网格显示,但不加载数据,而是写入Sorry, an error occurred

有谁知道原因?希望我给了你足够的继续。

4

1 回答 1

1

我不为此使用 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!'],

function(aspect,lang, DataGrid, dom,JsonRest,ObjectStore){ // 将组件映射到 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 !

});

请先通过回显类似这样的简单内容来尝试此代码:

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

然后用你自己的 JSON ......

于 2013-02-11T17:51:18.993 回答