Extjs 中实现combox多选,已经解决了原有的bug 【选择多条记录后,鼠标点击其他空白处,选择的数据丢失,】
来源:互联网 发布:淘宝找黑客改成绩 编辑:程序博客网 时间:2024/04/27 15:15
需求说明,今天在需求时候,顾客在要求可以从多个部门查询,于是乎做了这个。
实现结果:
实现代码
保存为Ext.ux.form.LovCombo在项目中引用
// vim: ts=4:sw=4:nu:fdc=4:nospell/** * Ext.ux.form.LovCombo, List of Values Combo * * @author Ing. Jozef Sakáloš * @copyright (c) 2008, by Ing. Jozef Sakáloš * @date 16. April 2008 * @version $Id: Ext.ux.form.LovCombo.js 285 2008-06-06 09:22:20Z jozo $ * * @license Ext.ux.form.LovCombo.js is licensed under the terms of the Open Source * LGPL 3.0 license. Commercial use is permitted to the extent that the * code/component(s) do NOT become part of another Open Source or Commercially * licensed development library or toolkit without explicit permission. * * License details: http://www.gnu.org/licenses/lgpl.html */ /*global Ext */// add RegExp.escape if it has not been already addedif('function' !== typeof RegExp.escape) {RegExp.escape = function(s) {if('string' !== typeof s) {return s;}// Note: if pasting from forum, precede ]/\ with backslash manuallyreturn s.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');}; // eo function escape}// create namespaceExt.ns('Ext.ux.form'); /** * * @class Ext.ux.form.LovCombo * @extends Ext.form.ComboBox */Ext.ux.form.LovCombo = Ext.extend(Ext.form.ComboBox, {// {{{ // configuration options/** * @cfg {String} checkField name of field used to store checked state. * It is automatically added to existing fields. * Change it only if it collides with your normal field. */ checkField:'checked'/** * @cfg {String} separator separator to use between values and texts */ ,separator:','/** * @cfg {String/Array} tpl Template for items. * Change it only if you know what you are doing. */// }}} // {{{ ,initComponent:function() { // template with checkboxif(!this.tpl) {this.tpl = '<tpl for=".">'+'<div class="x-combo-list-item">'+'<img src="' + Ext.BLANK_IMAGE_URL + '" '+'class="ux-lovcombo-icon ux-lovcombo-icon-'+'{[values.' + this.checkField + '?"checked":"unchecked"' + ']}">'+'<div class="ux-lovcombo-item-text">{' + (this.displayField || 'text' )+ '}</div>'+'</div>'+'</tpl>';} // call parent Ext.ux.form.LovCombo.superclass.initComponent.apply(this, arguments);// install internal event handlersthis.on({ scope:this,beforequery:this.onBeforeQuery,blur:this.onRealBlur});// remove selection from input fieldthis.onLoad = this.onLoad.createSequence(function() {if(this.el) {var v = this.el.dom.value;this.el.dom.value = '';this.el.dom.value = v;}}); } // e/o function initComponent // }}}// {{{/** * Disables default tab key bahavior * @private */,initEvents:function() {Ext.ux.form.LovCombo.superclass.initEvents.apply(this, arguments);// disable default tab handling - does no goodthis.keyNav.tab = false;} // eo function initEvents// }}}// {{{/** * clears value */,clearValue:function() {this.value = '';this.setRawValue(this.value);this.store.clearFilter();this.store.each(function(r) {r.set(this.checkField, false);}, this);if(this.hiddenField) {this.hiddenField.value = '';}this.applyEmptyText();} // eo function clearValue// }}}// {{{/** * @return {String} separator (plus space) separated list of selected displayFields * @private */,getCheckedDisplay:function() {var re = new RegExp(this.separator, "g");return this.getCheckedValue(this.displayField).replace(re, this.separator + ' ');} // eo function getCheckedDisplay// }}}// {{{/** * @return {String} separator separated list of selected valueFields * @private */,getCheckedValue:function(field) {field = field || this.valueField;var c = [];// store may be filtered so get all recordsvar snapshot = this.store.snapshot || this.store.data;snapshot.each(function(r) {if(r.get(this.checkField)) {c.push(r.get(field));}}, this);return c.join(this.separator);} // eo function getCheckedValue// }}}// {{{/** * beforequery event handler - handles multiple selections * @param {Object} qe query event * @private */,onBeforeQuery:function(qe) {qe.query = qe.query.replace(new RegExp(this.getCheckedDisplay() + '[ ' + this.separator + ']*'), '');} // eo function onBeforeQuery// }}}// {{{/** * blur event handler - runs only when real blur event is fired */,onRealBlur:function() {this.list.hide();var rv = this.getRawValue();var rva = rv.split(new RegExp(RegExp.escape(this.separator) + ' *'));var va = [];var snapshot = this.store.snapshot || this.store.data;// iterate through raw values and records and check/uncheck itemsExt.each(rva, function(v) {snapshot.each(function(r) {if(v === r.get(this.displayField)) {va.push(r.get(this.valueField));}}, this);}, this);this.setValue(va.join(this.separator));this.store.clearFilter();} // eo function onRealBlur// }}}// {{{/** * Combo's onSelect override * @private * @param {Ext.data.Record} record record that has been selected in the list * @param {Number} index index of selected (clicked) record */,onSelect:function(record, index) { if(this.fireEvent('beforeselect', this, record, index) !== false){// toggle checked fieldrecord.set(this.checkField, !record.get(this.checkField));// display full listif(this.store.isFiltered()) {this.doQuery(this.allQuery);}// set (update) value and fire eventthis.setValue(this.getCheckedValue()); this.fireEvent('select', this, record, index); }} // eo function onSelect// }}}// {{{/** * Sets the value of the LovCombo * @param {Mixed} v value */,setValue:function(v) {if(v) {v = '' + v;if(this.valueField) {this.store.clearFilter();this.store.each(function(r) {var checked = !(!v.match( '(^|' + this.separator + ')' + RegExp.escape(r.get(this.valueField))+'(' + this.separator + '|$)'));r.set(this.checkField, checked);}, this);this.value = this.getCheckedValue();this.setRawValue(this.getCheckedDisplay());if(this.hiddenField) {this.hiddenField.value = this.value;}}else {this.value = v;this.setRawValue(v);if(this.hiddenField) {this.hiddenField.value = v;}}if(this.el) {this.el.removeClass(this.emptyClass);}}else {this.clearValue();}} // eo function setValue// }}}// {{{/** * Selects all items */,selectAll:function() { this.store.each(function(record){ // toggle checked field record.set(this.checkField, true); }, this); //display full list this.doQuery(this.allQuery); this.setValue(this.getCheckedValue()); } // eo full selectAll// }}}// {{{/** * Deselects all items. Synonym for clearValue */ ,deselectAll:function() {this.clearValue(); } // eo full deselectAll // }}}// 修正 : 当多选控件放到grid中时,grideditor会调用combobox中的这个方法。将选中的显示值传入到setvalue中,导致选择丢失。 ,assertValue : function(){ var val = this.getRawValue(), rec,arr_rec,i=0; // 分离value为数组,循环取rec var arr_val = val.split(this.separator); var arr_value = this.value.split(this.separator); for(;i<arr_val.length;i++){ if(this.valueField && Ext.isDefined(arr_value[i])){ rec = this.findRecord(this.valueField, arr_value[i]); } if(!rec || rec.get(this.displayField) != arr_val[i].trim()){ rec = this.findRecord(this.displayField, arr_val[i].trim()); } if(rec && !arr_rec){ arr_rec = []; } if(rec){ arr_rec.push(rec); } } if(!arr_rec && this.forceSelection){ if(val.length > 0 && val != this.emptyText){ this.el.dom.value = Ext.value(this.lastSelectionText, ''); this.applyEmptyText(); }else{ this.clearValue(); } }else{ if(arr_rec && this.valueField){ // onSelect may have already set the value and by doing so // set the display field properly. Let's not wipe out the // valueField here by just sending the displayField. if (this.value == val){ return; } i = 0; val = ""; var ival; for(;i<arr_rec.length;i++){ ival = arr_rec[i].get(this.valueField); if(!ival){ ival = arr_rec[i].get(this.displayField); } if(i ==0 ){ val = ival; }else{ val = val+","+ival; } } } this.setValue(val); } }}); // eo extend // register xtypeExt.reg('lovcombo', Ext.ux.form.LovCombo); // eof
CSS
/** vim: ts=4:sw=4:nu:fdc=4:nospell * * Ext.ux.form.LovCombo CSS File * * @author Ing.Jozef Sak谩lo拧 * @copyright (c) 2008, by Ing. Jozef Sak谩lo拧 * @date 5. April 2008 * @version $Id: Ext.ux.form.LovCombo.css 189 2008-04-16 21:01:06Z jozo $ * * @license Ext.ux.form.LovCombo.css is licensed under the terms of the Open Source * LGPL 3.0 license. Commercial use is permitted to the extent that the * code/component(s) do NOT become part of another Open Source or Commercially * licensed development library or toolkit without explicit permission. * * License details: http://www.gnu.org/licenses/lgpl.html */.ux-lovcombo-icon { width:16px; height:16px; float:left; background-position: -1px -1px ! important; background-repeat:no-repeat ! important; } .ux-lovcombo-icon-checked { background: transparent url(../ext3/resources/images/default/menu/checked.gif); } .ux-lovcombo-icon-unchecked { background: transparent url(../ext3/resources/images/default/menu/unchecked.gif); } /* eof */
注意调整你的checked.gif 和 uncheck.gif的位置
使用案例:
{ anchor: "99%", fieldLabel: "按部门查询", xtype: "lovcombo", name: "Q_teacherName_S_LK", hideOnSelect:false, mode: "local", editable: false, triggerAction: "all", valueField: "depName", displayField: "depName", store: new Ext.data.JsonStore({ root: "result", autoLoad: true, url: __ctxPath + "/system/selectDepartment.action", fields: ["depId", "depName"] }) }
- Extjs 中实现combox多选,已经解决了原有的bug 【选择多条记录后,鼠标点击其他空白处,选择的数据丢失,】
- 将GridView和数据库查询结果绑定起来后,点击查询出了结果。但是点击第二面或者其他的,就直接变空白了。(已经解决)
- VBA中的Combox选择第一条记录
- Extjs3.3.1 实现翻页后,可记录已经选择的选项
- datagrid加checkbox实现分页不丢失选择的记录
- datagrid加checkbox实现分页不丢失选择的记录
- SQL语句:选择分类后的第一条记录
- AngularJS:表格中选择了多个复选框后如何通过点击提交按钮将复选框选中的数据提取出来
- 三维空间中鼠标点击选择线段的方法
- 解决在iframe页面里使用了DD_belatedPNG后显示空白的bug
- 窗体中选择多条记录打印
- DataGirdView 新建 Row 鼠标点击 数据变成 空白 的问题
- 我想在datagrid中选择若干记录,再点击添加按钮,给别一个ACCESS表添加选择的记录,在VB中如何实现?
- JavaScript 弹出窗体点击按钮返回选择数据的实现
- 关于jquery 1.9以上多次点击checkbox无法选择的bug解决
- 只选择Table中特定的几条数据
- extjs 时间范围选择的实现
- datagrid加checkbox实现分页不丢失选择的记录 (转)
- XML
- 基础数据结构之图二
- 、 只要openSession一次,hibernate内部就会连接数据库一次,所以在代码中opesnSession的次数,越多效率越低
- maven2添加第三方jar包
- Sony Xperia T 升级 Jelly Bean 重启问题的解决方案
- Extjs 中实现combox多选,已经解决了原有的bug 【选择多条记录后,鼠标点击其他空白处,选择的数据丢失,】
- sqlserver 同列字符连接相加
- win2003下自动备份 mysql的配置方法
- 在pyLucene中使用中文分词器(在pyLucene中引用Jar包)
- SQLServer 2005死锁终极大法
- 游戏服务器运维常见故障总结
- B*Tree索引与Bitmap索引
- Firefox使用技巧
- 从QQ邮箱的广告邮件聚合想起的