How to get a segment button without using its ID in sencha touch?

223 views Asked by At
Code:

     items: [{
            xtype: 'segmentedbutton',
            disabled: true,
            //id:'segmentWidget',
                    items: [
                        {
                            text: 'text1',
                            flex: 1,
                        },
                        {
                            text: 'text2',
                            flex: 1
                        },
                        {
                            text: 'text2',
                            flex: 1
                        }
                    ]

        }]

Ext.getCmp('segmentWidget').setPressedButtons(0);

How can i do this without using ID

Thanks

1

There are 1 answers

0
Andrea Casaccia On

If you don't want to use id to select components in Sencha (and it is a good idea to do that when possible to keep things reusable and avoiding polluting the global ids space) you have several options.

(bold part is taken from Sencha docs, the rest is mine)

  1. Ext.Container.getComponent() method: Examines this container's items property and gets a direct child component of this container. Basically you pass in a string which should match the id or itemIdproperty of one of the items in that container.

  2. Ext.Container.query() method: Retrieves all descendant components which match the passed selector. Executes an Ext.ComponentQuery.query using this container as its root. This returns an array of Ext.Component. Take a look at Ext.ComponentQuery.querydocs to understand which queries you can do. In short, you can simply select by xtype, in your case you can do: fatherComponent.query('segmentedbutton')[0], or by another property, for example: fatherComponent.query('[text=text1]')[0]

  3. Ext.Component.up() method: Walks up the ownerCt axis looking for an ancestor Container which matches the passed simple selector.