我的目标是通过按下按钮将新列添加到现有的DefaultDataTable。我可以通过这种方式向表中添加列(不按任何按钮)
columns.add(new PropertyColumn(new Model<String>("First Name"), "name.first"));
一切正常。该表看起来像这样
First Name Last Name
---------- ---------
Novak Djokovic
Rafael Nadal
David Ferrer
当我尝试通过按下按钮添加新列时出现问题。期望的结果将是这个
First Name Last Name Nick Name
---------- --------- ---------
Novak Djokovic Nole
Rafael Nadal Rafa
David Ferrer Ferru
但我得到了这个
First Name Last Name
---------- ---------
Novak Djokovic Nole
Rafael Nadal Rafa
David Ferrer Ferru
如您所见,新列标题不会出现,我需要显示它。这里有html
<html xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd">
<head>
<title>DataTable Example</title>
</head>
<body>
<form wicket:id="form">
<input wicket:id="submit" type="submit" value="submit"/>
</form>
<table wicket:id="datatable"></table>
</body>
</html>
和代码:
public class DataTablePage {
final UserProvider userProvider = new UserProvider();
DefaultDataTable table;
List<IColumn<?>> columns = new ArrayList<IColumn<?>>();
public DataTablePage() {
columns.add(new PropertyColumn(new Model<String>("First Name"), "name.first"));
columns.add(new PropertyColumn(new Model<String>("Last Name"), "name.last"));
table = new DefaultDataTable("datatable", columns, userProvider, 10);
table.setOutputMarkupId(true).setOutputMarkupPlaceholderTag(true);
add(table);
Form form = new Form("form");
add(form);
form.add(new AjaxButton("submit") {
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
columns.add(new PropertyColumn(new Model<String>("Nick Name"), "name.nick"));
if (target != null)
target.add(table);
}
@Override
protected void onError(AjaxRequestTarget target, Form<?> form) {}
});
}
public class UserProvider extends SortableDataProvider {
private List<Contact> contacts = new ArrayList<Contact>();
public UserProvider() {
contacts.add(new Contact(new Name("Novak", "Djokovic", "Nole")));
contacts.add(new Contact(new Name("Rafael", "Nadal", "Rafa")));
contacts.add(new Contact(new Name("David", "Ferrer", "Ferru")));
}
@Override
public Iterator iterator(int first, int count) {
return contacts.subList(first, first + count).iterator();
}
@Override
public int size() {
return contacts.size();
}
public IModel<Contact> model(final Object object) {
return new AbstractReadOnlyModel<Contact>() {
@Override
public Contact getObject() {
return (Contact) object;
}
};
}
}
class Name implements Serializable {
private String firstName;
private String lastName;
private String nickName;
// Constructor, getters and setters
}
class Contact implements Serializable {
private final Name name;
// Constructor and getter
}
提前致谢。
答案 0 :(得分:1)
在Wicket 6中有所改进 - 见WICKET-4596。