Thank you for reporting this bug. We will make it our priority to review this report.
  1. #1
    Ext User mjoksa's Avatar
    Join Date
    Jun 2007
    Posts
    73
    Vote Rating
    0
    mjoksa is on a distinguished road

      0  

    Default [CLOSED][3.??]ComboBox with strings in ArrayStore and empty item issue...

    [CLOSED][3.??]ComboBox with strings in ArrayStore and empty item issue...


    Hello,
    I just discovered issue mentioned in title. I have created combo as shown in below code
    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:
    Code:
    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

  2. #2
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    16,913
    Vote Rating
    630
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    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.
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!

Thread Participants: 1