ciochPep
ciochPep

Reputation: 2592

How to force GWT to add unique ID to each DOM element?

I am having a GWT app, I would like to add ID to each element automaticlly if it's impossible, what would be the fastest way to do it manually?

Upvotes: 2

Views: 2174

Answers (4)

Suresh Atta
Suresh Atta

Reputation: 121998

Yes its Possible :

First way:

Button b = new Button();
DOM.setElementAttribute(b.getElement(), "id", "my-button-id")

Second way :

FlowPanel panel = new FlowPanel();
panel.getElement().setId("my-flowpanel-id");

If you want to do assign all the id's attached to DOM you can do

Iterator<Widget> iterator = RootPanel.get().iterator();

while(iterator.hasNext()) {
    Widget w = iterator.next();
     w.getElement().setId("id");
}

Upvotes: 1

appbootup
appbootup

Reputation: 9537

I am not so sure about performance issues. How would adding a dew ids in a form of 10-20 widgets cause performance loss?

To automagically set debug ids on widgets you need to include following in your module.gwt.xml file.

<!-- Enable debug ID. -->
<inherits name="com.google.gwt.user.Debug"/>
<set-property name="gwt.enableDebugId" value="true"/>

Upvotes: 3

Abhijith Nagaraja
Abhijith Nagaraja

Reputation: 3380

If you want to have an element to have an Id always, you can go with ensureDebugId of UI Object class. It make sure that, your element have an Id set before attaching it to the dom.

Upvotes: 1

Andrei Volgin
Andrei Volgin

Reputation: 41089

You can create your own Widget class that extends GWT Widget and sets an id to each one:

public class myWidget extends Widget {

    public myWidget(String id) {
        super();
        getElement().setId("id");
    }

    public void setId(String id) {
        // Use this method if you use Ui:Binder
        getElement().setId("id");
    }
}

You can, obviously, extend other classes instead of Widget if you don't need id on all widgets, like FlowPanel, Button, etc.

Upvotes: 1

Related Questions