Results 1 to 4 of 4

Thread: Set default value on select form field

  1. #1
    Sencha User cyberwombat's Avatar
    Join Date
    Oct 2011
    Posts
    237
    Answers
    6
    Vote Rating
    4
      0  

    Default Answered: Set default value on select form field

    Given this:
    Code:
    xtype: 'selectfield',
    			                    	name: 'gender',
                        				label: 'Gender',
                        				required: false,
                        				useClearIcon: true,
                        				options: [{
                        					text: 'Male',
                        					value: 'male'
                        				}, {
                        					text: 'Female',
                        					value: 'female'
                        				}]
    How do I start with a blank value - If I add a blank option is shows in drop down.
    Thanks!

  2. Evidently you have to trick it. It's a two step process. In the view setup a listener for the painted event:

    Code:
    Ext.define('MyApp.view.Register', {
        extend: 'Ext.Panel',
        xtype: 'register',
        initialize: function() {
            this.on({
                scope: this,
                painted: 'onPainted',
            });
        },
        onPainted: function() {
            Ext.getCmp('gender').setOptions([  //use id of select
                {text: 'Male',  value: 'male'},
                {text: 'Female', value: 'female'}
            ])
        },
      .....
    And then in the select do NOT put options:
    Code:
    {
         xtype: 'selectfield',
         name: 'gender',
         label: 'Gender',
         id: 'gender',   //we need an id for the listener
         required: false,
         useClearIcon: true,
         placeHolder: ''   //this can be anything
    },

  3. #2
    Sencha - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,556
    Answers
    3931
    Vote Rating
    1272
      0  

    Default

    If you want text to be shown when there is no value, then placeHolder is the config you want to use. It's marked as deprecated but it will not be and that message will be removed on next release.
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

  4. #3
    Sencha User cyberwombat's Avatar
    Join Date
    Oct 2011
    Posts
    237
    Answers
    6
    Vote Rating
    4
      0  

    Default

    Doesn't seem to work for selects.

    This works:
    Code:
    {
        xtype: 'textfield',
        label: 'Phone Number',
        required: true,
        placeHolder: '9992222',
    },
    But this doesn't
    Code:
    {
        xtype: 'selectfield',
        name: 'gender',
        label: 'Gender',
    
    
        required: false,
        useClearIcon: true,
           placeHolder: 'Female',  //or any other value such as female, hello, blank
        options: [{
            text: 'Male',
            value: 'male'
        }, {
            text: 'Female',
            value: 'female'
        }]
    
    
    },
    Last edited by cyberwombat; 2 Nov 2011 at 8:57 AM. Reason: mispell

  5. #4
    Sencha User cyberwombat's Avatar
    Join Date
    Oct 2011
    Posts
    237
    Answers
    6
    Vote Rating
    4
      0  

    Default

    Evidently you have to trick it. It's a two step process. In the view setup a listener for the painted event:

    Code:
    Ext.define('MyApp.view.Register', {
        extend: 'Ext.Panel',
        xtype: 'register',
        initialize: function() {
            this.on({
                scope: this,
                painted: 'onPainted',
            });
        },
        onPainted: function() {
            Ext.getCmp('gender').setOptions([  //use id of select
                {text: 'Male',  value: 'male'},
                {text: 'Female', value: 'female'}
            ])
        },
      .....
    And then in the select do NOT put options:
    Code:
    {
         xtype: 'selectfield',
         name: 'gender',
         label: 'Gender',
         id: 'gender',   //we need an id for the listener
         required: false,
         useClearIcon: true,
         placeHolder: ''   //this can be anything
    },

Posting Permissions

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