How to replace element with my GWT widget?

后端 未结 5 456
慢半拍i
慢半拍i 2021-02-02 01:46

Is it possible to replace known html element with my widget component? (Emphasis on the word \'replace\', I don\'t want to put the widget in th

5条回答
  •  伪装坚强ぢ
    2021-02-02 02:02

    I agree with markovuksanovic - you should consider DOM manipulation on a "higher level". For example, the functionality you need is provided via the InsertPanel interface, which FlowPanel implements:

    FlowPanel mainPanel = new FlowPanel();
    Hyperlink link = new Hyperlink("Something cool");
    mainPanel.add(link);
    mainPanel.add(new Label("Bla bla"));
    
    // Now we want to replace the first element of the FlowPanel with a TextBox
    // We can do that by its index...
    mainPanel.remove(0);
    
    // ...or Widget instance
    mainPanel.remove(link);
    
    // Now we add a TextBox at the beginning
    mainPanel.add(new TextBox(), 0);
    

    As you can see, this code is much more readable (at least to me), you don't manipulate the DOM directly (referencing via ids and such). Of course, there are places where direct DOM manipulation is beneficial (optimizing, most notably), but for the most part, I'd avoid juggling Elements, ids and such :) (at least in GWT)

提交回复
热议问题