java Wicket 动态添加组件到表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13419494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wicket dynamically add components to form
提问by DavidVdd
I'm having trouble adding components to a form dynamically. What I'm trying to do is: Give the user a drop-down list with items he can choose like name, age, ...
我在动态地将组件添加到表单时遇到了问题。我想要做的是:给用户一个下拉列表,其中包含他可以选择的项目,如姓名、年龄、......
When a user presses add: there comes a (label + inputbox) in 1 component wich allows him to put in the value. You might think I could hide those components wich aren't selected but the user is also able to add values to the drop-down list.
当用户按下添加时:1 个组件中有一个(标签 + 输入框),允许他输入值。您可能认为我可以隐藏那些未选择的组件,但用户也可以将值添加到下拉列表中。
The problem I have is how to add and remove components (label+ inputbox) without having wicket:ids in the HTML?
我的问题是如何在 HTML 中没有 wicket:ids 的情况下添加和删除组件(标签+输入框)?
This is what I'm trying to add:
这就是我要添加的内容:
<wicket:panel>
<div wicket:id="hldValue">
<label wicket:id="lblValue"></label>
<input type="text" wicket:id="value"/>
</div>
</wicket:panel>
The problem I have here is that the ID is always the value I want to name dynamically. Is using dynamic HTML to create this component a good idea? I'm overriding getMarkupResourceStream
and getCacheKey
to achieve this. Still I feel this is not the right way. Any other suggestions?
我在这里遇到的问题是 ID 始终是我想动态命名的值。使用动态 HTML 创建这个组件是个好主意吗?我压倒一切getMarkupResourceStream
并getCacheKey
实现这一目标。我仍然觉得这不是正确的方法。还有其他建议吗?
回答by 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
您需要一个 ListView,因为您可以拥有面板类型,一个模型,您可以在其中添加带有列表视图的逻辑,至少有 2 个表单,一个用于 DropDown,其中用户也选择数据添加,最后另一个用于提交整个数据列表显示。你可以使用 AJAX 但它是可选的
in order to know how to use repeaters(the ListView is a advanced repeater) with form components you can check hereto know his basic usage, herefor his usage with form components and finally hereto know how to use it with AJAX.
为了知道如何使用中继器(ListView控件是一种先进的中继器)与表格组件,您可以查看这里了解他的基本用法,在这里,他与表单组件的使用,最后在这里了解如何使用AJAX使用它。
BTW i have a example, here is just the critical part of the code.
顺便说一句,我有一个例子,这里只是代码的关键部分。
this is populateItem method on ListView.class
这是 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);
}
in other place you should add the dropdown to his own form and then on submit manipulate the listView object for example
在其他地方,您应该将下拉列表添加到他自己的表单中,然后在提交时操作 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: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
更新:在将新项目添加到列表视图之前,您应该提交列表视图的表单组件,如果您不这样做,您将失去用户更改
回答by Andrew Fielden
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.
您不必在页面中添加/删除组件。可以使组件动态可见/不可见。使用setVisible(false)
使组件不可见。这将是他们的初始状态。
You also need to call setOutputMarkupPlaceholderTag(true)
on the components you want to dynamically hide/show.
您还需要调用setOutputMarkupPlaceholderTag(true)
要动态隐藏/显示的组件。
When you need the components to be visible, call setVisible(true)
. This can be triggered by an Ajax button's onClick()
method.
当您需要组件可见时,请调用setVisible(true)
. 这可以由 Ajax 按钮的onClick()
方法触发。