You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by "login.masater.de" <lo...@masater.de> on 2014/08/13 18:40:27 UTC

Dynamically adding/removing panels to existing form

Hi there,
I'm new to wicket and got a problem, I searched google and the mailing list for
2 days now but can't find what I'm searching for, here is an reduced example of
what I'm trying to do.
This is very simplified and not the domain I'm working in, this is just used for
ease. I'm using wicket:6:16:0.
The Example in short: I have a person with multiple addresses. For the person
there is a form with a textfield for the name. Under that textfield is an ajax
link to add a panel containing an form for an address. The background-model is a
class person with a name string and a list of addresses and an address class
with a street/city string.
My problem now is how i can dynamically add addresses to that person.

Models:
===Person.java===
...
private String name;
private List<Address> addresses;
//getter/setter
...
===/Person.java===
===Address.java===
...
private String street;
private String city;
...
//getter/setter
....
===/Address.java===

===AddressPanel.java===
...
public Address(String id, IModel<Address> model) {
  super(id, model);
  SetDefaultModel(model);
  Form<?> form = new Form<Void>("form");
  TextField<String> street = new TextField<String>("street");
  form.add(street);
  TextField<String> city = new TextField<String>("city");
   form.add(city);
   add(city);
}
===/AddressPanel.java===
===AddressPanel.html===
<htmlstuff...>
  <wicket:panel>
    <form wicket:id="form">
      <input type="text" wicket:id="street"/>
      <input type="text" wicket:id="city"/>
    </form>
  </wicket:panel>
</htmlstuff...>
===/AddressPanel.html===

===PersonPage.html===
<htmlstuff...>
    <form wicket:id="form">
      <input type="text" wicket:id="name"/>
      <div wicket:id="addresses"/>
      <a href="#" wicket:id="addAddress">add address</a>
    </form>
</htmlstuff...>
===/PersonPage.html===
===PersonPage.java===
public class PersonPage extends WebPage {
    Person person;
    public PersonPage() {
      person = new Person();
      setDefaultModel(new CompoundPropertyModel<Person>(person);
      Form<?> form = new Form<Void>("form");
      TextField<String> name = new TextField<String>("name");
      form.add(name);
      add(form);

     //And here the troube begins, do I use an ListView? If so for AddressPanel
I think, but which model do I give to it?

      AjaxSubmitLink addLink = new AjaxSubmitLink("addAddress", form) {
        @Override
        protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
          // Do I now create a new Address or AddressPanel? If I create an
AddressPanel which Model do I use? How is the Address appended to the addresses
list in Person?
        }
      }
   }
}

many thanks in advance
Gunnar

Re: Dynamically adding/removing panels to existing form

Posted by "login.masater.de" <lo...@masater.de>.
worked like a charm and so easy :D

thank you

> Sven Meier <sv...@meiers.net> hat am 13. August 2014 um 20:31 geschrieben:
>
>
> A ListView should do fine:
>
> form.add(new ListView("addresses", new
> PropertyModel<List<Address>>(person, "adresses")) {
> populateItem(ListItem<Address> item) {
> item.add(new AddressPanel("address", item.getModel()));
> }
> });
>
> AjaxSubmitLink addLink = new AjaxSubmitLink("addAddress", form) {
> @Override protected void onSubmit(AjaxRequestTarget target, Form<?>
> form) {
> person.newAddress();
>
> target.add(getPage());
> }
> } ;
>
>
> Sven
>
> On 08/13/2014 06:40 PM, login.masater.de wrote:
> > Hi there,
> > I'm new to wicket and got a problem, I searched google and the mailing list
> > for
> > 2 days now but can't find what I'm searching for, here is an reduced example
> > of
> > what I'm trying to do.
> > This is very simplified and not the domain I'm working in, this is just used
> > for
> > ease. I'm using wicket:6:16:0.
> > The Example in short: I have a person with multiple addresses. For the
> > person
> > there is a form with a textfield for the name. Under that textfield is an
> > ajax
> > link to add a panel containing an form for an address. The background-model
> > is a
> > class person with a name string and a list of addresses and an address class
> > with a street/city string.
> > My problem now is how i can dynamically add addresses to that person.
> >
> > Models:
> > ===Person.java===
> > ...
> > private String name;
> > private List<Address> addresses;
> > //getter/setter
> > ...
> > ===/Person.java===
> > ===Address.java===
> > ...
> > private String street;
> > private String city;
> > ...
> > //getter/setter
> > ....
> > ===/Address.java===
> >
> > ===AddressPanel.java===
> > ...
> > public Address(String id, IModel<Address> model) {
> > super(id, model);
> > SetDefaultModel(model);
> > Form<?> form = new Form<Void>("form");
> > TextField<String> street = new TextField<String>("street");
> > form.add(street);
> > TextField<String> city = new TextField<String>("city");
> > form.add(city);
> > add(city);
> > }
> > ===/AddressPanel.java===
> > ===AddressPanel.html===
> > <htmlstuff...>
> > <wicket:panel>
> > <form wicket:id="form">
> > <input type="text" wicket:id="street"/>
> > <input type="text" wicket:id="city"/>
> > </form>
> > </wicket:panel>
> > </htmlstuff...>
> > ===/AddressPanel.html===
> >
> > ===PersonPage.html===
> > <htmlstuff...>
> > <form wicket:id="form">
> > <input type="text" wicket:id="name"/>
> > <div wicket:id="addresses"/>
> > <a href="#" wicket:id="addAddress">add address</a>
> > </form>
> > </htmlstuff...>
> > ===/PersonPage.html===
> > ===PersonPage.java===
> > public class PersonPage extends WebPage {
> > Person person;
> > public PersonPage() {
> > person = new Person();
> > setDefaultModel(new CompoundPropertyModel<Person>(person);
> > Form<?> form = new Form<Void>("form");
> > TextField<String> name = new TextField<String>("name");
> > form.add(name);
> > add(form);
> >
> > //And here the troube begins, do I use an ListView? If so for AddressPanel
> > I think, but which model do I give to it?
> >
> > AjaxSubmitLink addLink = new AjaxSubmitLink("addAddress", form) {
> > @Override
> > protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
> > // Do I now create a new Address or AddressPanel? If I create an
> > AddressPanel which Model do I use? How is the Address appended to the
> > addresses
> > list in Person?
> > }
> > }
> > }
> > }
> >
> > many thanks in advance
> > Gunnar
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>

Re: Dynamically adding/removing panels to existing form

Posted by Sven Meier <sv...@meiers.net>.
A ListView should do fine:

form.add(new ListView("addresses", new 
PropertyModel<List<Address>>(person, "adresses")) {
     populateItem(ListItem<Address> item) {
       item.add(new AddressPanel("address", item.getModel()));
     }
});

AjaxSubmitLink addLink = new AjaxSubmitLink("addAddress", form) {
     @Override protected void onSubmit(AjaxRequestTarget target, Form<?> 
form) {
          person.newAddress();

          target.add(getPage());
     }
} ;


Sven

On 08/13/2014 06:40 PM, login.masater.de wrote:
> Hi there,
> I'm new to wicket and got a problem, I searched google and the mailing list for
> 2 days now but can't find what I'm searching for, here is an reduced example of
> what I'm trying to do.
> This is very simplified and not the domain I'm working in, this is just used for
> ease. I'm using wicket:6:16:0.
> The Example in short: I have a person with multiple addresses. For the person
> there is a form with a textfield for the name. Under that textfield is an ajax
> link to add a panel containing an form for an address. The background-model is a
> class person with a name string and a list of addresses and an address class
> with a street/city string.
> My problem now is how i can dynamically add addresses to that person.
>
> Models:
> ===Person.java===
> ...
> private String name;
> private List<Address> addresses;
> //getter/setter
> ...
> ===/Person.java===
> ===Address.java===
> ...
> private String street;
> private String city;
> ...
> //getter/setter
> ....
> ===/Address.java===
>
> ===AddressPanel.java===
> ...
> public Address(String id, IModel<Address> model) {
>    super(id, model);
>    SetDefaultModel(model);
>    Form<?> form = new Form<Void>("form");
>    TextField<String> street = new TextField<String>("street");
>    form.add(street);
>    TextField<String> city = new TextField<String>("city");
>     form.add(city);
>     add(city);
> }
> ===/AddressPanel.java===
> ===AddressPanel.html===
> <htmlstuff...>
>    <wicket:panel>
>      <form wicket:id="form">
>        <input type="text" wicket:id="street"/>
>        <input type="text" wicket:id="city"/>
>      </form>
>    </wicket:panel>
> </htmlstuff...>
> ===/AddressPanel.html===
>
> ===PersonPage.html===
> <htmlstuff...>
>      <form wicket:id="form">
>        <input type="text" wicket:id="name"/>
>        <div wicket:id="addresses"/>
>        <a href="#" wicket:id="addAddress">add address</a>
>      </form>
> </htmlstuff...>
> ===/PersonPage.html===
> ===PersonPage.java===
> public class PersonPage extends WebPage {
>      Person person;
>      public PersonPage() {
>        person = new Person();
>        setDefaultModel(new CompoundPropertyModel<Person>(person);
>        Form<?> form = new Form<Void>("form");
>        TextField<String> name = new TextField<String>("name");
>        form.add(name);
>        add(form);
>
>       //And here the troube begins, do I use an ListView? If so for AddressPanel
> I think, but which model do I give to it?
>
>        AjaxSubmitLink addLink = new AjaxSubmitLink("addAddress", form) {
>          @Override
>          protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
>            // Do I now create a new Address or AddressPanel? If I create an
> AddressPanel which Model do I use? How is the Address appended to the addresses
> list in Person?
>          }
>        }
>     }
> }
>
> many thanks in advance
> Gunnar


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org