【问题标题】:Loading encoded JSON into Dojo Grid将编码的 JSON 加载到 Dojo Grid 中
【发布时间】:2013-02-10 23:21:37
【问题描述】:

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

我从this link:得到了一个想法

我在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

有人知道原因吗?希望我给了你足够的支持。

【问题讨论】:

  • {identifier: 是错误的 JSON。试试{"identifier" 如果你有这样的工具,你也可以尝试验证完整的 JSON:jsonlint.com
  • true,有效的 JSON 还需要独占使用双引号 (")...但我想这可能不是这里的问题..
  • 在我看来好像……。您创建网格的方式可能有问题。您是否提供了结构定义?也许一些代码可能会有所帮助..(尤其是您创建网格的部分 Grid)
  • 网格的创建取自附加的链接
  • 只是为了论证..this: echo $ajax = '{\"identifier\": \"db_id\", \"items\":'.json_encode($result)。 '}';也不行

标签: php mysql json dojo


【解决方案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":"Pew Pew !"}]';

然后使用您自己的 JSON...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-13
    • 2019-02-23
    • 1970-01-01
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-11
    相关资源
    最近更新 更多