extjs 下拉列表ComboBox动态向后天获取数据
1、comboBox静态数据创建为:
{
xtype:"combo",
name:"Degree",
fieldLabel:"学位",
store:["小学","初中","高中","专科","本科","硕士","博士"],
emptyText:"请选择适合你的学历"
}
2、comboBox向后台获取数据动态创建:
extjs 创建为:
{
xtype:'combo',
fieldLabel : '部门',
blankText : '请选择部门',
store : new Ext.data.JsonStore({
url : 'User_loadDept',//向后台获取数据的action路径
method : 'POST',//提交方式
fields : ['num', 'name'],//num:实际值,name:显示值
root : 'value'
}),
valueField : 'num',//实际值,一般是编号Id
displayField : 'name',//显示值,给人看的值
mode : 'remote',//代表store从远程传递,服务器获取
//mode:'local', //直接从本地获得数据(已通过nativeStore.load()将数据加载到本地,不需要用 remote 再从服务器获取)
triggerAction : 'all',//选中后可以修改的
editable : false//不可以编辑
}
后台action类中的方法:
// 加载部门
public void loadDept() throws Exception {
List<AyDepartment> list = null;
try {
list = deptDao.findAll();
} catch (Throwable e) {
e.printStackTrace();
}
String string = "";
if (list != null && list.size() > 0) {
string = "[";
for (AyDepartment dept : list) {
string += "{num:'" + dept.getDepId() + "',name:'"+ dept.getDepName() + "'},";
}
string = string.substring(0, string.length() - 1);
string += "]";
}
String store = "{'value':" + string + "}";
response.reset();
response.setContentType("text/json;charset=UTF-8");
response.getWriter().write(store);
System.out.println(store);
}
把store输出后格式为:
{'value':[{num:'1',name:'市场部'},{num:'2',name:'技术部'},{num:'3',name:'技术顾问'},{num:'4',name:'运营部'}]}