簡體   English   中英

在extjs的組合框中獲取所選值的相關ID

[英]Get relevant id for selected value in combobox in extjs

我實現了一個ExtJs組合框。

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
    })

然后,我使用Json從postgresql數據庫加載此組合。

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'
},
});

這是我的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();

    }

}

}

我的Json數據正在關注。

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

我的組合框加載良好。 現在,當用戶選擇特定的vRegNum時,我需要獲取相關的視頻。

任何建議表示贊賞。

提前感謝

嘗試一下(根據此代碼段修改您的代碼),然后讓我知道結果(我知道,我總是寫這句話,但是我不知道您在環境中得到了什么)

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();
        }
    }
},

這是組合框的重要部分getValue()方法。 如果只需要組合框的原始值,請嘗試使用getRawValue()方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM