Update DropDownChoice in DataView

Bernie

DownloadPage

I made a download page in Wicket. As you can see, it's a DataView where you can download a file, depending on the id column and the DropDownChoice 'version'.

So clicking 'Download' on id 160 with version 3 should download file160ver3.txt, while on id 159 with version 2 it should download file159ver2.txt. Unfortunately updating the DropDownChoice doesn't get reflected in the model. So clicking on the Download button always downloads the file in the same version. As I have defaulted to Version 2 in my DropDownChoice, it always downloads this version.

Here's my code:

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

What is it that I'm missing? How do I update the DropDownChoice?

Update and solution (changed according to Svens suggestion):

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

Thanks.

... And here's the complete code (Java and HTML below):

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>
svenmeier

You have to use a AjaxFormComponentUpdatingBehavior to transfer the newly selected item to the Java component (and its model):

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

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

And then your downloadLink has dynamically adjust to the current selection too:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Reusable DropDownChoice in Wicket Form

From Dev

Reusable DropDownChoice in Wicket Form

From Dev

Role privileges in Wicket DropDownChoice

From Dev

Wicket, How can a DropDownChoice select change the options of another DropDownChoice?

From Dev

Reverting dropDownChoice value if precondition is negative

From Dev

Convert selected DropDownChoice to Model in CompoundPropertyModel

From Dev

dropdownchoice with choicerenderer, how to get values

From Dev

Reverting dropDownChoice value if precondition is negative

From Dev

Wicket: Setting value of DropDownChoice with CompoundPropertyModel

From Dev

C# create DataView from DataView

From Dev

C# create DataView from DataView

From Dev

Converting DataView to DataSet

From Dev

observablecollection to dataView or dataset

From Dev

How to implement a DataView in Cocoa?

From Dev

what is the difference DataView and DataRowView

From Dev

DataView and prototypical Inheritance

From Dev

extjs dataview not show images

From Dev

WPF Datagrid DataBinding DataView

From Dev

xPages DataView and Repeat control

From Dev

How to cast DataView to DataSource

From Dev

extjs dataview not show images

From Dev

Webextension DataView constructor not working

From Dev

Is it possible to disable certain items in a Wicket DropDownChoice?

From Dev

How to add "onchange" SimpleAttributeModifier to DropDownChoice in Apache Wicket

From Dev

How to get selected value in Wicket DropDownChoice?

From Dev

Selecting the model attribute to display with a Wicket 7 DropDownChoice

From Dev

How to Convert return type Task<DataView> of async to DataView in c#

From Dev

DataGridViewRow Color Changes On DataView RowFilter

From Dev

Swapping byte order with DataView and TypedArrays

Related Related

  1. 1

    Reusable DropDownChoice in Wicket Form

  2. 2

    Reusable DropDownChoice in Wicket Form

  3. 3

    Role privileges in Wicket DropDownChoice

  4. 4

    Wicket, How can a DropDownChoice select change the options of another DropDownChoice?

  5. 5

    Reverting dropDownChoice value if precondition is negative

  6. 6

    Convert selected DropDownChoice to Model in CompoundPropertyModel

  7. 7

    dropdownchoice with choicerenderer, how to get values

  8. 8

    Reverting dropDownChoice value if precondition is negative

  9. 9

    Wicket: Setting value of DropDownChoice with CompoundPropertyModel

  10. 10

    C# create DataView from DataView

  11. 11

    C# create DataView from DataView

  12. 12

    Converting DataView to DataSet

  13. 13

    observablecollection to dataView or dataset

  14. 14

    How to implement a DataView in Cocoa?

  15. 15

    what is the difference DataView and DataRowView

  16. 16

    DataView and prototypical Inheritance

  17. 17

    extjs dataview not show images

  18. 18

    WPF Datagrid DataBinding DataView

  19. 19

    xPages DataView and Repeat control

  20. 20

    How to cast DataView to DataSource

  21. 21

    extjs dataview not show images

  22. 22

    Webextension DataView constructor not working

  23. 23

    Is it possible to disable certain items in a Wicket DropDownChoice?

  24. 24

    How to add "onchange" SimpleAttributeModifier to DropDownChoice in Apache Wicket

  25. 25

    How to get selected value in Wicket DropDownChoice?

  26. 26

    Selecting the model attribute to display with a Wicket 7 DropDownChoice

  27. 27

    How to Convert return type Task<DataView> of async to DataView in c#

  28. 28

    DataGridViewRow Color Changes On DataView RowFilter

  29. 29

    Swapping byte order with DataView and TypedArrays

HotTag

Archive