How to get currently active item`s index number (and not active item`s id) on a card layout on Extjs?

Mehdi Fanai picture Mehdi Fanai · Jun 1, 2011 · Viewed 9.9k times · Source

How do I get the currently active item's index number (and not active item's id) on a card layout? The following code will return active item's id:

     Ext.getCmp('my-wizard').getLayout().activeItem.id];

What if I don't want to define an id for my component items and I just want to access active item's index number?

Answer

Sean Adkinson picture Sean Adkinson · Jun 1, 2011

I couldn't find a built-in quick and easy way, but the following would work:

var wiz = Ext.getCmp('my-wizard');
var activeItem = wiz.getLayout().activeItem;
var activeIndex = wiz.items.indexOf(activeItem);

If this was something that you wanted to do often, you could add it to the CardLayout prototype:

Ext.override(Ext.layout.CardLayout, {
    getActiveIndex: function() {
        return this.container.items.indexOf(this.activeItem);
    }
});

Then you could use it with:

var activeIndex = Ext.getCmp('my-wizard').getLayout().getActiveIndex();