将编码的JSON加载到Dojo Grid中 [英] Loading encoded JSON into Dojo Grid

查看:160
本文介绍了将编码的JSON加载到Dojo Grid中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在PHP编程,
我想要一个数组我有(从mysql结果集中提取),将其转换为 JSON 然后在 dojox.grid.DataGrid 中使用它。



我从此链接获得了一个想法:



我在数组中使用了以下内容(在名为的文件中,getJSON.php

  echo $ ajax ={identifier:'db_id','items':json_encode ) }。 

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

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

其他所有内容与Dojo文档指定的完全一样。
网格显示,但不加载数据,而是写入对不起,发生错误



<有人知道原因吗?希望我给你足够的继续。

解决方案

我不使用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...

这篇关于将编码的JSON加载到Dojo Grid中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆