【问题标题】:Wicket. How can i get input values from listview textfields便门。如何从 listview 文本字段中获取输入值
【发布时间】:2016-07-04 07:13:09
【问题描述】:

我有这个

private ArrayList<String> offersParams = new ArrayList<>();

在这里,我通过 offerParams 大小创建带有标签和文本字段的列表视图

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

【问题讨论】:

    标签: listview input get wicket


    【解决方案1】:

    您可以让您的 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);
        ...
    }
    

    【讨论】:

    • 哦,谢谢你!它真的有效!我想地图,但不会写这个结构)
    【解决方案2】:

    您可以尝试使用 AjaxFormComponentUpdatingBehavior,并且必须将 offerStringParams 设为本地。

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

    【讨论】:

    • 谢谢,但我看到它不起作用,这个构造得到空对象 - String value = offersStringParams.getModelObject(); System.out.println(值); // 它是 - []
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多