Reputation: 39
I have printed a table using the DefaultDataTable object in Apache Wicket.
Now I want to add a link to each table cell.
I found this link which explained some of it, but I have a problem with the first method.
columns[0] = new TextFilteredPropertyColumn(new Model("Id"), "id", "id") {
// add the LinkPanel to the cell item
public void populateItem(Item cellItem, String componentId, IModel model) {
final Transaction transaction = (Transaction) model.getObject(cellItem);
cellItem.add(new TransactionList.LinkPanel(componentId, transaction));
}
};
private class LinkPanel extends Panel {
public LinkPanel(String id, Transaction transaction) {
super(id);
final String name = transaction.getId();
PageParameters param = new PageParameters("id=" + name);
BookmarkablePageLink link = new BookmarkablePageLink("link", TransactionDetail.class, param);
link.add(new Label("label", name));
add(link);
}
What is the transaction and what does the transaction do? What is the LinkPanel class? If there is an easier way, I'll love to know it!
Upvotes: 2
Views: 4148
Reputation: 1117
The "Transaction" is the object in the model of the cell (it is probably coming from the example as it is not coming from wicket). The "LinkPanel" class is an inner class that is also not from wicket. The purpose of this class is to make a panel that will contain a Link. You could add the link directly in the cell without having a panel like LinkPanel
columns[0] = new TextFilteredPropertyColumn(new Model("Id"), "id", "id") {
// add the LinkPanel to the cell item
public void populateItem(Item cellItem, String componentId, IModel model) {
cellItem.add(new Link<String>(componentId) {
@Override
public void onClick() {
setResponsePage(aPage.class);
//or do what you want when the link is clicked
}
@Override
public IMarkupFragment getMarkup() {
// display the content you like - access the properties of your object
return Markup.of("<div wicket:id='cell'>" + model.getObject() + "</div>");
}
});
// Populate your item here
}
};
But I would recommend to keep the inner panel (like LinkPanel) approach as I think it is a better practice then adding the link directly. By having your inner panel, you will be able to add label or images to the link very easily, something that would be much more difficult if you add the link directly.
Here is what I usualy do when I need to add links in a table (note that CallDetailRecord is not from wicket, it is just the object I used in this case) :
...
columns.add(new UserActionsColumn(new Model<String>(" ")));
...
private class UserActionsColumn extends AbstractColumn<CallDetailRecord> {
private static final long serialVersionUID = 1L;
public UserActionsColumn(IModel<String> displayModel) {
super(displayModel);
}
@Override
public void populateItem(Item<ICellPopulator<CallDetailRecord>> cellItem,
String componentId,
final IModel<CallDetailRecord> rowModel)
{
cellItem.add(new UserActionPanel(componentId, rowModel));
}
}
private class UserActionPanel extends Panel {
private static final long serialVersionUID = 1L;
public UserActionPanel(String id, final IModel<CallDetailRecord> model) {
super(id);
add(new AjaxLink<CallDetailRecord>("viewLink", model) {
private static final long serialVersionUID = 1L;
@Override
public void onClick(AjaxRequestTarget target) {
//actions to do when link is clicked
}
});
}
}
Hope this help
Upvotes: 6
Reputation: 5575
Transaction is just a Model class. It's an example, nothing povided by Wicket. The same applies to the LinkPanel class. It's an inner class of the model, extending the Wickel Panel class. A Panel class is needed to provide the markup for the Link.
Another option would be to use something like this:
columns[0] = new TextFilteredPropertyColumn(new Model("Id"), "id", "id") {
// add the LinkPanel to the cell item
public void populateItem(Item cellItem, String componentId, IModel model) {
cellItem.add(new AjaxEventBehaviour("onclick") {
protected void onEvent(final AjaxRequestTarget target) {
// Put Link logic here
}
});
// Populate your item here
}
};
to make the whole cell clickable (but requiring Ajax)...
Upvotes: 0