本文介绍了Dojo 中的 DataGrid,带有来自 servlet 的 json 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我第一次使用 JSON...并想用我的 JSON 数据填充我的数据网格,这是我的 JSON 数据:
I am using JSON for first time...and want to fill my datagrid with my JSON data,this is my JSON data :
{
"head": {
"vars": [ "s" , "fname" , "lname" ]
} ,
"results": {
"bindings": [
{
"s": { "type": "uri" , "value": "http://tn.gov.in/Person/41" } ,
"fname": { "type": "literal" , "value": " } ,
"lname": { "type": "literal" ,n" }
} ,
{
"s": { "type": "uri" , "value": "http://tn.gov.in/Person/37" } ,
"fname": { "type": "literal" , "value": "sh" } ,
"lname": { "type": "literal" , "value": "Vvan" }
} ,
{
"s": { "type": "uri" , "value": "http://tn.gov.in/Person/39" } ,
"fname": { "type": "literal" , "value": "Vavan " } ,
"lname": { "type": "literal" , "value": "Sran" }
}
]
}
}
我想在数据网格中显示fname
和lname
我应该怎么做呢?任何人都可以提供适用于上述 JSON 的示例代码吗?我用例子尝试了很多,我得到了一个空白网格
I want to display fname
and lname
in the data grid how should I so it?can any one give a sample code which works for above JSON? I tried a lot with examples , i am getting a blank grid
推荐答案
这里的关键是您需要先转换数据,然后才能在 dojo 网格中使用它.
The key point here is that you need to transform your data first before using it in dojo grid.
可以在此处找到现场演示.
dojo.require("dojox.grid.DataGrid");
dojo.require("dojo.data.ItemFileReadStore");
dojo.addOnLoad(function() {
var data = { "head": { "vars": [ "s" , "fname" , "lname" ] } , "results": { "bindings": [ { "s": { "type": "uri" , "value": "http://tn.gov.in/Person/41" } , "fname": { "type": "literal" , "value": "Gayathri" } , "lname": { "type": "literal" , "value": "Vasudevan" } } , { "s": { "type": "uri" , "value": "http://tn.gov.in/Person/37" } , "fname": { "type": "literal" , "value": "Magesh" } , "lname": { "type": "literal" , "value": "Vasudevan" } } , { "s": { "type": "uri" , "value": "http://tn.gov.in/Person/39" } , "fname": { "type": "literal" , "value": "Vasudevan " } , "lname": { "type": "literal" , "value": "Srinivasan" } } ] } };
var items = dojo.map(data.results.bindings, function(binding) {
return {fname : binding.fname.value, lname : binding.lname.value};
});
var store = new dojo.data.ItemFileReadStore({
data : {
items : items
}
});
_createGrid(store);
function _createGrid(store) {
var layout = _getGridLayout(),
node = dojo.create("div", {}, dojo.byId("grid"), "only");
var grid = new dojox.grid.DataGrid({
store : store,
structure : layout,
rowsPerPage: 10
}, node);
grid.update();
grid.startup();
return grid;
}
function _getGridLayout() {
return [[
{ field : "fname", name : "First Name", width : "50%"},
{ field : "lname", name : "Last Name", width : "50%" }
]];
}
});
这篇关于Dojo 中的 DataGrid,带有来自 servlet 的 json 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!