Wicket dynamically add components to form

半腔热情 提交于 2019-12-05 16:18:29
osdamv

You need a ListView because you can n have type of panels , a model where you add the logic with the listview, at least 2 forms , one for the DropDown where the user select the data too add and finally another to submit the data for entire ListView. You could use AJAX but is optional

in order to know how to use repeaters(the ListView is a advanced repeater) with form components you can check here to know his basic usage, here for his usage with form components and finally here to know how to use it with AJAX.

BTW i have a example, here is just the critical part of the code.

this is populateItem method on ListView.class

 @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);
 }

in other place you should add the dropdown to his own form and then on submit manipulate the listView object for example

 // 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: before add the new item to the listview you should submit the form components of the list view, if you don´t do it you going to loose the user changes

You don't have to add/remove components from the page. It's possible to make components dynamically visible/invisible. Use setVisible(false)to make the components invisible. This would be their initial state.

You also need to call setOutputMarkupPlaceholderTag(true) on the components you want to dynamically hide/show.

When you need the components to be visible, call setVisible(true). This can be triggered by an Ajax button's onClick() method.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!