lanevska
26 Aug 2011, 3:54 AM
Hi,
I am newbie to Extjs development and I am coding the MVC application. There is a combobox switching from localstorage which contains a search history with autocomplete to remote store. I encountered the following problem: when I enter a query and press Enter it always selects the first item in the drop-down list and sends it as a query parameter plquery via Ajax request. If I click on the trigger, it sends a correct one.
Here i post a code of my search component using ComboBox:
Ext.define('SWIP.view.search.SearchBar', {
extend : 'Ext.form.ComboBox',
alias : 'widget.searchbar',
store : 'HistoryRecords',
fieldLabel: 'Search',
labelWidth: 50,
trigger1Cls: Ext.baseCSSPrefix + 'form-clear-trigger',
trigger2Cls: Ext.baseCSSPrefix + 'form-search-trigger',
hasSearch : false,
paramName : 'plquery',
displayField: 'query',
autoSelect: true,
selectOnFocus:true,
typeAhead: true,
queryMode: 'local',
triggerAction: 'query',
disableKeyFilter:'true',
minChars: 1,
listConfig: {
getInnerTpl: function() {
return '<div class="search-item-combo" ><h3>{query}<span>{[Ext.Date.format(values.searchDate, "l, F d, Y g:i:s A")]}<br /></span></h3></div>';
}
},
initComponent : function() {
this.callParent(arguments);
this.addEvents('trigger1click', 'trigger2click');
this.on('specialkey', function(f, e){
if(e.getKey() == e.ENTER){
this.onTrigger2Click();
}
}, this);
},
onTrigger1Click : function(){
this.fireEvent('trigger1click',this);
},
onTrigger2Click : function(){
this.fireEvent('trigger2click',this);
}
});
Model
Ext.define('SWIP.model.HistoryRecord', {
extend : 'Ext.data.Model',
fields:
[{
name: 'query',
mapping: 'query'
},{
name: 'searchDate',
type: 'date',
dateFormat: 'c',
mapping: 'searchDate'
}]
});
Localstorage:
Ext.define('SWIP.store.HistoryRecords', {
extend : 'Ext.data.Store',
model : 'SWIP.model.HistoryRecord',
storeId : 'HistoryRecords',
autoLoad : true,
autoSync : false,
proxy : {
type: 'localstorage',
id : 'local-id'
},
constructor : function() {
this.callParent(arguments);
}
});
Controller
onSearchBarTrigger2Click : function(){
var recordIndex = -2;
var me = this.getSearchbar(),
store = this.getResultsStore(),
proxy = store.getProxy(),
value = me.getValue();
if (value.length < 1) {
this.onSearchBarTrigger1Click();
return;
}
proxy.extraParams[me.paramName] = value;
store.load();
me.hasSearch = true;
me.triggerEl.item(0).setDisplayed('block');
me.doComponentLayout();
this.getSearchbar().store = this.getHistoryRecordsStore();
recordIndex = this.getSearchbar().store.findBy(
function(record, id){
console.log('record.get' +record.get('query') + ' id = '+id);
if(record.get('query') == value){
return true; // a record with this data exists
}
return false; // there is no record in the store with this data
}
);
if (recordIndex > -1){
model = this.getSearchbar().store.getAt(recordIndex);
}
else if (recordIndex == -1)
{
model = Ext.ModelMgr.create({
query: value,
searchDate: new Date()
}, 'SWIP.model.HistoryRecord');
this.getSearchbar().store.add(model);
this.getSearchbar().store.sync();
}
}
});
After 2 days of searching on the forums I didn't find the way to fix it and I really count on your help.
Regards,
Anna Lanevska
I am newbie to Extjs development and I am coding the MVC application. There is a combobox switching from localstorage which contains a search history with autocomplete to remote store. I encountered the following problem: when I enter a query and press Enter it always selects the first item in the drop-down list and sends it as a query parameter plquery via Ajax request. If I click on the trigger, it sends a correct one.
Here i post a code of my search component using ComboBox:
Ext.define('SWIP.view.search.SearchBar', {
extend : 'Ext.form.ComboBox',
alias : 'widget.searchbar',
store : 'HistoryRecords',
fieldLabel: 'Search',
labelWidth: 50,
trigger1Cls: Ext.baseCSSPrefix + 'form-clear-trigger',
trigger2Cls: Ext.baseCSSPrefix + 'form-search-trigger',
hasSearch : false,
paramName : 'plquery',
displayField: 'query',
autoSelect: true,
selectOnFocus:true,
typeAhead: true,
queryMode: 'local',
triggerAction: 'query',
disableKeyFilter:'true',
minChars: 1,
listConfig: {
getInnerTpl: function() {
return '<div class="search-item-combo" ><h3>{query}<span>{[Ext.Date.format(values.searchDate, "l, F d, Y g:i:s A")]}<br /></span></h3></div>';
}
},
initComponent : function() {
this.callParent(arguments);
this.addEvents('trigger1click', 'trigger2click');
this.on('specialkey', function(f, e){
if(e.getKey() == e.ENTER){
this.onTrigger2Click();
}
}, this);
},
onTrigger1Click : function(){
this.fireEvent('trigger1click',this);
},
onTrigger2Click : function(){
this.fireEvent('trigger2click',this);
}
});
Model
Ext.define('SWIP.model.HistoryRecord', {
extend : 'Ext.data.Model',
fields:
[{
name: 'query',
mapping: 'query'
},{
name: 'searchDate',
type: 'date',
dateFormat: 'c',
mapping: 'searchDate'
}]
});
Localstorage:
Ext.define('SWIP.store.HistoryRecords', {
extend : 'Ext.data.Store',
model : 'SWIP.model.HistoryRecord',
storeId : 'HistoryRecords',
autoLoad : true,
autoSync : false,
proxy : {
type: 'localstorage',
id : 'local-id'
},
constructor : function() {
this.callParent(arguments);
}
});
Controller
onSearchBarTrigger2Click : function(){
var recordIndex = -2;
var me = this.getSearchbar(),
store = this.getResultsStore(),
proxy = store.getProxy(),
value = me.getValue();
if (value.length < 1) {
this.onSearchBarTrigger1Click();
return;
}
proxy.extraParams[me.paramName] = value;
store.load();
me.hasSearch = true;
me.triggerEl.item(0).setDisplayed('block');
me.doComponentLayout();
this.getSearchbar().store = this.getHistoryRecordsStore();
recordIndex = this.getSearchbar().store.findBy(
function(record, id){
console.log('record.get' +record.get('query') + ' id = '+id);
if(record.get('query') == value){
return true; // a record with this data exists
}
return false; // there is no record in the store with this data
}
);
if (recordIndex > -1){
model = this.getSearchbar().store.getAt(recordIndex);
}
else if (recordIndex == -1)
{
model = Ext.ModelMgr.create({
query: value,
searchDate: new Date()
}, 'SWIP.model.HistoryRecord');
this.getSearchbar().store.add(model);
this.getSearchbar().store.sync();
}
}
});
After 2 days of searching on the forums I didn't find the way to fix it and I really count on your help.
Regards,
Anna Lanevska