简体   繁体   中英

Get relevant id for selected value in combobox in extjs

I am implemented an ExtJs combobox.

new Ext.form.ComboBox({
        store : vehicleStore,
        displayField : 'vRegNum',
        valueField : 'vRegNum',
        fieldLabel : 'Vehicles',
        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() {
            }

        },
        //width : 147,
        emptyText : 'Delivery Vehicle'
        //flex : 1
    })

And I load this combo from postgresql database using Json.

var vehicleStore = new Ext.data.JsonStore({
fields : [ {
    id : 'vCode'
}, {
    name : 'vRegNum'
} ],
root : 'vehicles',
//autoDestroy : true,
autoLoad : true,

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

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

This is my DeliveryVehicle.java servlet.

//imports
public class DeliveryVehicle extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public DeliveryVehicle() {
    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 {
    processRequest(request, response);
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
 *      response)
 */
protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    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 vehiclecode, registrationnumber from hoobtvehicles v WHERE v.status='1'";

        vehicleResultSet = statement.executeQuery(sql);

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

        while (vehicleResultSet.next()) {
            jsonData += "{ 'vCode' : '";
            jsonData += vehicleResultSet.getString(1).trim();
            jsonData += "', ";
            jsonData += "'vRegNum' : '";
            jsonData += vehicleResultSet.getString(2).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();

    }

}

}

My Json data is following.

{'vehicles':[{ 'vCode' : '1001', 'vRegNum' : 'XY-100-123' } , { 'vCode' : '1002', 'vRegNum' : 'GY-122-120' } , { 'vCode' : '1000000001', 'vRegNum' : 'XY-100-123' } ]}

My combobox is loaded fine. Now I need to get relevant vode when user select a particular vRegNum.

Any suggestions are appreciated.

Thanx in advance

Try this ( modify your code based on this snippet ) and let me know the result ( I know, I always write this sentence but I don't know what you get in your environment )

items: [
{
    xtype: 'combobox',
    fieldLabel: 'CTG ALANI',
    id: 'ctg-main',
    inputWidth: 467,
    fieldStyle: 'height: 26px',
    margin: '15 5 0 0',
    valueField: 'CUST_ASSORT_SECTION_ID',
    displayField: 'CTG_SECTION',
    store: ctgSection,
    queryMode: 'local',
    autoSelect: true,
    forceSelection: true,
    triggerAction: 'all',
    listeners: {
        select: function (combo) {
            ctgDescription.proxy.extraParams = {'ctgmain': combo.getValue(), 'type': 'ctg_desc'};
            ctgDescription.removeAll();
            ctgDescription.load();
        }
    }
},

Here is, important part getValue() method of the combobox. If you want only raw value of the combobox, just try getRawValue() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM