Displaying Data on a JSP page using Dojo Data grid

Deadly 提交于 2019-12-13 04:19:37

问题


I have written this code which is returning a Json string.It contains a set of values(names). Now i want to display these values on a jsp page using Dojo data grid. I Don't know how to use this returned Json string as a data for Dojo grid. And how to format the table Structure. Also i want when i click on a particular row in a table(which in this case contains only a single column - Employee name as per my query) a new Window opens up(probably a new JSP page). how to do that? Please help me with the code. Thanks.

PopulateTextbox.java

package MyPackage;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;

public class PopulateTextbox {

    Gson gson = new Gson();
    String temp;
    List <String>rowValues = new ArrayList<String>();
    String[] contactListNames;
    Connection con=null;
    Statement st=null;
    ResultSet rs=null;


    public String method(){


        try{


        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        Class.forName(driver);

        String db = "jdbc:odbc:Practice_Database";
        con = DriverManager.getConnection(db,"","");

        st = con.createStatement();
        String sql = "SELECT Emp_Name FROM EmployeeSearch";
        rs = st.executeQuery(sql);

        while(rs.next()){

            rowValues.add(rs.getString("Emp_Name"));
        }
        contactListNames = (String[]) rowValues.toArray(new String[rowValues.size()]);
        temp = gson.toJson(contactListNames);

    }catch(Exception e){System.out.println(e);}
    /*finally{
        try {
                if(con!=null)con.close();
            } catch (SQLException e) {

                e.printStackTrace();
            }
        try {
            if(rs!=null)rs.close();
        } catch (SQLException e) {

            e.printStackTrace();
        }try {
            if(st!=null)st.close();

        } catch (SQLException e) {

            e.printStackTrace();
        }


    }*/
        return temp;

    }
}

回答1:


The dojox.grid.DataGrid uses a data store as its source. A dojo.data.ItemFileReadStore can take a json object that is the data.

var grid = ...
var store = new dojo.data.ItemFileReadStore({
    data: YOUR_JSON_HERE
});
grid.setStore(store);

The json that the store uses to initialize itself looks like the following

{
    identifier: 'id',
    items: [
        { id: 0, name: 'x' },
        { id: 1, name: 'y' }
    ]
}

So you will need to modify your java code to generate the specific json format. Also note that each item has an id that must be unique across all items in the store.

http://dojotoolkit.org/reference-guide/dojo/data/ItemFileReadStore.html

The DataGrid has onRowClick and onRowDblClick events that you can tie into to do what you need when the user selects it.

http://dojotoolkit.org/reference-guide/dojox/grid/DataGrid.html#dojox-grid-datagrid



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

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