DonkeyKong
DonkeyKong

Reputation: 818

ExtJs select component by id starting with a string

i need to create a component dynamically by a button click. My restrictions are:

  1. It's going to has an Id starts with a fixed string like 'myComp_' and followed by a random number
  2. At any time there will be only one component that has an id starts with 'myComp_xxx'

So before creating the component i have to check if there's any created before and remove it... My problem starts here. Ext.getCmp() wants the specific id. But i only have that fixed string : myComp_...

Is there any way to get the component created before???

Thanks in advance and sorry about my English.

Upvotes: 7

Views: 33646

Answers (5)

JonnyRaa
JonnyRaa

Reputation: 8038

Sounds like you should be using the normal component query stuff - in general it is not a good idea to use id. You can query by xtype and by itemId (which you can assign manually).

The following

Ext.ComponentQuery.query('grid form');  

would find all things with xtype grid that have forms inside them somewhere.

Ext.ComponentQuery.query('grid #okButton');  

whereas the # here is saying look for grids that have something with itemId 'okButton' in them.

You can nest this to whatever level you need and use other operators to be more specific and as someone else has rightly pointed out you can use up and down on components to do this relative to the current component. Its worth noting that rather than getting an array back with all the results you just get the first one when you use up and down.

See the documentation for more information this.

Also see point 6 on this list of bad practices to avoid for more of the why

Upvotes: 2

dougajmcdonald
dougajmcdonald

Reputation: 20047

Another alternative if you know 'where' your component is going to be created is to use the up()/down() methods.

E.g. if you have a button and want to get the form it's contained within inside a click handler you can do something like this:

function myClickHandler(btn) {
     var form = btn.up('form');

     //do something with form 
}

Upvotes: 0

Varun Achar
Varun Achar

Reputation: 15109

For ExtJs 4.X use Ext.ComponentQuery.query('*[id^=myComp_xxx]');

For ExtJs 3.X you can either use the following

var el = Ext.query('*[id^=myComp_xxx]');
var cmp = Ext.getCmp(el.id);

Or (this one i haven't tried personally, but i think it should work) if the component is a child of a component that you can access, then:

var el = parentComp.find("action","btn");

and set a property called action : btn in the button config.

Upvotes: 8

Johan Haest
Johan Haest

Reputation: 4421

You need to use this: Ext.getCmp(id)

Ext.getCmp("myComp_xxx");

Upvotes: 2

Arun P Johny
Arun P Johny

Reputation: 388316

I think what you are looking for is DomQuery.

Ex:

Ext.query("*[id^=myComp_xxx]")

Upvotes: 6

Related Questions