Hello,
I created a page to manage all users of my application. It contains a form for
creating and updating users and a table below to delete and edit users.
Here is the creation of the table and its columns:
List<IColumn<UserModel, String>> columns = new ArrayList<IColumn<UserModel,
String>>();
columns.add(new AbstractColumn<UserModel, String>(new
ResourceModel("datatable.action")) {
/** serialVersionUID */
private static final long serialVersionUID = 6160557212653571302L;
@Override
public void populateItem(Item<ICellPopulator<UserModel>> cellItem,
String componentId, IModel<UserModel> model) {
cellItem.add(new UsermanagementPanel(componentId, model));
}
});
columns.add(new PropertyColumn<UserModel, String>(new
ResourceModel("user.username"), "username", "username"));
columns.add(new PropertyColumn<UserModel, String>(new
ResourceModel("user.firstname"), "firstname", "firstname"));
columns.add(new PropertyColumn<UserModel, String>(new
ResourceModel("user.lastname"), "lastname", "lastname"));
columns.add(new PropertyColumn<UserModel, String>(new
ResourceModel("user.eMailaddress"), "eMailaddress", "eMailaddress"));
columns.add(new PropertyColumn<UserModel, String>(new
ResourceModel("user.phoneNumber"), "phoneNumber", "phoneNumber"));
new DefaultDataTable<UserModel, String>(this.id, initColumns(),
new
DAPDataProvider<UserModel>(userDao.getAllUsers(), "username"), 10);
As you can see every row of the column gets a UsermanagementPanel which only
contains 2 links. One for editing a user and one for deletion.
The link for editing should be an AjaxFallbackLink. It should set the user of
the row where the edit link was clicked as the default model of the form above
and add it to the AjaxRequestTarget for reloading the form.
Here is the Panel:
public class UsermanagementPanel extends Panel {
/** The user which should be changed or deleted. */
private UserModel userForAction;
/**
* The constructor which creates the panel with the edit and delete
link.
*
* @param id
* the id of the panel
* @param model
*/
public UsermanagementPanel(String id, IModel<UserModel> model) {
super(id, model);
addLinks();
userForAction = model.getObject();
log.info("Creates Panel for user: " +
userForAction.getUsername());
}
/**
* Add the edit and delete link.
*
* @param model
*/
private void addLinks() {
AjaxFallbackLink<UserModel> edit = new
AjaxFallbackLink<UserModel>("editUser") {
/** serialVersionUID */
private static final long serialVersionUID =
4045653268294938060L;
@Override
public void onClick(AjaxRequestTarget target) {
Usermanagement usermanagement =
(Usermanagement) getPage();
Form<UserModel> form =
usermanagement.getUserForm();
form.setDefaultModelObject(userForAction);
target.add(form);
}
};
Link<String> delete = new Link<String>("deleteUser") {
/** serialVersionUID */
private static final long serialVersionUID =
3217036727482973672L;
@Override
public void onClick() {
log.info("Triggering deleting of user" +
userForAction.getUsername());
UserDao userDao = new UserDaoImpl();
userDao.deleteUserById(userForAction.getUserID());
log.debug("Deleting complete, reload page");
setResponsePage(Usermanagement.class);
}
};
add(edit);
add(delete);
}
}
<wicket:panel xmlns:wicket="http://wicket.apache.org">
<a href="#" wicket:id="editUser" id="editLink">
<wicket:message key="datatable.editUser"/>
</a>
<a href="#" wicket:id="deleteUser" id="deleteLink">
<wicket:message key="datatable.deleteUser"/>
</a>
</wicket:panel>
In the table of the usermanagement page I can see 3 users and their
UsermanagementPanels. Now I click the edit link in the first row.
In the AjaxDebugWindow and in the browsers development tools, I can see that 3
Requests where send. One for each user. Then It renders the form by using ajax
with every user. So it renders the form 3 times. Finally I see the data of the
User in the 3rd row in the form.
If I click the edit link in the 2nd or 3rd row of the table, a
NullPointerException will be thrown, because the AjaxRequestTarget is null.
Can somebody explain why 3 Requests where send when I click only the link in
the first row? Why is the AjaxRequestTarget null for the other rows? I only
want one Request to show only the user of the row. What am I doing wrong?
Mit freundlichen Grüßen
Christoph Manig