How can I add a textfield dynamically after clicki

2019-09-15 19:57发布

问题:

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)

回答1:

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...