PDA

View Full Version : [CLOSED][3.??]ComboBox with strings in ArrayStore and empty item issue...



mjoksa
31 Aug 2009, 12:08 PM
Hello,
I just discovered issue mentioned in title. I have created combo as shown in below code


StatusStore = function() {
var data = [
['', ''],
['0', 'In-Active'],
['1', 'Active']
];
var store = new Ext.data.ArrayStore({
idIndex: 0,
fields: ['value', 'name'],
data: data
});
return store;
} ();
StatusCombo = function(config) {
var defaultConfig = {
emptyText: '[None]',
tpl: '<tpl for="."><div class="x-combo-list-item">{name:defaultValue("&nbsp;")}</div></tpl>',//HACK: Render empty value in valid height. See: http://extjs.com/forum/showthread.php?t=65803&highlight=combobox+empty+text
name: 'StatusID',
hiddenName: 'StatusID',
displayField: 'name',
valueField: 'value',
forceSelection: true,
editable: false,
fieldLabel: 'Status',
store: StatusStore,
triggerAction: 'all',
mode: 'local',
anchor: '-24'
};
Ext.applyIf(config, defaultConfig);
return new Ext.form.ComboBox(config);
};


When setValue(0) method is called, combo will set its value to an empty item (first item in ArrayStore). This happens because JavaScript is weakly typed and comparison like 0 == '' will return true.
To overcome this issue I had to override findRecord method in ComboBox with code below:


Ext.override(Ext.form.ComboBox, {
findRecord: function(prop, value){
var record;
if(this.store.getCount() > 0){
this.store.each(function(r){
if ((typeof(r.data[prop]) == 'string' && typeof(value) == 'number' && r.data[prop] == '') ||
(typeof(r.data[prop]) == 'number' && typeof(value) == 'string' && value == '')) {
if(value === r.data[prop]) {
record = r;
return false;
}
}
else {
if(r.data[prop] == value){
record = r;
return false;
}
}
});
}
return record;
}
});


Can anyone from Ext team confirm this and inform community if this is going to be fixed in next revision of framework.

Cheers

evant
31 Aug 2009, 4:53 PM
You could use strict equivalance (===), but that ~still~ won't solve your problem, because '' nor '0' === 0. At this point it's probably not likely we'll fix this, you should pass in the appropriate value for your store to ensure that you get an appropriate match.

Marking this as closed.