Results 1 to 4 of 4

Thread: How to determine whether the combo value is from value field or display field

  1. #1
    Sencha Premium Member
    Join Date
    Apr 2010
    Posts
    195
    Vote Rating
    25
      0  

    Default How to determine whether the combo value is from value field or display field

    Is there any convenient way to determine whether the combo value is from the value field or the display field? Let's say the combo is backed by a json store with two fields: 'value' and 'display', which may look something like this:
    Code:
    [
        {'value':'0001', 'display':'Ford'},
        {'value':'0002', 'display':'VW'},
         ...  
    ]
    Now in the combo, if the users type in 'Ford', the combo value returns '0001', whereas if the users type in 'For', the combo value returns 'For' as is, as there is not a value found for the incomplete word 'For'.

    Now the questions is, is there any convenient way to determine if the combo value is from the value field or the display field? The difficult thing is that the values being populated into the store is not predictable, therefore, I don't think I should make any assumption about the pattern of user input. So I found it very difficult to precisely tell which field the value actually comes from.

    Thanks.

  2. #2
    Sencha Premium Member
    Join Date
    Apr 2010
    Posts
    195
    Vote Rating
    25
      0  

    Default

    The real tricky thing is that when the user types in 'Ford', the backend receives '0001', and when the user types in '0001', the backend also receives '0001'. If I always assume the user typed in 'Ford' even the user actually typed in '0001', the user might get confused and think, I mean '0001', why are you talking about 'Ford' to me?

  3. #3
    Sencha - Support Team slemmon's Avatar
    Join Date
    Mar 2009
    Location
    Boise, ID
    Posts
    6,154
    Vote Rating
    249
      1  

    Default

    Check out getValue() -vs- getRawValue()

    Code:
    // The data store containing the list of states
    var states = Ext.create('Ext.data.Store', {
        fields: ['abbr', 'name'],
        data : [
            {"abbr":"AL", "name":"Alabama"},
            {"abbr":"AK", "name":"Alaska"},
            {"abbr":"AZ", "name":"Arizona"}
            //...
        ]
    });
    
    
    // Create the combo box, attached to the states data store
    var combo = Ext.create('Ext.form.ComboBox', {
        fieldLabel: 'Choose State',
        store: states,
        queryMode: 'local',
        displayField: 'name',
        valueField: 'abbr',
        renderTo: Ext.getBody()
    });
    
    
    Ext.widget('button', {
        renderTo: Ext.getBody(),
        text: 'getValue()',
        handler: function () {
            console.log(combo.getValue());
        }
    });
    
    
    Ext.widget('button', {
        renderTo: Ext.getBody(),
        text: 'getRawValue()',
        handler: function () {
            console.log(combo.getRawValue());
        }
    });

  4. #4
    Sencha Premium Member
    Join Date
    Apr 2010
    Posts
    195
    Vote Rating
    25
      0  

    Default

    Thank you @slemmon. Your code answered my question. So essentially, when the combo.getValue() is the same as combo.getRawValue(), it does not matter which value to use, when they are different, it's clear to me that the user has accepted the suggested one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •