Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

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!

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

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

about 4 years ago · Juan Pablo Isaza Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!