1. #1
    Sencha User
    Join Date
    Apr 2008
    Posts
    133
    Vote Rating
    0
    FireGlow is on a distinguished road

      0  

    Default Get Selected Value of a picker

    Get Selected Value of a picker


    Hey there,

    I'm trying hard to get the selected value of a picker. After a few hours I'm totally confused why it doesn't just work. I just used the code of the example and added listeners. Last standing is here::

    Code:
    	var picker = Ext.create('Ext.Picker', {
    		id : 'picker',
    		hideOnMaskTap: true,
    	    slots: [
    	        {
    	            name : 'player',
    	            title: 'Player',
    	            data : [
    	                    {text: '50 KB/s', value: 50},
    	                    {text: '100 KB/s', value: 100},
    	                    {text: '200 KB/s', value: 200},
    	                    {text: '300 KB/s', value: 300}
    	                ]
    	        }
    	    ],   
            listeners: {
    
    
            	change : {
    				fn : function() {
    					for (myKey in Ext.getCmp('picker').getValue()){
    						alert (" Ext.getCmp('picker').getValue()["+myKey +"] = "+Ext.getCmp('picker').getValue()[myKey]);
    						}
                    }
                },
    
            	pick : {
    				fn : function(picker, value) {
    					for (myKey in value)
    					{
    						alert ("value["+myKey +"] = "+value[myKey]);
    					}
                    }
                }
            }
    	});
    	Ext.Viewport.add(picker);
    	picker.show();
    Both times the alert works but it only shows me value[player] = null or Ext.getCmp('picker').getValue()[player] = null. Is there anybody who can help me to get the selected value, since it is the most necessary functionality of a picker

  2. #2
    Sencha User
    Join Date
    Apr 2008
    Posts
    133
    Vote Rating
    0
    FireGlow is on a distinguished road

      0  

    Default


    Code:
    getValue: function() {
            var values = {},
                items = this.getItems().items,
                ln = items.length,
                item, i;
    				alert('test');
    
    
            for (i = 0; i < ln; i++) {
                item = items[i];
                if (item && item.isSlot) {
    				alert(item.getName());
    				alert(item.getValue());                values[item.getName()] = item.getValue();
                }
            }
    
    
            this._values = values;
    
    
            return this._values;
        }
    I had a look at the sencha code and added 2 alerts. One is telling me "player" and the other "null". So the value is null after selecting one, maybe this is a bug in sencha lib itself or do I have to use another method to get the selected value?

  3. #3
    Sencha User
    Join Date
    Apr 2008
    Posts
    133
    Vote Rating
    0
    FireGlow is on a distinguished road

      0  

    Default


    Well after scanning the source code of sencha touch, I found following code that works for me:
    Code:
    slots: [
    {
    title: 'Blue 1',
    name : 'playerB1',
    data : [
    {text: '50 KB/s', value: 50},
    {text: '100 KB/s', value: 100},
    {text: '200 KB/s', value: 200},
    {text: '300 KB/s', value: 300}
    ]
    }
    ],
    listeners: { change : { fn : function() { var items = this.getItems().items, ln = items.length, item, i, players = {}; for (i = 0; i < ln; i++) { item = items[i]; if (item && item.isSlot) { alert(item.getStore().getAt(item.selectedIndex).get(item.getValueField())); } }
    I worked this out based on the code already found in the getvalue() method combined with the store getValue():
    Code:
    //STORE:
    getValue: function() {
            var store = this.getStore(),
                record, value;
    
    
            if (!store) {
                return;
            }
    
    
            if (!this.rendered) {
                return this._value;
            }
    
    
            //if the value is ever false, that means we do not want to return anything
            if (this._value === false) {
                return null;
            }
    
    
            record = store.getAt(this.selectedIndex);
    
    
            value = record ? record.get(this.getValueField()) : null;
            this._value = value;
    
    
            return value;
        }
    I guess this is the reason why I got null for the normal call:

    Code:
     
          if (this._value === false) {            return null;
            }

  4. #4
    Sencha User
    Join Date
    Feb 2012
    Location
    Switzerland
    Posts
    20
    Vote Rating
    0
    ofranky is on a distinguished road

      0  

    Default


    Hi

    I'm struggling with the same problem. Thanks for sharing your solution but i think the detour over the store is a bit tedious...
    Can someone of the Sencha Team please tell us how you would get the value of the slot that was picked?

    And also, if I didn't miss something there is no event to listen to that fires when the picker is closed (by clicking on 'done'...). Do I have to listen for the button-tap event of the 'done'-button for that case?
    Thanks.

  5. #5
    Sencha User
    Join Date
    Feb 2012
    Location
    Switzerland
    Posts
    20
    Vote Rating
    0
    ofranky is on a distinguished road

      0  

    Default


    *push* (sorry for that)

  6. #6
    Sencha User
    Join Date
    Feb 2012
    Location
    Switzerland
    Posts
    20
    Vote Rating
    0
    ofranky is on a distinguished road

      0  

    Default


    For me it turned out to be a problem to use a name for the slots...?!? Without a name it works. To get the chosen value I now use: this.picker.getValues().null

Thread Participants: 1