你需要一个ListView,因为你可以有面板类型,一个用listview添加逻辑的模型,至少2个表单,一个用于DropDown,用户在其中选择添加数据,最后另一个用于提交整个数据列表显示。您可以使用 AJAX 但可选
为了了解如何将转发器(ListView 是一个高级转发器)与表单组件一起使用,您可以检查here https://cwiki.apache.org/confluence/display/WICKET/ListView+and+other+repeaters了解他的基本用法,here http://wicketinaction.com/2008/10/building-a-listeditor-form-component/对于他与表单组件的使用,最后here https://cwiki.apache.org/confluence/display/WICKET/How+to+repaint+a+ListView+via+Ajax了解如何将其与 AJAX 结合使用。
顺便说一句,我有一个例子,这只是代码的关键部分。
这是 ListView.class 上的 populateItem 方法
@Override
protected void populateItem(ListItem<ListViewModel> item) {
item.add(new TextField<Integer>("quantity", new PropertyModel<Integer>(item.getDefaultModelObject(),
"averageQuantity"));
item.add(new TextField<Integer>("position", new PropertyModel<Integer>(item.getDefaultModelObject(), "order"))
.add(new IntegerValidator()));
item.add(new Label("description", item.getModelObject().getName()));
item.setOutputMarkupId(true);
}
在其他地方,您应该将下拉列表添加到他自己的表单中,然后在提交时操作 listView 对象
// I use a AjaxButton to perform the user submit if you don't
// want use it, you should reload the entire page
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
//redraw the parent of the list view
target.add(theContainerOfTheListView);
//the submited model of the dropdown
ListViewModel item = form.getObject();
List<ListViewModel> list = listViewObject.getObject();
list.add(item);
//you could sort the list object in order to sort the listViewObject
}
UPDATE:在将新项目添加到列表视图之前,您应该提交列表视图的表单组件,如果不这样做,您将丢失用户更改