0

I am trying to set a default value in a ComboBox in ExtJS 3.4 I have tried to set value: 'Standard' in the ComboBox config, but that only places a string in the box. I did some digging around and tried to setup the afterrender function, but still haven't gotten it to populate. The goal is to get the box to actually select the value and populate the box with the Json data, so that the user is able to select from subsequent ComboBoxes.

        var hatComboStore = new Ext.data.JsonStore({
                autoLoad: true,
                fields: [
                    'BIN_ID',
                    'BIN_DESC'
                ],
                baseParams: {
                    method: 'post'
                },
                url: 'json_bin_hat.php'
        });

        var hatCombo = new Ext.form.ComboBox({
            allowBlank: false,
            autoSelect: true,
            displayField: 'BIN_DESC',
            emptyText: 'Select a hat...',
            fieldLabel: 'Hat',
            forceSelection: false,
            hiddenName: 'hatId',
            itemId: 'hatId',
            listEmptyText: 'No records found',
            listeners: {
                afterrender: function(combo, record, index) {
                    var hat = combo.getValue();
                    binCombo.store.baseParams.hat = hat;
                },      
                select: function(combo, record, index) {
                    var hat = combo.getValue();
                    binCombo.store.baseParams.hat = hat;
                },
                focus: function(combo) {
                    binCombo.clearValue();
                }
            },
            mode: 'remote',
            msgTarget: 'side',
            name: 'hatDesc',
            store: hatComboStore,
            submitValue: true,
            triggerAction: 'all',
            valueField: 'BIN_ID'
        });

Anyone have any ideas? Thanks for your help!

sla009
  • 11
  • 2

3 Answers3

1

I've got it to work using an override method.

Basically, the displayValue is what I wanted to display ('Standard') and the value is what I wanted to execute (value: 1). Apparently, the remote store is a little tricky to work with, so that's why the override was necessary.

        var hatComboStore = new Ext.data.JsonStore({
                autoLoad: true,
                fields: [
                    'BIN_ID',
                    'BIN_DESC'
                ],
                baseParams: {
                    method: 'post'
                },
                url: 'json_bin_hat.php'
        });

        var hatCombo = new Ext.form.ComboBox({
            allowBlank: false,
            autoSelect: true,
            displayField: 'BIN_DESC',
            displayValue: 'Standard',
            emptyText: 'Select a hat...',
            fieldLabel: 'Hat',
            forceSelection: false,
            hiddenName: 'hatId',
            itemId: 'hatId',
            listEmptyText: 'No records found',
            listeners: {
                afterrender: function(combo, record, index) {
                    var hat = combo.getValue();
                    binCombo.store.baseParams.hat = hat;
                },      
                select: function(combo, record, index) {
                    var hat = combo.getValue();
                    binCombo.store.baseParams.hat = hat;
                },
                focus: function(combo) {
                    binCombo.clearValue();
                }
            },
            mode: 'remote',
            msgTarget: 'side',
            name: 'hatDesc',
            store: hatComboStore,
            submitValue: true,
            triggerAction: 'all',
            valueField: 'BIN_ID'
            value: 1
        });

        //Override method to default hatCombo value to 'Standard' while underlying value = 1
        Ext.override(Ext.form.ComboBox, {
            initValue: Ext.form.ComboBox.prototype.initValue.createSequence(function() {
                if (this.mode == 'remote' && !!this.valueField && this.valueField != this.displayField && this.displayValue) {
                    if (this.forceSelection) {
                        this.lastSelectionText = this.displayValue;
                    }    
                    this.setRawValue(this.displayValue);
                }    
            })
        });

tdy
  • 36,675
  • 19
  • 86
  • 83
sla009
  • 11
  • 2
0

Looks like the value prop is not working for remote stores. Probably the combo is rendered before the store has loaded. You can set the value after the store has loaded.

This works for me (tested as an ExtJS 3.4 fiddle).

var hatComboStore = new Ext.data.JsonStore({
    autoLoad: true,
    fields: [
        'id',
        'title'
    ],
    url: 'https://jsonplaceholder.typicode.com/todos'
});

var hatCombo = new Ext.form.ComboBox({
    fieldLabel: 'Hat',
    store: hatComboStore,
    displayField: 'title',
    valueField: 'id',
    mode: 'local',
    submitValue: true,
    triggerAction: 'all',
    // Apparently does not work with remote store.
    // value: '1'
});

hatComboStore.on('load', () => hatCombo.setValue('1'));

new Ext.form.FormPanel({
    items: hatCombo,
    renderTo: Ext.getBody()
});

srk
  • 1,625
  • 1
  • 10
  • 26
  • Thanks for your help. I am still just getting the value of Standard in as a string. This doesn't actually select the "Standard" option in the combobox, it just places the string in the box and I cannot use the input in the rest of the form. I cannot edit the store because I'm reading values from a remote SQL database that I reference later in my code. The closest I've gotten was editing value to '1', but I need to decode the value of 1 to display 'Standard – sla009 Oct 29 '21 at 19:21
  • I've edited my answer to use a remote store. – srk Oct 29 '21 at 21:27
-1

see example of setting default value:

Combo config:

{
    triggerAction: 'all',
    id: 'dir_id',
    fieldLabel: 'Direction',
    queryMode: 'local',
    editable: false,
    xtype: 'combo',
    store : dirValuesStore,
    displayField:'name',
    valueField:'id',
    value: 'all',
    width: 250,
    forceSelection:true
}

source: Extjs 4 combobox default value