我实现了一个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时,我需要获得相关的vode。
任何建议都表示赞赏。
提前完成
答案 0 :(得分:0)
试试这个(根据这个片段修改你的代码)并让我知道结果(我知道,我总是写下这句话,但我不知道你在你的环境中得到了什么)
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()
方法。