Extjs combobox动态加载的实例

js文件----------------------------------
storeRoleName = new Ext.data.Store({  
    proxy: new Ext.data.HttpProxy({  
        url: '../TestServlet'
    }),  
    reader: new Ext.data.JsonReader({
    root:'roleNameDate'
        },[
        {name: 'value', mapping: 'vluae'},
        {name: 'name', mapping: 'name'}
    ])
});

var combo =new Ext.formComboBox({
name:'roleName',
allowBlank: false,
readOnly:true,
store:storeRoleName,
valueField: 'value',
displayField:'name',
mode: 'remote',
loadingText: 'loading...',
triggerAction: 'all',
emptyText:'请选择'
});



servlet文件 TestServlet.java-------------------------------------
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   try {
    Class.forName("com.mysql.jdbc.Driver");
    conn = DriverManager.getConnection(
      "jdbc:mysql://localhost:3306/assetmanagementdb", "root",
      "12345");
    String sql = "select roleName from tb_role";
    stm = conn.createStatement();
    rs = stm.executeQuery(sql);
    response.setContentType("text/plain");
    response.setCharacterEncoding("utf-8");
    System.out.println("---------------------------------------------");
    PrintWriter out = response.getWriter();
    String s = "{\"roleNameDate\":[";
    while(rs.next())
    {
     s = s + "{value:'" + rs.getString("roleName") + "',";
     s = s + "name:'" + rs.getString("roleName") + "'},";
    }
    String s1 = s.substring(0, s.length()-1);
    s1 = s1 + "]}";
    out.print(s1);
    System.out.println(s1);
         out.flush();
         out.close();
   } catch (Exception e) {
    e.printStackTrace();
   }
   finally
   {
    try
    {
     if(rs!=null)
       rs.close();
     if(stm!=null)
       stm.close();
     if(conn!=null)
       conn.close();
    }
    catch(Exception e1){
     e1.getMessage();
    }
   }  
}



你可能感兴趣的:(Extjs combobox动态加载的实例)