【问题标题】:Wicket: Update DropDownChoice in DataViewWicket:在 DataView 中更新 DropDownChoice
【发布时间】:2021-02-28 23:04:32
【问题描述】:

我在 Wicket 中创建了一个下载页面。如您所见,它是一个 DataView,您可以在其中下载文件,具体取决于 id 列和 DropDownChoice 'version'。

因此,在版本 3 的 id 160 上点击“下载”应该下载 file160ver3.txt,而在版本 2 的 id 159 上点击“下载”应该下载 file159ver2.txt。不幸的是,更新 DropDownChoice 并没有反映在模型中。因此,单击“下载”按钮始终会下载相同版本的文件。由于我在 DropDownChoice 中默认为版本 2,因此它始终会下载此版本。

这是我的代码:

DropDownChoice<Integer> choice = new DropDownChoice<>("version", new Model<Integer>(2), List.of(1, 2, 3));
choice.add(new AjaxEventBehavior("change") {
    @Override
    protected void onEvent(AjaxRequestTarget target) {
        target.add();
        System.out.println(choice.getModelObject());    // doesn't change
    }
});
item.add(choice);

// The value of choice.getModelObject() doesn't change
DownloadLink download = new DownloadLink("download", getFile(p.getId(), choice.getModelObject()));
download.setOutputMarkupId(true);
item.add(download);

我错过了什么?如何更新 DropDownChoice?

更新及解决方案(根据斯文斯建议修改):

        choice.add(new AjaxFormComponentUpdatingBehavior("change") {
            @Override
            protected void onUpdate(AjaxRequestTarget target) {
                System.out.println(choice.getModelObject());
            }
        });
        item.add(choice);

        DownloadLink download = new DownloadLink("download", () -> {
            return getFile(p.getId(), choice.getModelObject());
        });

        // ...

private File getFile(int id, DropDownChoice<Integer> choice) throws FileNotFoundException, IOException {
    Integer version = choice.getModelObject();

谢谢。

... 这是完整的代码(下面是 Java 和 HTML):

public DownloadPage(PageParameters params) {
    List<PrefKey> prefKeys = db.getPrefKeys();
    DataView<PrefKey> dataView = getDataView(prefKeys);
    Form<Void> form = new Form<>("form");
    add(form);
    form.add(dataView);
}

private DataView<PrefKey> getDataView(List<PrefKey> prefKeys) {
    IDataProvider<PrefKey> provider = new ListDataProvider<>(prefKeys);
    DataView<PrefKey> dataView = new DataView<>("dbAsDataView", provider, 10) {
        private static final long serialVersionUID = 12345L;

        @Override
        protected void populateItem(Item<PrefKey> item) {
            PrefKey p = item.getModelObject();
            item.add(new Label("tdId", p.getId()));
            item.add(new Label("tdKey", p.getKey()));
            try {
                DropDownChoice<Integer> choice = new DropDownChoice<>("version", new Model<Integer>(2), List.of(1, 2, 3));
                choice.add(new AjaxEventBehavior("change") {
                    @Override
                    protected void onEvent(AjaxRequestTarget target) {
                        target.add();
                        System.out.println(choice.getModelObject());    // doesn't change
                    }
                });
                item.add(choice);

                DownloadLink download;
                // The value of choice.getModelObject() doesn't change
                download = new DownloadLink("download", getFile(p.getId(), choice.getModelObject()));
                download.setOutputMarkupId(true);
                item.add(download);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
    return dataView;
}

    <h1>Wicket Download</h1>
    <form wicket:id="form" action="">
        <table id="tblDataView" class="table table-striped table-hover">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Key</th>
                    <th>Version</th>
                    <th>Download</th>
                </tr>
            </thead>
            <tbody>
                <tr wicket:id="dbAsDataView">
                    <td wicket:id="tdId"></td>
                    <td wicket:id="tdKey"></td>
                    <td><select wicket:id="version"></select></td>
                    <td><input type="button" wicket:id="download" value="Download"></input></td>
                </tr>
            </tbody>
        </table>
    </form>

【问题讨论】:

    标签: wicket


    【解决方案1】:

    您必须使用 AjaxFormComponentUpdatingBehavior 将新选择的项目传输到 Java 组件(及其模型):

    choice.add(new AjaxFormComponentUpdatingBehavior("change") {
      @Override
      protected void onUpdate(AjaxRequestTarget target) {
      }
    });
    

    https://ci.apache.org/projects/wicket/guide/8.x/single.html#_ajaxformcomponentupdatingbehavior

    然后您的 downloadLink 也会动态调整为当前选择:

    download = new DownloadLink("download", () -> {
        return getFile(p.getId(), choice.getModelObject()
    });
    

    【讨论】:

    • 谢谢。不完全在那里,但我们越来越近了。我已经更新了上面的问题(参见“更新”)。不幸的是,我的 Downloadlink,现在将 DropDownChoice 作为参数,仍然在其模型中保留旧的默认值 (DownloadLink("download", ...,choice)。
    • 是的,就是这样。伟大的。写得很优雅。不幸的是,这很难谷歌;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多