In an ExtJS ComboBox, it’s fairly easy to implement a search feature where given the user’s entry, you can return a list of possible matches. I outline this mechanism in Sencha ExtJS: Simple Autocomplete Example.
However, recent user feedback was, “If I know the unit I’m looking for, why can’t I just type it in and hit ENTER and skip the whole search feature to find the one I need?”
Doh.
Yes, I was making the users wait – even if for 1 second – for a list of possible matches to be displayed, when 90% of the time they know which one they want to open.
Luckily, I was able to briefly look at the documentation for ComboBox and find out how to accomplish this. You need to set the enableKeyEvents config property to true, and then hook into the keypress event and check for the user pressing the ENTER (or RETURN) key. When the ENTER key is pressed, prevent the query from being executed, and open the item the user entered.
var WidgetSearch = new Ext.form.ComboBox({
minChars: 3,
loadingText: '',
itemSelector: 'div.search-item',
triggerClass: 'x-form-search-trigger',
applyTo: 'search-field',
enableKeyEvents: true,
store: new Ext.data.Store({
proxy: new Ext.data.HttpProxy({url: 'widgets.cfm'}),
reader: new Ext.data.JsonReader({
root: 'data',
totalProperty: 'recordcount'
}, [
{name: 'widget_number', type: 'string'},
{name: 'widget_name', type: 'string'}
])
}),
tpl: new Ext.XTemplate(
'<tpl for="."><div class="search-item">',
'{widget_number} - {widget_name}',
'</div></tpl>'
),
onSelect: function(record){
document.location.href = 'displayWidgetDetails.cfm&wid=' + record.data.widget_number;
},
listeners:
{
// Hook into the keypress event to detect if the user pressed the ENTER key
keypress: function(comboBox, e){
if (e.getCharCode() == e.ENTER) {
// Prevent the default query action since the user
// believes she has entered a proper widget number
comboBox.on('beforequery', function(q){q.cancel=true;},this);
// Redirect browser to widget detail view
document.location.href = 'displayWidgetDetails.cfm&wid=' + WidgetSearch.getValue();
}
}
}
});
5 Responses for "Sencha ExtJS: Allow users to find without searching in a ComboBox"
Thanks for these great posts! I couldn’t get this to work properly as the the drop-down steals focus (and therefore keypress events) from the input field. To fix this I used the specialKey event:
listeners:
{
specialKey : function(field, e) {
if(e.getKey() == e.ENTER) {
console.log(“specialKey”, this.getValue());
}
}
}
This is great, however I need to onkeypress dynamically refresh a cfgrid. This works if I hit say “C” in the combobox and then click enter, but I want to avoid hitting enter so that as I type in the combobox, the cfgrid dynamically changes.
I am trying to take a extjs combobox that is within a cfgrid header and have json drive the extjs combobox (vs a html select dropdown as in your autocomplete example). I have seen countless examples close to this but none for exactly this. No matter what I try, I always get an error within ext-yui-adapter.js which doesn’t really make sense as I wouldn’t think ext-yui-adapter.js would even be used. It says f is undefined or is not an object…I am not referencing f anywhere. Any thoughts?
I use the Extjs’s combox to make the live search, but the confuse is ‘The combox’ must type 4 key it just work. Would you mind to give me an advice to make live search just type one key, not the special key, then it immediately do the live search , thanks first.
[...] aspect of comboboxes, but it help me get to the “aha – enableKeyEvents” moment. Sencha ExtJS: Allow users to find without searching in a ComboBox LD_AddCustomAttr("AdOpt", "1"); LD_AddCustomAttr("Origin", "other"); [...]
Leave a reply