I used the clearable combobox extensively in my earlier applications to give users a way to clear a filter on a store, as I would have multiple boxes set up for filter options in a west or east panel next to a grid. When extjs4 rolled out I knew that I had to offer the same usability but wanted to upgrade some apps and my extjs3.x code just did not work. The new clearable combobox is below:
Code: http://www.learnsomethings.com/fcps_checks/js/app.js
Working Example: http://www.learnsomethings.com/fcps_checks/index.html
The code you will need to implement this is below, you may notice three custom configs for the new combobox, these allow it to work as a filter for a form object or grid.
spObject – This is the grid id that you would like to associate this combo with.
spExtraParam – This is the name of the extra param that you would like to clear using this object. Note: you will need the setExtraParam store override to use this.
spForm – Alternately, you can connect the clear function to a form using this config option.
I would have used e.within, however, this built in extjs function did not work in firefox 7 so I had to revert to the good old e.target.name, in case you were wondering.
Ext.define('Ext.ux.ClearableCombo', {
extend: 'Ext.form.field.ComboBox',
alias: 'widget.xcombo',
triggerTip: 'Click to clear selection.',
spObj:'',
spForm:'',
spExtraParam:'',
qtip:'Clearable Combo Box',
trigger1Class: 'x-form-select-trigger',
trigger2Class: 'x-form-clear-trigger',
onRender: function(ct, position){
Ext.ux.ClearableCombo.superclass.onRender.call(this, ct, position);
var id = this.getId();
this.triggerConfig = {
tag:'div', cls:'x-form-twin-triggers', style:'display:block;width:46px;', cn:[
{tag: "img", style: Ext.isIE?'margin-left:-3;height:19px':'', src: Ext.BLANK_IMAGE_URL, id:"trigger1" + id, name:"trigger1" + id, cls: "x-form-trigger " + this.trigger1Class},
{tag: "img", style: Ext.isIE?'margin-left:-6;height:19px':'', src: Ext.BLANK_IMAGE_URL, id:"trigger2" + id, name:"trigger2" + id, cls: "x-form-trigger " + this.trigger2Class}
]};
this.triggerEl.replaceWith(this.triggerConfig);
this.triggerEl.on('mouseup',function(e){
if(e.target.name == "trigger1" + id ){
this.onTriggerClick();
} else if(e.target.name == "trigger2" + id){
this.reset();
if(this.spObj!=='' && this.spExtraParam !== ''){
Ext.getCmp(this.spObj).store.setExtraParam(this.spExtraParam,'');
Ext.getCmp(this.spObj).store.load()
}
if(this.spForm!==''){
Ext.getCmp(this.spForm).getForm().reset();
}
}
},
this);
var trigger1 = Ext.get("trigger1" + id);
var trigger2 = Ext.get("trigger2" + id);
trigger1.addClsOnOver('x-form-trigger-over');
trigger2.addClsOnOver('x-form-trigger-over');
}
});
Here’s how you implement it in the javascript.
var fruits = Ext.create('Ext.data.Store', {
fields: ['fruit'],
data : [
{"fruit":"Apples"},
{"fruit":"Oranges"},
{"fruit":"Grapes"}
]});
var fruit_cbo = Ext.create('Ext.ux.ClearableCombo', {
fieldLabel: 'Choose Year',
store: fruits,
queryMode: 'local',
displayField: 'fruit',
renderTo: 'clearable_cbo',
valueField: 'fruit',
listeners:{select:{fn:function(combo, record, index) {
// do something!
}}}
});