我如何动态地添加一个文本框点击一个按钮后? [英] How can I add a textfield dynamically after clicking a button?
问题描述
任何人都可以点我在这里正确的方向。林希望有发言权2文本框最初那么用户将能够点击添加按钮,另外2个文本框将显示(这个需要是动态的,用户可以单击一次或例如100次)
Can anyone point me in the right direction here. Im wanting to have say 2 textfields initially then the user will be able to click 'add' button and another 2 textfields will be displayed (this needs to be dynamic as the user could click once or a 100 times for example)
推荐答案
由于它的小门,它应该是Java的...我会重新标记这一点,但我的帐户是新为...
Since it's wicket, it should be Java... I would retag this but my account is to new for that...
把你的初始文本框模型成一个单一的包装对象,这添加到列表(因此只包含一个元素),并显示该列表中的一个ListView。在按钮的onclick事件,另外这些包装对象添加到您的列表,并刷新ListView控件。
Put your initial Textfields models into a single wrapper-object and add this to a list (thus containing only one element) and display this list in a ListView. In the onClick Event of your Button, add another of these wrapper-objects to your list and refresh the ListView.
像
public class TwoTextFields {
private IModel textFieldOne;
private IModel textFieldTwo;
[... constructor, getters setters here ...]
和
public class MyPanel extends Panel {
private List<TwoTextFields> list = new ArrayList<TwoTextFields>();
public MyPanel(String id) {
super(id);
add( New ListView<TwoTextFields>("list", list) {
@Override
protected populateItem(Item<TwoTextFields> item) {
add( new TextField("fieldOne", new PropertyModel(item, "textFieldOne");
add( new TextField("fieldTwo", new PropertyModel(item, "textFieldTwo");
}
});
add( new Button("button) {
@Override
protected void onClick() {
list.add(new TwoTextFields());
}
});
}
我不知道这是否编译......它只是给你的想法,不想火起来蚀为...
I don't know if this compiles... It's just to give you the idea, didn't want to fire up eclipse for that...
这篇关于我如何动态地添加一个文本框点击一个按钮后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!