I have a button with an iconcls that depends on a fieldvalue.
The icon is set correctly (when I alert the iconcls of the button, it's correct), but it's not shown.
Does anybody know what I am doing wrong?

Try to get it working by hard-coding the iconCls, that leaves less room for things to go wrong. Then try to do it dynamically.

Inspect the elements using a debugger like Firebug or the Chrome Developer Tools. See whether the iconCls turns up as a class on your component's elements. If it does then the problem is just writing a suitable CSS rule. The tools I mentioned will let you inspect the current CSS rules to give you an idea of what you need to do.

kathy_s

6 Jan 2012, 3:15 AM

Thanks for the quick response.

There's no gap, you just see the wrong icon. Even when I change the iconCls hardcoded, the new icon isn't shown.
When I inspect the element with firebug, it's the original iconCls, not the new one.

Any idea why?

skirtle

6 Jan 2012, 3:30 AM

Before I dig any further, which ExtJS version are you using?

kathy_s

6 Jan 2012, 4:22 AM

I use ext-4.0.7-gpl

skirtle

6 Jan 2012, 5:33 AM

OK, I gave it a shot and it worked fine for me. I've included my test case below, give it a try and see whether it works for you. If it does, it gives you a starting point for working out what the difference is.

Clearly putting 'button.' in your selector won't work as the iconCls is applied to the inner span, not the button element.

kathy_s

6 Jan 2012, 6:27 AM

You are right, like this it works.
The difference with what I am trying to do is that the icon is changed in the handler of the button and 'setIconCls' is called on the btn reference of the handler
When I do:

Ext.getCmp('buttonActivate').setIconCls
in stead of :

btn.setIconCls
it doesn't work anymore

Do you know why?
The id of the button is unique, so that is not the problem.
The object returned from Ext.getCmp, do I have to cast it to a button object? If yes, how?

skirtle

6 Jan 2012, 6:49 AM

I never use Ext.getCmp with static ids. Not because it's buggy, just because it's a use of global variables and they're short-term gain for long-term pain. It's an OO anti-pattern.

That said, Ext.getCmp does work. It will return you the component with that id. There's no such concept as casting in JavaScript. Given you aren't seeing an error message and it's updating the iconCls property it suggests that you are successfully calling setIconCls on a button, or possibly one of the other classes that support that method.

That leads me to wonder which button you're calling it on, because it appears it isn't the button you think it is. You claim the ids are unique but I'm a bit cynical about that, one reason why the use of static ids is a bad idea is that you quickly slip into scenarios where you think ids are unique when they aren't.

If things are still unexplained, try posting the code you use to create the button.

kathy_s

6 Jan 2012, 12:07 PM

You were absolutely right!
In a widget I created I had a button with the same id.
I already had a bad feeling about the large amount of global variables.
Being a java programmer, OO principles are not new to me, but I am new to extjs and javascript and I still have to learn a lot about it's syntax and possibilities. I guess I should do some research on setting up a good application structure and rearrange my whole app.