2

Hey I have a ListView nested inside a ListView and want to change the inner ListViews visibility on click of an AjaxLink.

I saw a lot of posts about it and took some advice from it. E.g. : .setMarkupId(true), add a WebMarkupContainer, ...

But nothing really helped. Anyway, here's what I got so far.

public class EditZustaendigkeit extends WebPage {
private static final long serialVersionUID = 1L;
private List<Institut> institute = new ArrayList<Institut>();
private Form<Void> mainForm = new Form<Void>("mainForm");

public EditZustaendigkeit() {
    this.add(mainForm);
    createInstitute();

    mainForm.add(new ListView<Institut>("listView", getInstitute()) {

        private static final long serialVersionUID = 1L;

        protected void populateItem(final ListItem<Institut> item) {
            final int index = item.getIndex();
            AjaxLink<String> myLink;
            IModel<String> labelModel = Model.of("+");
            final Institut institut = (Institut) institute.get(index);

            item.add(new Label("institutLabel", new PropertyModel<Institut>(institut, "langName")));


            final WebMarkupContainer div = new WebMarkupContainer("cont");
            div.setOutputMarkupId(true);
            div.setOutputMarkupPlaceholderTag(true);
            div.add(new ListItemZustaendigkeit("zustaendigkeiten", item, institut));
            item.add(div);

            item.add(myLink = new AjaxLink<String>("extenderInstitut") {
                private static final long serialVersionUID = 1L;

                @Override
                public void onClick(AjaxRequestTarget target) {
                    collapseItem(index, item, div);
                    target.add(mainForm);
                    item.add(div);
                    setResponsePage(getPage());
                }
            });
            myLink.setBody(labelModel);
            myLink.setOutputMarkupId(true);

            item.add(new CheckBox("institutCheck", Model.of(Boolean.FALSE)) {
                private static final long serialVersionUID = 1L;
                // todo
            });
        }
    });
}

public void expandAllItem(ListView<Institut> listView) {

}

public void collapseAllItem(ListView<Institut> listView) {

}

public void expandItem(int index, ListItem<Institut> item, WebMarkupContainer div) {
    div.setVisible(true);
}

public void collapseItem(int index, ListItem<Institut> item, WebMarkupContainer div) {
    div.setVisible(false);
}

ListItemZustaendigkeit extends from Panel. It has it's corresponding .html file.

Right now I want to focus on public void collapseItem() because I guess if one can be solved all the others should be possible as well.

Peter
  • 1,844
  • 2
  • 31
  • 55
  • 1
    Just a wild guess, have you tried setting `setReuseItems(true)` on the `ListView`? This will help Wicket keep the same id attributes for the list elements, which might be helpful if the view is to be refreshed through Ajax. Also look into `setOutputMarkupPlaceHolderTag()` as explained [here](http://stackoverflow.com/a/9671796/851811) – Xavi López Aug 25 '15 at 14:47
  • 1
    Thank both of you, now it works! :) – Peter Aug 26 '15 at 05:50
  • Glad to know it's been sorted out :) Please, would you find the time to mark the answer that solved your problem as _accepted_ by checking the green tick next to it? If you resolved your problem by other means note you can also add your own answer and accept it. This way future visitors of the question will clearly identify the correct answer and we'll have a better SO. – Xavi López Aug 26 '15 at 07:24
  • Just read it om the tour, but thank you for the hint. :) – Peter Aug 26 '15 at 08:42
  • Note on the margin. Wicket "evangelism" prefer override getter `bool isVisible() { return authorCondition;}` over `setVisible(condition)`. In few situation this was helpfull for me. Of course not always override is possible. – Jacek Cz Sep 02 '15 at 07:03

1 Answers1

2

The problem appears to be in your onClick of "extenderInstitut". You are calling setResponsePage which will cause your repeater to initialize again. Also, you want to have setOutputMarkupId(true) on mainForm.

item.add(myLink = new AjaxLink<String>("extenderInstitut") {
  private static final long serialVersionUID = 1L;

  @Override
  public void onClick(AjaxRequestTarget target) {
    collapseItem(index, item, div);
    target.add(mainForm);
  }
});
JeredM
  • 897
  • 1
  • 14
  • 25