Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

How to modify an attribute on a HTML tag

To modify a tag attribute, there is a nifty new class called wicket.AttributeModifier.

...

In your HTML you would have something like this:

panelnoformat
Code Block
html
html

 <span wicket:id="alarm" class="unkown"/>Alarm Status</span>

And in your java something like this:

unmigrated-wiki-markup
Panel
Code Block

 Label alarmLabel = new Label("alarm", new PropertyModel(test,  "alarmState"));
 alarmLabel.add(new AttributeModifier("class", new Model() {
   public Object getObject(final Component component) {
         String cssClass;
         if ( test.getAlarmState() ) 
             cssClass = "alarm";
         } else {
             cssClass="noAlarm";
         }
         return cssClass;
   }
 }));
 add(alarmLabel);

If the attribute is not already present it will not, by default, be created. To quote the Javadoc: "If an attribute is not in the markup, this modifier will add an attribute to
the tag only if addAttributeIfNotPresent is true and the replacement value is
not null."