简体   繁体   中英

How can I add a textfield dynamically after clicking a button?

Can anyone point me in the right direction here. Im wanting to have say 2 textfields initially then the user will be able to click 'add' button and another 2 textfields will be displayed (this needs to be dynamic as the user could click once or a 100 times for example)

Since it's wicket, it should be Java... I would retag this but my account is to new for that...

Put your initial Textfields models into a single wrapper-object and add this to a list (thus containing only one element) and display this list in a ListView. In the onClick Event of your Button, add another of these wrapper-objects to your list and refresh the ListView.

Something like

public class TwoTextFields {
    private IModel textFieldOne;
    private IModel textFieldTwo;

    [... constructor, getters setters here  ...]

and

public class MyPanel extends Panel {

   private List<TwoTextFields> list = new ArrayList<TwoTextFields>();

   public MyPanel(String id) {
       super(id);
       add( New ListView<TwoTextFields>("list", list) {

           @Override
           protected populateItem(Item<TwoTextFields> item) {
               add( new TextField("fieldOne", new PropertyModel(item, "textFieldOne");
               add( new TextField("fieldTwo", new PropertyModel(item, "textFieldTwo");
           }
      });
      add( new Button("button) {

           @Override
           protected void onClick() {
               list.add(new TwoTextFields());
           }
      });
}

I don't know if this compiles... It's just to give you the idea, didn't want to fire up eclipse for that...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM