Setting a Default Value to a ComboBox using Json in ExtJS 3.4

391 views Asked by At

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!

3

There are 3 answers

2
Irakli Shalamberidze On

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

2
srk On

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()
});

0
sla009 On

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);
                }    
            })
        });