我正在使用DOJO ItemFileReadStore和dojox.grid.DataGrid来显示网格内的数据
请在此处查看图片 http://imageshare.web.id/viewer.php?file=kdfvrkmn6k7xafmi4jdy.jpg
EMployee.Java
public class Employee {
String name;
String dept;
// Setters and Getters
}
这是我的Servlet
response.setContentType("text/x-json;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
List list = new ArrayList();
Employee emp1 = new Employee();
Employee emp2 = new Employee();
emp1.setDept("CSE");
emp1.setName("Vamsi");
emp2.setDept("EEE");
emp2.setName("Raju");
list.add(emp1);
list.add(emp2);
List jsonresponse = new ArrayList();
for (int i = 0; i < list.size(); i++) {
JSONObject nextObject = new JSONObject();
nextObject.put("name", list.get(i));
jsonresponse.add(nextObject);
}
JSONObject json = new JSONObject();
json.put("label", "name");
json.put("items", jsonresponse.toArray());
response.getWriter().write(json.toString());
}
这是我的JSP页面
<body class=" claro ">
<span dojoType="dojo.data.ItemFileReadStore" jsId="store1" url="http://localhost:8080/Man/MyServlet2"></span>
<table dojoType="dojox.grid.DataGrid" store="store1"
style="width: 100%; height: 500px;">
<thead>
<tr>
<th width="150px" field="name">Name</th>
<th width="150px" field="dept">Dept</th>
</tr>
</thead>
</table>
请在此处查看图片 http://imageshare.web.id/viewer.php?file=kdfvrkmn6k7xafmi4jdy.jpg
请帮助,谢谢。
答案 0 :(得分:0)
+1用于在您的问题中发布服务器输出(firebug屏幕截图)。这使人们更容易帮助您 - 例如,我可以很容易地看到数据格式仍然不太正确。你似乎在dojo和stackoverflow都变得越来越好了!
请记住,ItemFileReadStore期望数据采用特定格式。您的servlet正在生成:
{label: "name", items: [
{name: {dept: "CSE", name: "Vansi"}},
{name: {dept: "ABC", name: "Abcd"}}
]}
您看到您告诉商店每个商品的“名称”是具有某些属性的对象(“dept”和“name”)。这就是网格在名称列中显示object Object
的原因。它应该是:
{label: "name", items: [
{dept: "CSE", name: "Vansi"},
{dept: "ABC", name: "Abcd"}
]}
我对java不是很了解,但我相信只需要对servlet进行一些小改动:
// The for loop that adds employees to jsonresponse.
for (int i = 0; i < list.size(); i++)
{
// Instead of adding the
Emloyee e = (Employee)list.get(i);
JSONObject nextObject = new JSONObject();
nextObject.put("name", e.getName());
nextObject.put("dept", e.getDept());
jsonresponse.add(nextObject);
}
事实上,您可以json.put("items", list.toArray());
而不是将每位员工添加到jsonresponse
。