标签: combobox
2015-06-14 23:23 5171人阅读 评论(2) 收藏 举报
 分类:
ExtJS(32) 
 

目录(?)[+]

 

原文转自起飞网http://www.qeefee.com/article/000171

ComboBox 是ExtJS中经常用到的控件,今天我们来讲一下它的一些用法。

使用本地Store

ComboBox需要结合Store一起使用,下面是一个最简单的例子,结合本地的Store的用法:

  1. var genderStore = Ext.create("Ext.data.Store", {
  2. fields: ["Name", "Value"],
  3. data: [
  4. { Name: "男", Value: 1 },
  5. { Name: "女", Value: 2 }
  6. ]
  7. });
  8.  
  9. var form = Ext.create("Ext.form.FormPanel", {
  10. layout: "column",
  11. title: "用户信息",
  12. border: false,
  13. margin: "10",
  14. width: 800,
  15. defaultType: "textfield",
  16. fieldDefaults: { labelAlign: 'right', labelWidth: 80, margin: "5 0", columnWidth: 0.3, msgTarget: "qtip", selectOnFocus: true },
  17. items: [
  18. { name: "UserName", fieldLabel: "姓名", allowBlank: false },
  19. {
  20. xtype: "combobox",
  21. name: "Gender",
  22. fieldLabel: "性别",
  23. store: genderStore,
  24. editable: false,
  25. displayField: "Name",
  26. valueField: "Value",
  27. emptyText: "--请选择--",
  28. queryMode: "local"
  29. }
  30. ],
  31. renderTo: Ext.getBody()
  32. });

运行页面,截图如下:

这大概就是最简单的用法了,绑定一个store,设置显示和值的字段就可以了。代码注释如下:

  1. xtype: "combobox", //使用xtype定义
  2. name: "Gender", //form提交时的名称
  3. fieldLabel: "性别", //显示的Label
  4. store: genderStore, //绑定的Store
  5. editable: false, //是否可编辑
  6. displayField: "Name", //显示的字段
  7. valueField: "Value", //值的字段
  8. emptyText: "--请选择--", //当没有值时的水印文字
  9. queryMode: "local" //查询模式

使用远程数据动态加载的Store

首先我们来修改一下Store:

  1. var genderStore = Ext.create("Ext.data.Store", {
  2. fields: ["Name", "Value"],
  3. autoLoad: true,
  4. proxy: {
  5. type: "ajax",
  6. actionMethods: { read: "POST" },
  7. url: rootUrl + "Combo/FetchGenderList",
  8. reader: {
  9. type: "json",
  10. root: "data"
  11. }
  12. }
  13. });

此时,我们的Store会加载url中的Json参数,看看我们ASP.NET MVC  中的代码:

  1. public JsonResult FetchGenderList()
  2. {
  3. AjaxResult result = new AjaxResult();
  4. List<GenderModel> genderList = new List<GenderModel>();
  5. genderList.Add(new GenderModel() { Name = "男", Value = 1 });
  6. genderList.Add(new GenderModel() { Name = "女", Value = 2 });
  7. result.Set(true, genderList);
  8.  
  9. return Json(result);
  10. }

由于在MVC中,默认的JsonResult不允许Get请求,所以我们在请求数据的时候使用了POST方式,这通过设置actionMethods来实现的,默认的actionMethods如下:

  1. {create: 'POST', read: 'GET', update: 'POST', destroy: 'POST'}

我们修改了proxy中的actionMethods:

  1. actionMethods: { read: "POST" }

这样就可以实现通过POST方式请求数据了。

此时form中的代码不用修改,直接编译运行就可以了。

combo 多选

combo内置了对多选的支持,只需要将multiSelect配置项设置为true即可。为了配合例子说明多选如何使用,我们在form中增加一个兴趣的字段,其配置项如下:

  1. {
  2. xtype: "combobox",
  3. name: "Interest",
  4. fieldLabel: "兴趣",
  5. store: interestStore,
  6. editable: false,
  7. multiSelect: true,
  8. displayField: "InterestName",
  9. valueField: "InterestCode",
  10. emptyText: "--请选择--",
  11. queryMode: "local"
  12. }

多选的store如下:

  1. var interestStore = Ext.create("Ext.data.Store", {
  2. fields: ["InterestName", "InterestCode"],
  3. autoLoad: true,
  4. proxy: {
  5. type: "ajax",
  6. actionMethods: { read: "POST" },
  7. url: rootUrl + "Combo/FetchInterestList",
  8. reader: {
  9. type: "json",
  10. root: "data"
  11. }
  12. }
  13. });

服务器的方法如下:

  1. public JsonResult FetchInterestList()
  2. {
  3. OperateResult result = new OperateResult();
  4. List<InterestModel> interestList = new List<InterestModel>();
  5. interestList.Add(new InterestModel() { InterestCode = "01", InterestName = "读书" });
  6. interestList.Add(new InterestModel() { InterestCode = "02", InterestName = "摄影" });
  7. interestList.Add(new InterestModel() { InterestCode = "03", InterestName = "集邮" });
  8. interestList.Add(new InterestModel() { InterestCode = "04", InterestName = "音乐" });
  9. interestList.Add(new InterestModel() { InterestCode = "05", InterestName = "种植" });
  10. interestList.Add(new InterestModel() { InterestCode = "06", InterestName = "跳舞" });
  11. result.Set(true, interestList);
  12.  
  13. return Json(result);
  14. }

我都是硬编码,没有搞数据库动态获取什么的,只是一个演示。

编译代码之后刷新页面,看到新增的字段已经显示出来了,我们选择两个,效果图如下:

赋值和取值

不管是什么控件,赋值和取值都是大家比较关心的。来看看赋值的代码:

  1. buttons: [
  2. {
  3. text: "为性别赋值",
  4. handler: function () {
  5. this.up("form").down("combobox[name=Gender]").setValue(2);
  6. }
  7. },
  8. {
  9. text: "为兴趣赋值",
  10. handler: function () {
  11. this.up("form").down("combobox[name=Interest]").setValue(["02", "04"]);
  12. }
  13. }
  14. ]

为form添加两个Button,分别为两个combobox赋值。

  • 对于单选的赋值,只要把value传递给combobox的setValue方法就可以了。
  • 对于多选的赋值,需要传递value的数组给setValue方法。

接下来看看获取值的方法,继续添加一个获取值的Button,代码如下:

  1. {
  2. text: "获取Form值",
  3. handler: function () {
  4. var data = this.up("form").getValues();
  5. Ext.MessageBox.alert("提示", "Gender:" + data.Gender + "<br />Interest:" + data.Interest);
  6. }
  7. }

data中包含了我们form中的值,它是Json格式的数据,我们可以方便的获取Gender和Interest的数据。运行代码的截图如下:

这些都是客户端的处理,在真正使用的时候,我们是需要将整个form提交给服务器的,那么接下来看看服务器的处理吧,继续添加按钮,用来提交form:

  1. {
  2. text: "提交Form",
  3. handler: function () {
  4. var formCmp = this.up("form");
  5. if (formCmp.isValid()) {
  6. formCmp.submit({
  7. url: rootUrl + "Combo/SubmitFormData",
  8. mehtod: "POST",
  9. success: function (form, action) {
  10. var result = action.result;
  11. },
  12. failure: function (form, action) {
  13. Ext.Msg.alert("failed", action.result.message);
  14. }
  15. });
  16. }
  17. }
  18. }

服务器端的处理,我们首先添加一个UserModel:

  1. public class UserModel
  2. {
  3. public string UserName { get; set; }
  4. public int Gender { get; set; }
  5. public List<string> Interest { get; set; }
  6. }

然后是SubmitFormData的代码:

  1. public JsonResult SubmitFormData(UserModel model)
  2. {
  3. OperateResult result = new OperateResult();
  4.  
  5. result.Set(true);
  6. return Json(result);
  7. }

运行程序,我们打开跟踪调试,得到model的值如下:

我们服务器已经很好的接收到了值。

然后我们在看最后一个例子,使用form从服务器加载数据,这个也是很常用的。首先还是添加按钮,当点击的时候从服务器取数据:

  1. {
  2. text: "加载Form数据",
  3. handler: function () {
  4. var formCmp = this.up("form");
  5. formCmp.load({
  6. url: rootUrl + "Combo/FetchFormData",
  7. mehtod: "POST",
  8. success: function (form, action) {
  9.  
  10. },
  11. failure: function (form, action) {
  12. Ext.Msg.alert("failed", action.result.message);
  13. }
  14. });
  15. }
  16. }

然后是服务器的方法:

  1. public JsonResult FetchFormData()
  2. {
  3. OperateResult result = new OperateResult();
  4.  
  5. UserModel model = new UserModel();
  6. model.UserName = "Jerry";
  7. model.Gender = 2;
  8. model.Interest = new List<string>() { "01", "03", "06" };
  9.  
  10. result.Set(true, model);
  11. return Json(result);
  12. }

当单击按钮的时候,我们的form将加载到数据:

默认值

listeners : {
      afterRender : function(combo) {
         var firstValue = store.reader.jsonData[0].text;
         combo.setValue(firstValue);//同时下拉框会将与name为firstValue值对应的 text显示
      }
   }

});

combo.on('load',function(){Ext.getCmp("myCombo").setValue(1);});

显示的值

获取实际值是用,getValue()方法,

获取显示值是用,getRawValue()方法

ExtJS ComboBox 下拉列表详细用法的更多相关文章

  1. 45. ExtJS ComboBox 下拉列表详细用法

    转自:https://blog.csdn.net/luckypeng/article/details/46496151 ComboBox 是ExtJS中经常用到的控件,今天我们来讲一下它的一些用法. ...

  2. MFC中ComboBox控件用法

    MFC ComboBox 一.入门篇 ComboBox (组合框)控件很简单,可以节省空间.从用户角度来看,这个控件是由一个文本输入控件和一个下拉菜单组成的.用户可以从一个预先定义的列表里选择一个选项 ...

  3. C#播放声音的四种方法 +AxWindowsMediaPlayer的详细用法

    C#播放声音的四种方法 第一种是利用DirectX 1.安装了DirectX SDK(有9个DLL文件).这里我们只用到MicroSoft.DirectX.dll和 Microsoft.Directx ...

  4. 在DOS下的DEBUG命令的详细用法

    在DOS下的DEBUG命令的详细用法 名称 解释 格式 a (Assemble) 逐行汇编 a [address] c (Compare) 比较两内存块 c range address d (Dump ...

  5. __declspec关键字详细用法

    __declspec关键字详细用法 __declspec用于指定所给定类型的实例的与Microsoft相关的存储方式.其它的有关存储方式的修饰符如static与extern等是C和C++语言的ANSI ...

  6. CString.Format的详细用法(转)

    CString.Format的详细用法(转) 在MFC程序中,使用CString来处理字符串是一个很不错的选择.CString既可以处理Unicode标准的字符串,也可以处理ANSI标准的字符串.CS ...

  7. IFRAM的详细用法

    IFRAM的详细用法:   IFRAM的详细用法:  <IFRAME>用于设置文本或图形的浮动图文框或容器. BORDER <IFRAME BORDER="3"& ...

  8. 【转】java.util.vector中的vector的详细用法

    [转]java.util.vector中的vector的详细用法 ArrayList会比Vector快,他是非同步的,如果设计涉及到多线程,还是用Vector比较好一些 import java.uti ...

  9. DOM Style样式对象的详细用法

    DOM Style样式对象的详细用法 HTML Style样式比较复杂,相应访问.修改方法也有所差异.参考相关资料,整理如下. 典型Html文件如下,有三种定义方式. <head>     ...

随机推荐

  1. charset - 设置 G0/G1 字符集槽中的一个的 ACM

    总览 (SYNOPSIS) charset [-v] G0|G1 [cp437|iso01|vt100|user|<acm_name>] 描述 (DESCRIPTION) linux 终端 ...

  2. Mysql使用遇到的问题(一)

    1.在使用MySQL的时候,已经新建好了表,插入数据的时候报这个错误: Incorrect string value: '\xE5\xAF\x92\xE6\xB1\x9F...' for column ...

  3. 第1节 yarn:13、yarn资源调度的介绍

    Yarn资源调度 yarn集群的监控管理界面: http://192.168.52.100:8088/cluster jobHistoryServer查看界面: http://192.168.52.1 ...

  4. 第2节 mapreduce深入学习:7、MapReduce的规约过程combiner

    第2节 mapreduce深入学习:7.MapReduce的规约过程combiner 每一个 map 都可能会产生大量的本地输出,Combiner 的作用就是对 map 端的输出先做一次合并,以减少在 ...

  5. hdfs深入:09、获取分布式文件系统客户端的几种方式

    FileSystem是一个抽象类:获取一个抽象类有两种方式:第一种:看这个抽象类有没有提供什么方法返回他本身第二种:找子类 具体代码如下: /** * 通过url注册的方式访问hdfs,了解,不会用到 ...

  6. 如何用SQL语句在指定字段前面插入新的字段?

    如何用SQL语句在指定字段前面插入新的字段? 2007-10-17 09:28:00|  分类: 笔记|举报|字号 订阅     create proc addcolumn @tablename va ...

  7. BZOJ 1711 吃饭dining/Luogu P1402 酒店之王 拆点+最大流流匹配

    题意: (吃饭dining)有F种食物和D种饮料,每种食物或饮料只能供一头牛享用,且每头牛只享用一种食物和一种饮料.现在有n头牛,每头牛都有自己喜欢的食物种类列表和饮料种类列表,问最多能使几头牛同时享 ...

  8. Luogu P4299 首都 LCT

    既然是中文题目,这里便不给题意. 分析: 这个题的做法据说是启发式合并? 但是我不会啊…… 进入正题,LCT是怎样做掉这道题的.记得在前面的一篇<大融合>的题解中,介绍过LCT维护子树信息 ...

  9. 笔试算法题(35):最长递增子序列 & 判定一个字符串是否可由另一个字符串旋转得到

    出题:求数组中最长递增子序列的长度(递增子序列的元素可以不相连): 分析: 解法1:应用DP之前需要确定当前问题是否具有无后效性,也就是每个状态都是对之前状态的一个总结,之后的状态仅会受到前一个状态的 ...

  10. 用LAMP构架创建DISCUZ论坛

    # rpm -q httpd mariadb mariadb-server php php-mysql # yum -y install httpd mariadb-server php php-my ...