在EXTJS中如何使用OnSelectRow在另一个网格中选择一行时在网格中加载数据?

时间:2013-12-16 11:11:28

标签: java json servlets extjs4

我是extjs的新手。我开发了一个应用程序,其中有两个网格。在单击第一个网格的行时,第二个网格应根据所选的特定行显示数据。我不知道如何将该行的值传递给servlet页面?

下面的

是第一个网格的代码......

     var store = new Ext.data.JsonStore({
    fields: [
    'YCSET_ID','YCSET_DESC', 'YCSET_CCY', 'YCSET_COMP_MODE','YCSET_YCTYPE','YCSET_INTBASIS','YCSET_DISC_FACTOR'
            ],
    proxy: {
        type: 'ajax',
        url: 'USCities',
        params: {
            store_id: 1

    },
    scope:this,
    //method to call when the request is successful
    success: this.onSaveSuccess,
    //method to call when the request is a failure
    failure: this.onSaveFailure,
        reader: {
               type: 'json',
               root: 'cityList'
           }
    }
        });
     store.load();
       // create the Grid
        var grid = new Ext.grid.GridPanel({
            selType: 'cellmodel',
            trackMouseOver: true,
            plugins: [
                Ext.create('Ext.grid.plugin.CellEditing', {
                    clicksToEdit: 2
                })
                ],
       store: store,
        columns: [
                {xtype: 'rownumberer'},
                {header: 'YCSET_ID', width: 90, id :'ycset_id',sortable: true, dataIndex: 'YCSET_ID',editor: 'textfield'},
                {header: 'YCSET_DESC', width: 90, sortable: true, dataIndex: 'YCSET_DESC',editor: 'textfield'},
                {header: 'YCSET_CCY', width: 80, sortable: true, dataIndex: 'YCSET_CCY'},
                {header: 'YCSET_COMP_MODE', width: 90, sortable: true, dataIndex: 'YCSET_COMP_MODE'},
                {header: 'YCSET_YCTYPE', width: 100, sortable: true, dataIndex: 'YCSET_YCTYPE',editor: 'textfield'},
                {header: 'YCSET_INTBASIS', width: 100, sortable: true, dataIndex: 'YCSET_INTBASIS'},
                {header: 'YCSET_DISC_FACTOR', width: 150, sortable: true, dataIndex: 'YCSET_DISC_FACTOR',editor:'textfield'}
            ],
            dockedItems: [{
                xtype: 'pagingtoolbar',
                store: store,   
                dock: 'bottom',
                displayInfo: true
                //plugins: Ext.create('Ext.ux.ProgressBarPager', {})
            }],
            viewConfig: {
                forceFit: true,
                stripeRows: true,
                enableTextSelection: true
            },

            height:267,
            autoExpandColumn: 'ycset_id',
            autoScroll: true,
            width:620

        });

USCities。的java

 package pack1;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

import pack2.City;
import pack2.CityInformation;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

public class USCities extends HttpServlet {
private static final long serialVersionUID = 1L;
private String storeId;
public USCities() {
super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) {           
doPost(request,response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
storeId = request.getParameter("store_id");
String start = request.getParameter("start");
String limit = request.getParameter("limit");
System.out.println(storeId);

PrintWriter out = response.getWriter();
response.setContentType("text/html");

CityInformation cityInformation = new CityInformation();

ArrayList<City> cityList = cityInformation.getCities(storeId,start,limit);
System.out.println("cityList----------"+cityList);
Gson gson = new Gson();
JsonArray arrayObj = new JsonArray();
for(int i=0;i<cityList.size();i++){
  System.out.println("cityList-----1-----"+cityList.get(i).toString());
City city = cityList.get(i);
JsonElement cityObj = gson.toJsonTree(city); 
System.out.println("Json Element----"+cityObj);
arrayObj.add(cityObj);
}

JsonObject myObj = new JsonObject();
myObj.addProperty("success", true);
myObj.add("cityList", arrayObj);
myObj.addProperty("totalCount", limit);

out.println(myObj.toString());
out.close();

}

 private void updateyield(HttpServletRequest request, HttpServletResponse response) throws IOException{

 //get the record Information 
 String recordInfo = request.getParameter("recordInfo");

 //parse JSON object and populate the Customer bean 
 JSONObject yieldObj = (JSONObject) JSONSerializer.toJSON(recordInfo); 
 City city = (City) JSONObject.toBean(yieldObj, City.class);

 CityInformation cityInformation = new CityInformation();

boolean success = cityInformation.updategrid(storeId, city);

 PrintWriter out = response.getWriter();
 response.setContentType("text/html");

 //send response back whether the request was successful
 JSONObject myObj = new JSONObject();
 myObj.put("success", success);
 out.println(myObj.toString());
 out.close();

 }
 }

*的 City.java *

 package pack2;
import java.sql.Connection;
 import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class CityInformation {   

Connection conn = null;             
PreparedStatement stmt = null;      
String sql = null;

public ArrayList<City> getCities(String storeId, String start, String limit) {  

ArrayList<City> cityList = new ArrayList<City>(); 


try {       

  Class.forName("oracle.jdbc.driver.OracleDriver");
 conn =      DriverManager.getConnection("jdbc:oracle:thin:@172.16.3.150:1521:NABA","STEN","STEN");
  System.out.println("connection establisted ");
 sql="SELECT   YCSET_ID,YCSET_DESC,YCSET_CCY,YCSET_COMP_MODE,YCSET_YCTYPE,YCSET_INTBASIS,YCSET_DISC_FACTOR FROM YCSET_MASTER";
 stmt = conn.prepareStatement(sql);
 stmt.setInt(1,Integer.parseInt(storeId));
 stmt.setInt(2, Integer.parseInt(start));
 stmt.setInt(3, Integer.parseInt(limit));
  ResultSet rs = stmt.executeQuery();  

 while(rs.next()){ 
 City city = new City();

 city.setYCSET_ID(rs.getString("YCSET_ID"));
 city.setYCSET_DESC(rs.getString("YCSET_DESC").trim());
 city.setYCSET_CCY(rs.getString("YCSET_CCY").trim());
 city.setYCSET_COMP_MODE(rs.getString("YCSET_COMP_MODE").trim());
 city.setYCSET_YCTYPE(rs.getString("YCSET_YCTYPE"));
 city.setYCSET_INTBASIS(rs.getString("YCSET_INTBASIS"));
 city.setYCSET_DISC_FACTOR(rs.getString("YCSET_DISC_FACTOR").trim());
 cityList.add(city);

 }                                                                          

  rs.close();                                                                
  stmt.close();                                                              
  stmt = null;                                                               


   conn.close();                                                              
   conn = null;                                                    

    }                                                                
   catch(Exception e){
  System.out.println(e);
  }                       

    finally {                                                        
  if (stmt != null) {                                             
  try {                                                          
   stmt.close();                                                 
   } catch (SQLException sqlex) {                                 

  }                                                              

  stmt = null;                                             
  }                                                         

  if (conn != null) {                                       
 try {                                                    
 conn.close();                                           
 } catch (SQLException sqlex) {                           

 }                                                        

 conn = null;                                             
 }                                                         
}               

 return cityList;

 }   
  public boolean updategrid(String storeId, City city) { 

 boolean success = true;

 try {      
     //Context ctx = (Context) new InitialContext().lookup("java:comp/env");
     //conn = ((DataSource) ctx.lookup("jdbc/mysql")).getConnection(); 
     Class.forName("oracle.jdbc.driver.OracleDriver");
     conn = DriverManager.getConnection("jdbc:oracle:thin:@172.16.3.196:1521:NABARDRM","NABARD_STENV_2","NABARD_STENV_2");

     sql = "UPDATE YCSET_MASTER set YCSET_ID = ?, YCSET_DESC = ?, YCSET_YCTYPE = ?, YCSET_DISC_FACTOR = ?" +
     " where store_id = ? and YCSET_ID = ?";
     stmt = conn.prepareStatement(sql);
     stmt.setString(1,city.getYCSET_ID()); 
     stmt.setString(2,city.getYCSET_DESC());
     stmt.setString(6,city.getYCSET_YCTYPE());
     stmt.setString(8,city.getYCSET_DISC_FACTOR());

     stmt.executeUpdate();

     stmt.close();                                                             
     stmt = null;                                                              
     conn.close();                                                             
     conn = null;                                                   

   }                                                               
   catch(Exception e){

     success = false;
     System.out.println(e);

   }                      

      finally {                                                       

       if (stmt != null) {                                            
         try {                                                         
             stmt.close();                                                
         } catch (SQLException sqlex) {                                
             // ignore -- as we can't do anything about it here           
         }                                                             

         stmt = null;                                            
       }                                                        

      if (conn != null) {                                      
         try {                                                   
             conn.close();                                          
         } catch (SQLException sqlex) {                          
             // ignore -- as we can't do anything about it here     
         }                                                       

         conn = null;                                            
     }                                                        
   }              

   return success;

  }  

 }   

1 个答案:

答案 0 :(得分:0)

grid1.getSelectionModel().on('selectionchange', function(selModel, records) {
    var selectedRecord = records[0];
    grid2.getStore().load({
        params: {
            paramName: selectedRecord.get('param') //this will be sent as "paramName" to the server 
        }
    })
, this);

您可以在文档页面查看一个类似的示例: http://docs.sencha.com/extjs/4.2.1/#!/example/grid/binding.html