便门。我如何从列表视图文本字段中获取输入值
Wicket. How can i get input values from listview textfields
我有这个
private ArrayList<String> offersParams = new ArrayList<>();
这里我通过 offersParams 大小创建带有标签和文本字段的列表视图
final ListView listview = new ListView("listview", offersParams) {
public void populateItem(final ListItem item) {
item.add(new Label("label", item.getModel());
item.add(offersStringParams = new TextField<>("textField", Model.of(""));
offersStringParams.setOutputMarkupId(true);
}
};
form.add(listview);
这是我的html
<tr wicket:id="listview">
<td class="col-xs-6"><span class="control-label" wicket:id="label">label</span></td>
<td class="col-xs-6"><input class="form-control" wicket:id="textField" type="text"></td>
</tr>
在我需要从 textFields 获取所有输入值之后,但我只能通过此代码获取最后一个值
所以我需要帮助。我是检票口的新手,我不明白如何从文本字段中获取所有输入值。提交时 - 我需要将对标签和输入值放入 JSONObject
UPD
这是我提交的 btn,其中 inputValue - 它是来自文本字段的输入值之一
setOffer = new Button("setOffer") {
@Override
public void onSubmit() {
super.onSubmit();
try {
JSONObject jsonObject = new JSONObject();
for (int a = 0; a < offersParams.size(); a++) {
jsonObject.put(offersParams.get(a), inputValue);
}
您可以尝试使用 AjaxFormComponentUpdatingBehavior 并且必须将 offersStringParams 设为本地。
item.add(new Label("label", item.getModel());
final TextField<String> offersStringParams = new TextField<>("textField", Model.of("");
item.add(offersStringParams);
offersStringParams.setOutputMarkupId(true);
offersStringParams.add(new AjaxFormComponentUpdatingBehavior("onsubmit") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
String value = offersStringParams.getModelObject();
// do something with the value here
}
});
您可以像这样让您的 TextFields 直接写入地图:
private Map<String, String> offersValues = new HashMap<>();
final ListView listview = new ListView("listview", offersParams) {
public void populateItem(final ListItem item) {
item.add(new Label("label", item.getModel());
item.add(new TextField<>("textField", new PropertyModel(offersValues, item.getModelObject()));
}
在 #onSubmit()
中,您可以将地图转换为 JSONObject:
public void onSubmit() {
JSONObject jsonObject = new JSONObject(offersValues);
...
}
我有这个
private ArrayList<String> offersParams = new ArrayList<>();
这里我通过 offersParams 大小创建带有标签和文本字段的列表视图
final ListView listview = new ListView("listview", offersParams) {
public void populateItem(final ListItem item) {
item.add(new Label("label", item.getModel());
item.add(offersStringParams = new TextField<>("textField", Model.of(""));
offersStringParams.setOutputMarkupId(true);
}
};
form.add(listview);
这是我的html
<tr wicket:id="listview">
<td class="col-xs-6"><span class="control-label" wicket:id="label">label</span></td>
<td class="col-xs-6"><input class="form-control" wicket:id="textField" type="text"></td>
</tr>
在我需要从 textFields 获取所有输入值之后,但我只能通过此代码获取最后一个值
所以我需要帮助。我是检票口的新手,我不明白如何从文本字段中获取所有输入值。提交时 - 我需要将对标签和输入值放入 JSONObject
UPD 这是我提交的 btn,其中 inputValue - 它是来自文本字段的输入值之一
setOffer = new Button("setOffer") {
@Override
public void onSubmit() {
super.onSubmit();
try {
JSONObject jsonObject = new JSONObject();
for (int a = 0; a < offersParams.size(); a++) {
jsonObject.put(offersParams.get(a), inputValue);
}
您可以尝试使用 AjaxFormComponentUpdatingBehavior 并且必须将 offersStringParams 设为本地。
item.add(new Label("label", item.getModel());
final TextField<String> offersStringParams = new TextField<>("textField", Model.of("");
item.add(offersStringParams);
offersStringParams.setOutputMarkupId(true);
offersStringParams.add(new AjaxFormComponentUpdatingBehavior("onsubmit") {
@Override
protected void onUpdate(AjaxRequestTarget target) {
String value = offersStringParams.getModelObject();
// do something with the value here
}
});
您可以像这样让您的 TextFields 直接写入地图:
private Map<String, String> offersValues = new HashMap<>();
final ListView listview = new ListView("listview", offersParams) {
public void populateItem(final ListItem item) {
item.add(new Label("label", item.getModel());
item.add(new TextField<>("textField", new PropertyModel(offersValues, item.getModelObject()));
}
在 #onSubmit()
中,您可以将地图转换为 JSONObject:
public void onSubmit() {
JSONObject jsonObject = new JSONObject(offersValues);
...
}