nashuald
nashuald

Reputation: 825

Wicket: How is the better way to use repeated forms using ajax?

I have the following classes:

public class Group {
    private long id;
    private String name;
    ...
    private List<Person> members;
}

public class Person {
    private long id;
    private String name;
    private String comments;
}

I have the following wicket panel:

<wicket:panel>
   <div>
     <!-- Group data -->
   </div>
   <form wicket:id="group">
   <table>
      <tbody wicket:id="container">
         <tr wicket:id="members">
             <td wicket:id="personId"></td>
             <td wicket:id="name"></td>
             <td><input type="text" name="comments" wicket:id="comments" value="" /></td>
         </tr>
      </tbody>
   </table>
   <input type="submit" value="save" >
   </form>      
</wicket:panel>

In the other hand, I have a WebPage that have filtering functions and show me the differents groups:

<html>
<body>
   <form wicket:id="filterOptions">
     <!-- filter options -->
   </form>
   <div wicket:id="resultingGroups">
       <!-- contains the groups that matching the filters -->
   </div>
</body>
</html>

I want to update the groups doing ajax submits. I tried using ListView, but, I dont know why, when I click 'save' button the changes are reflected on in the first group panel, if I update any other group and click 'save' the Group object is not updated.

I research a lot and found that ListView is not a good choice for what I want to do, but I don't know which View is the correct one.

Filter form works with ajax button too, and works fine.

I am very new on Wicket.

Thanks in advance!!

Upvotes: 0

Views: 713

Answers (2)

nuno
nuno

Reputation: 1791

Actually ListView, DataView and other repeaters are good for what you want to do.

  • the ListView should be populated by a Model with a collection of the items you want
    ListModel peopleModel = //... DAO access (database) 
    ListView peopleList = new ListView("resultingGroups", peopleModel){ ... };
  • in order to repaint (refresh the ListView) you should enclose peopleList in a container with the .setOutputMarkupId(true); (so that Wicket is able to find that container through ajax).
...
    WebMarkupContainer peopleContainer = new WebMarkupContainer("container");
    peopleContainer.setOutputMarkupId(true);
    peopleContainer.add(peopleList);
...
  • ...and then whenever you want to refresh the results shown in the List you need to set the new results to the model (peopleModel) and repaint the container that has the ListView ((*))
public void onEvent(AjaxRequestTarget target) {
    //... on button click or something
    newPeopleList = // ... DAO
    peopleModel.setObject(newPeopleList);

    target.add(peopleContainer); // (*) repaint
}

Hope that helped. More examples here

https://cwiki.apache.org/WICKET/how-to-repaint-a-listview-via-ajax.html

Upvotes: 1

Related Questions