Extjs让combobox写起来简洁又漂亮
也已经写了很久时间的extjs ,每次都用到很多的combobox,配置很多东西觉得实在是太麻烦,所以根据常用到的情况写了一个简便的combobox,再次记录下来,以免放在某个地方忘记了找不到了。
定义一个基本的baseCombobox类,如下。
Ext.define('Admin.view.baseCmp.BaseCombobox', { extend: 'Ext.form.field.ComboBox', xtype: 'baseCombobox', editable: false, labelSeparator: ':', labelWdith: 0, triggerAction: 'all', labelAlign: 'right', //forceSelection: true,此属性操作时,就算去掉文字后,失去焦点后还是会选择上一次选择的记录 autoSelect: true, selectOnfocus: true, valueNotFoundText: '', name:'', queryMode: 'local', url:'', displayField: '', valueField: '', requires:['Admin.view.baseCmp.BaseComboboxController'], controller: 'baseComboboxController', emptyIndex:-1,//自定义属性,空值所在下标,-1则不添加 selectIndex:0,//自定义属性,自动选择下标 params:null,//自定义属性,数据参数 listeners: { render: 'getComboData', scope: 'controller' }, });
Ext.define('Admin.view.baseCmp.BaseComboboxController', { extend: 'Ext.app.ViewController', alias: 'controller.baseComboboxController', getComboData: function (combo) { Ext.Ajax.request({ url: combo.url, method :'POST', params:combo.params, success: function (response) { var dataJson = Ext.decode(response.responseText); if(dataJson.state != 200 || dataJson.data == null || dataJson.data.length == 0) { //服务器返回错误 return ; } var data = dataJson.data; //插入“全部”选项 if(combo.emptyIndex >= 0) { var emp = {}; emp[combo.displayField] = "全部"; emp[combo.valueField] = "全部"; Ext.Array.insert(data,combo.emptyIndex,[emp]); } var store = Ext.create('Ext.data.Store', { fields: Ext.Object.getKeys(data[0]), data: data }); combo.setStore(store); //如果指定选中某个值 if(combo.selectValue != null) { combo.select(combo.selectValue); } else { //如果指定选中某个下标的值,-1为最后一个,> 0 则为第selectIndex个 if(combo.selectIndex == -1) { console.log(data.length - 1); combo.select(data[data.length - 1][combo.valueField]); } else { combo.select(data[combo.selectIndex][combo.valueField]); } } //触发选中事件 //combo.fireEvent('select', combo,store.getAt(combo.selectIndex)); }, failure: function (response) { //请求服务器失败 } }); } });
调用实例:
{ xtype: 'baseCombobox', name: "typeName", fieldLabel: "类型", displayField: 'typeName', valueField: 'id', emptyIndex:0, multiSelect:false, url:"/itemType/list", listeners:{ select:'query' } },
这样大大方便了我使用combobox,如果某种类型的combobox需要重复使用,建议还是直接定义好他,到需要用的时候一句:
xtype: 'itemTypeCombobox',就可以搞定了,代码看起来简洁又漂亮。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
常用Extjs工具:Extjs.util.Format使用方法
常用Extjs工具:Extjs.util.Format使用方法,需要的朋友可以参考下2012-03-03extJs 文本框后面加上说明文字+下拉列表选中值后触发事件
在用extjs做参数设置的时候,想在文本框后加入说明子,在网站上走了一大圈还是没能找到合适的,小花用了一个下午的上班时间终于将这个页面与我想要的功能做出,页面很好看与自然,希望网友在用extjs做项目的时候用的着.2009-11-11基于ExtJs在页面上window再调用Window的事件处理方法
下面小编就为大家带来一篇基于ExtJs在页面上window再调用Window的事件处理方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-07-07JavaScript的Ext JS框架中的GridPanel组件使用指南
GridPanel和TreePnal功能类似,都是Ext JS中的表格便携利器,相比之下GridPanel还要更强大并且更复杂一些,下面我们就来整理一下JavaScript的Ext JS框架中的GridPanel组件使用指南2016-05-05ExtJs 学习笔记 Ext.Panle Ext.TabPanel Ext.Viewport
ExtJs 学习笔记基础篇 面板的使用(Ext.Panle、Ext.TabPanel、Ext.Viewport)2008-12-12
最新评论