分享

ExtJs4学习笔记(五) comboBox使用方法及扩展

 一本正经地胡闹 2019-06-13
  • 基础用法

首先介绍最基础的用法,效果类似用html中的select但是提供后台加载方法,下面来看下代码:

  1. Ext.onReady(function(){
  2. //模拟数据,如果需要后台加载直接用proxy即可
  3. var store=Ext.create('Ext.data.Store',{
  4. fields:['text','value'],
  5. data:[{
  6. "text":"天津",
  7. "value":"tj",
  8. },{
  9. "text":"北京",
  10. "value":"bj",
  11. },{
  12. "text":"上海",
  13. "value":"sh",
  14. }]
  15. });
  16. Ext.create('Ext.form.ComboBox',{
  17. fieldLabel: '城市',
  18. store: store,//数据
  19. queryMode: 'local',//加载本地数据
  20. displayField: 'text',//显示的字段,对应store中的text值
  21. valueField: 'vaule',//实际传递到后台的值
  22. renderTo: Ext.getBody()//直接输出到body中
  23. })
  24. })

效果如图:


  • 下拉树

这里用到了一个官方的拓展Ext.ux.TreePicker,在Ext中的example文件夹中会有一个ux文件夹,将整个文件夹拷贝到自己项目的ExtJs的src文件夹下,没看懂路径也不要紧,直接引用Ext.ux.TreePicker会提示路径错误,这时候吧ux放到他提示的路径即可。由于下拉树需要读取后台信息,所以这里用到了.net mvc4,下面贴上代码。

前台页面js

  1. Ext.onReady(function () {
  2. Ext.onReady(function () {
  3. var combo=Ext.create('Ext.ux.TreePicker', {
  4. url: 'home/getComboBox',//获得数据的地址
  5. displayField: 'text',//显示的文本
  6. valueField: 'id',//提交表单后传递到后台的值
  7. renderTo: Ext.getBody()
  8. })
  9. console.info(combo);
  10. })
  11. })
后台tree实体类
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. namespace Extjs4Demo.Models
  6. {
  7. public class Tree
  8. {
  9. public string id { set; get; }
  10. public string text { set; get; }
  11. public bool leaf { set; get; }
  12. public Tree children { set; get; }
  13. }
  14. }

后台controller
  1. public ActionResult getComboBox()
  2. {
  3. Tree tree1=new Tree(){id="t001",leaf=true,text="测试树节点01"};
  4. Tree tree2=new Tree(){id="t002",leaf=true,text="测试树节点02"};
  5. Tree tree3=new Tree(){id="t003",leaf=true,text="测试树节点03"};
  6. Tree tree4=new Tree(){id="t004",leaf=true,text="测试树节点04"};
  7. Tree tree5=new Tree(){id="t005",leaf=true,text="测试树节点05"};
  8. List<Tree> list=new List<Tree>();
  9. list.Add(tree1);
  10. list.Add(tree2);
  11. list.Add(tree3);
  12. list.Add(tree4);
  13. list.Add(tree5);
  14. return Json(list, JsonRequestBehavior.AllowGet);
  15. }

下面是效果图:


  • 下拉Grid

待续

  • 多选下拉列表

其实官方提供了一个多选列表,但是我们发现官方示例并不好用,因为当我们进行多选后,再打开多选菜单原来被选中的项并没有被标注,而且comboxBox中可以用delete删除comoboBox。

那么我们来说下新找到的这个扩展究竟有什么优点。

1.选中后会在列表中高亮选中

2.在列表中点击已被选中项,会取消选中,并删除combox对用数据

3.支持在选项前添加图标

4.支持双击combox中选中项来取消选中

5.支持一件删除所有选中

6.档选中项超出列表时会自动拉伸,以适应多选。

更多优点就不赘述,下面贴上官方地址http://www./forum/showthread.php?195773-Ext.ux.ComboFieldBox-intuitive-multi-select-combobox-for-4.1,虽然上面说支持4.1但实际4.2也是支持的。

首选需要导入对应js和css

js不需要手动导入,只需要将2个js文件放入项目js文件夹src/ux中即可ComboFieldBox.css需要手动导入

  1. Ext.onReady(function () {
  2.     //自定义model
  3.     Ext.define('State', {
  4.         extend: 'Ext.data.Model',
  5.         fields: [
  6.             { type: 'string', name: 'abbr' },
  7.             { type: 'string', name: 'name' },
  8.             { type: 'string', name: 'slogan' },
  9.             { type: 'string', name: 'iconCls' }
  10.         ]
  11.     });
  12.     //模拟数据
  13.     var states = [
  14.         { "abbr": "AL", "name": "Alabama", "slogan": "The Heart of Dixie" },
  15.         { "abbr": "FR", "name": "France", "slogan": "Sarkozy will never be elected again. French people are not that stupid!", iconCls: 'x-icon-fr' },
  16.         { "abbr": "AU", "name": "Australia", iconCls: 'x-icon-au' },
  17.         { "abbr": "AT", "name": "Austria", iconCls: 'x-icon-at' },
  18.         { "abbr": "AK", "name": "Alaska", "slogan": "The Land of the Midnight Sun" },
  19.         { "abbr": "AZ", "name": "Arizona", "slogan": "The Grand Canyon State" },
  20.         { "abbr": "AR", "name": "Arkansas", "slogan": "The Natural State" },
  21.         { "abbr": "CA", "name": "California", "slogan": "The Golden State" },
  22.         { "abbr": "CO", "name": "Colorado", "slogan": "The Mountain State" },
  23.         { "abbr": "CT", "name": "Connecticut", "slogan": "The Constitution State" },
  24.         { "abbr": "IA", "name": "Iowa", "slogan": "The Corn State" },
  25.         { "abbr": "LT", "name": "Long test to see if it gets ellipsifyed", "slogan": "The Constitution State" }
  26.     ];
  27.     //自定义store
  28.     var store = Ext.create('Ext.data.Store', {
  29.         model: 'State',
  30.         data: states,
  31.         storeId: 'storeId'
  32.     });
  33.     var comboFieldBox2 = Ext.create('Ext.ux.ComboFieldBox', {
  34.         displayField: 'name',//显示字段
  35.         value: ['AK', 'CT'],//默认选中
  36.         iconClsField: 'iconCls',//图标
  37.         valueField: 'abbr',//传递到后台字段
  38.         width: 500,//宽度
  39.         labelWidth: 130,//标题宽度
  40.         store: store,//store
  41.         queryMode: 'local',//本地数据
  42.         forceSelection: false,
  43.         renderTo: 'mul'
  44.     });
  45. })

效果如图:

转自

https://blog.csdn.net/u014677625/article/details/23868561

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约