-
13 Jan 2009 7:40 AM #1
How to use datagrid/JSON/jsp
How to use datagrid/JSON/jsp
I'm trying to get the datagrid to work with json and jsp
this my file jsp
and this is my file jsCode:<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ page language="java" import="java.sql.*"%> <%@ page language="java" import="java.io.*" %> <%@ page language="java" import="net.sf.json.*" %> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> <% JSONObject essai = new JSONObject(); essai.put("firstname","thara"); essai.put("occupation","aaaaa"); essai.put("job","nnnnnn"); out.print(essai); %> </body> </html>
and this my file htmlCode:Ext.onReady(function(){ var ds = new Ext.data.Store({ proxy: new Ext.data.HttpProxy({url:'index.jsp'}), reader: new Ext.data.JsonReader({ root: "essai", fields: [{ name: 'firstname'}, { name: 'occupation'}, { name: 'job'}] //remoteSort : false, //autoLoad : true }) }); var grid = new Ext.grid.GridPanel( { ds:ds, cm: new Ext.grid.ColumnModel([ {header : "Mynom", width: 8, dataIndex : 'firstname'}, {header: "Myoccupation", width: 10, dataIndex: 'occupation'}, {header: "Myjob", width: 10, dataIndex: 'job'} ]), viewConfig: {forceFit:true}, width: 600, height: 300, collapsible: true, animCollapse: false, title: 'EmployeeInformation', iconCls: 'icon-grid', renderTo: document.body }) ds.load() });
On the screen i show just the datagrid without dataCode:<html> <head> <title>Exemple JSON</title> <link rel="stylesheet" type="text/css" href="lib/ext/resources/css/ext-all.css" /> <!-- GC --> <!-- LIBS --> <script type="text/javascript" src="lib/ext/adapter/ext/ext-base.js"></script> <!-- ENDLIBS --> <script type="text/javascript" src="lib/ext/ext-all.js"></script> <script type="text/javascript" src="ExampleJSON.js"></script> <!--<script language="javascript" src="../grid/PropsGrid.js"></script>--> <style type="text/css"> html, body { font:normal 12px verdana; margin:0; padding:0; border:0 none; overflow:hidden; height:100%; } p { margin:5px; } .settings { background-image:url(shared/icons/fam/folder_wrench.png); } .nav { background-image:url(shared/icons/fam/folder_go.png); } </style> </head> <body> <div id="sarra"></div> </body> </html>
?Please help
Thanks
-
13 Jan 2009 8:02 AM #2Sencha - Community Support Team
- Join Date
- Mar 2007
- Location
- Frederick MD, NYC, DC
- Posts
- 16,169
- Vote Rating
- 28
verify that the JSON is actually being rendered. Please show how it's being sent to the browser.
my gut feeling tells me that you're sending back something like:which is wrongCode:{firstname : 'thara', occupation : 'aaaaa', job : 'nnnnn'}
Jay Garcia @ModusJesus || Modus Create co-founder
Ext JS in Action author
Sencha Touch in Action author
Get in touch for Ext JS & Sencha Touch Touch Training
We are also working on Video-based Sencha Touch training: Check it out here.
-
13 Jan 2009 8:06 AM #3
1) If you are using the simple json jar file to create your Serialized objects. it might not work. I used to use this jar for all my other JSON outputs, but EXT JS expects a certain format. Another alternative is to use FlexJSON @ http://flexjson.sourceforge.net/ They have a section on how JSON is expected in EXT JS and they accomodate for it. I'm not sure if the library you are using will have the right format.Code:I'm trying to get the datagrid to work with json and jsp JSONObject essai = new JSONObject(); essai.put("firstname","thara"); essai.put("occupation","aaaaa"); essai.put("job","nnnnnn"); out.print(essai);
2) You don't have a store set up but I see you have the following in your Grid Panel code
3) How do you plan on calling this JSP to return the JSON output to you. I know a JSP is a servlet, but it would be easier to create a servlet and just have it return the JSON Data.Code:var grid = new Ext.grid.GridPanel( { ds:ds, - Where is this(Ext.data.Store) configured?
-
14 Jan 2009 12:11 AM #4
Hi,
when I execute the jsp on browser, I have this result:
I don't know where is the problemCode:{"firstname":"thara","occupation":"aaaaa","job":"nnnnnn"}
Is this fragment of code correct??:
thanksCode:essai.put("firstname","thara"); essai.put("occupation","aaaaa"); essai.put("job","nnnnnn"); out.println(essai);
-
14 Jan 2009 6:40 AM #5
I don't think that is correct. Read the JsonReader class and it shows what you need to pass back to the grid.
You need to add the following to your JSON response.
More importantly, which JSON Serializer are you using? FlexJSON takes care ofCode:totalProperty: 'results', root: 'rows', id: 'id'
the required properties EXT JS expects in the JSON output.
-
14 Jan 2009 7:24 AM #6
Sorry if I'm off-topic, but I don't have the rights to create a new thread

Just one question: how can flexJSON serialize the properties in the natural order? (and not alphabetical).
For example, to obtain :
(which is the order of fields as declared in the class)Code:{ "firstName": "William", "address": "addr" }
instead of the version alphabetically sorted by field's name:
ThanksCode:{ "address": "addr", "firstName": "William" }
-
14 Jan 2009 7:44 AM #7
-
14 Jan 2009 7:50 AM #8
I need to create a grid with dynamic headers and data. This json data is parsed by a script js and then set to the Data Store and the ColumnModel of a GridPanel.
I've already tried that solution ...no luck
-
14 Jan 2009 8:04 AM #9


Reply With Quote