Displaying Data on a JSP page using Dojo Data grid

天大地大妈咪最大 提交于 2019-12-13 07:25:28

问题


I am new to Dojo widgets. I want to display some data using dojogrid widget. I want to populate the data in widget by using an access database. i am connecting to database using JDBC and will retrieve some set of values in resultset. Now i want to Display that values in Dojogrid Widget. Please suggest me some code or links as to how to approach on this problem.? Thanks in advance.


回答1:


There are several ways of achieving this. Since you are using JSP, the easiest would be to leverage the Java bindings in JSP.

do the following: 1) in your JSP, include dojo and supporting files (stylesheets, dojo.js and dojo.requires for the grid)

2) create a java variable in the request/jsp or session that stores the data that you want to render in the grid. You can use google gson or similar library to convert Java objects to JSON

3) create a dojo datastore (such as an itemfilereadstore) by binding it to the java object that holds the data. For example:

        <%

//you can also use a Java object/collectin and converted to json string using libraries such as Gson
    String gridData = "{
                        identifier:"id",
                        items: [
                              {id:1, "Name":915,"IpAddress":6},
                              {id:2, "Name":916,"IpAddress":6},
                              {id:3, "Name":917,"IpAddress":6}                        ]
                  }";


       %>

    var gridStore = new dojo.data.ItemFileReadStore( { data: dojo.fromJson(<%= gridData %>)} );
    var gridStructure =  var gridStructure =[{
                    cells:[
                                [
                                      { field: "Name",
                                            name: "Name",
                                            width: "40%", styles: 'text-align: right;'
                                      },
                                      { field: "IpAddress",
                                            name: "Ip Address" ,
                                            width: "40%",
                                            styles: 'text-align: right;'
                                      }
                                ]
                          ]
                    }];

4) create the datagrid by specifying the layout and use the store you created in step 3 to populate the grid

  <div jsid="mygrid" id="mygrid" dojoType="dojox.grid.DataGrid" 
    gridOptions='{query:"" ,structure:gridStructure,store:gridStore}' 
    title="Simple Grid" style="width: 500px; height: 150px;"></div>

Alternately, if you want to be more SOA-ish, you can use your jsp/Java EE backend as a service that returns JSON, and use plain HTML for rendering (and use AJAX to fetch the JSON from the Java EE backend)



来源:https://stackoverflow.com/questions/9453768/displaying-data-on-a-jsp-page-using-dojo-data-grid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!