根据Json数据中的id检索值,并将该值设置为组合框的值

时间:2014-01-07 12:18:42

标签: json extjs combobox

我实现了一个JsonStore如下。

var defaulsValStore = new Ext.data.JsonStore({
fields : [ {
    id : 'category'
}, {
    name : 'name'
} ],
root : 'defaultVal',
//idProperty : 'category',
autoLoad : true,

proxy : new Ext.data.HttpProxy({
    url : "http://" + host + ":" + port + "/" + projectName + "/"
            + "SetDefaultValues"

}),
reader : {
    type : 'json',
    root : 'defaultVal'
},
});

我的SetDefaultValues.java servlet是:

//imports


public class SetDefaultValues extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public SetDefaultValues() {
    super();
    // TODO Auto-generated constructor stub
}

@Override
public void init() throws ServletException {
    // TODO Auto-generated method stub
    super.init();
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
 *      response)
 */
protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    processRequest(request, response);
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
 *      response)
 */
protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    processRequest(request, response);
}

protected void processRequest(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    ServletContext context = getServletContext();

    String dbName = context.getInitParameter("ConnectionDB");
    String connectionHost = context.getInitParameter("ConnectionHost");
    String connectionUser = context.getInitParameter("ConnectionUser");
    String connectionPassword = context
            .getInitParameter("ConnectionPassword");
    String port = "5433";

    Statement statement = null;
    ResultSet vehicleResultSet = null;
    Connection pgConnection = null;
    // String lineString = "";

    try {
        pgConnection = ConnectionManager.getPostgresConnection(
                connectionHost, connectionUser, connectionPassword, dbName,
                port);
        // out.println(connectionHost+","+ connectionUser+","+
        // connectionPassword+","+ dbName);
        statement = pgConnection.createStatement();
        // out.print(pgConnection);

        String sql = "";

        sql = "SELECT v.vehiclecode, v.employeecode, e.name, e.employeecategory FROM hoobtvehcileemplyees v, hoobtemplyees e WHERE v.employeecode = e.employeecode";

        vehicleResultSet = statement.executeQuery(sql);

        String jsonData = "{'defaultVal':[";

        while (vehicleResultSet.next()) {
            jsonData += "{ 'category' : '";
            jsonData += vehicleResultSet.getString(4).trim();
            jsonData += "', ";
            jsonData += "'name' : '";
            jsonData += vehicleResultSet.getString(3).trim();

            if (vehicleResultSet.isLast()) {
                jsonData += "' }  ";
            } else {
                jsonData += "' } , ";
            }
        }

        jsonData += "]}";
        out.print(jsonData);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        out.println(e.toString());
        e.printStackTrace();

    }

}

}

我的Json数据是这样的:

{'defaultVal':[{ 'category' : '1', 'name' : 'Kamal Subhasingha' } , { 'category' : '2', 'name' : 'Namal Witharana' } , { 'category' : '3', 'name' : 'Yohan' } , { 'category' : '4', 'name' : 'Ahan Liyanage' } , { 'category' : '5', 'name' : 'Sampath Jayaweera' } , { 'category' : '6', 'name' : 'Saman' } ]}

现在我需要根据类别获取名称并将该名称设置为我的组合框值。当用户选择另一个组合框值时,就会发生这种情况。 我的组合框:

new Ext.form.ComboBox({
        store : vehicleStore,
        displayField : 'vRegNum',
        valueField : 'vCode',
        fieldLabel : 'Delivery Vehicle',
        id : 'vehicleCombo',
        typeAhead : true,
        forceSelection : true,
        mode : 'local',
        triggerAction : 'all',
        selectOnFocus : true,
        editable : true,
        hidden : false,
        //xtype : 'combo',
        minChars : 1,
        hideLabel : true,
        style : 'marginleft:10px',
        listeners : {
            select : function() {
                var selectedVehicle = Ext.getCmp('vehicleCombo').getValue();
                alert(selectedVehicle);


            }

        },

        emptyText : 'Delivery Vehicle'

    })

1 个答案:

答案 0 :(得分:0)

组合框的事件是否正确启动了?

尝试使用change-listener而不是select-listener:

    listeners : {
        change : function(combobox, selectedVehicle) {
            alert(selectedVehicle);
        }