Question

Hy, I want to display a certain part (a div for example) of my wicket-template only under a certain condition (for example only if I have the data to fill it). The problem is:

If I only add the panel (filling the div) if I got the data, an exception is thrown every time I call the page without the data (because the referenced wicket-id is not added to the component-tree).

The only solution which came to my mind was to add a empty panel if there is no data. This is not an ideal solution because I got some unneeded code in the java-code and many empty divs in my rendered html.

So is there a better solution to include several parts of a wicket-template only under a condition?

Was it helpful?

Solution

Although this is an old question here could be one more solution: wicket:enclosure (and this )

Update: Now I needed this functionality by my self (for jetwick). I'm using WebMarkupContainer one for loggedIn state and one for loggedOut state and set the right visibility:

if (loggedIn()) {            
   WebMarkupContainer loggedInContainer = new WebMarkupContainer("loggedIn");
   //## do something with the user
   User user = getUserSomeWhere();
   loggedInContainer.add(new UserSearchLink("userSearchLink"));
   add(loggedInContainer);
   add(WebMarkupContainer("loggedOut").setVisible(false));
} else {
   add(new WebMarkupContainer("loggedIn").setVisible(false));
   WebMarkupContainer loggedOutContainer = WebMarkupContainer("loggedOut");
   loggedOutContainer.add(new LoginLink() {...});
   add(loggedOutContainer);
}

The advantage of this for me is that I prevent a NullpointerExc in the //## marked line and the enclose feature of wicket would look more ugly to me in this case I think.

OTHER TIPS

Like @miaubiz said, you can call setVisible(false), or you can override the isVisible() method, if the visibility is conditional to some other state (fields filled, for example).

Yup, you want to override isVisible. This will keep the isVisible=false html markup from even rendering to the final html page. Also, according to the docs (mentioned in EmptyPanel), you can use the WebMarkupContainer as the wrapping component.

    this.add(new SimpleResourceModelLabel(NO_DATA_LABEL){
        private static final long serialVersionUID = 1L;

        @Override
        public boolean isVisible() { return myList.isEmpty(); } 
    });

    final WebMarkupContainer table = new WebMarkupContainer(MY_DATA_TABLE){
        private static final long serialVersionUID = 1L;

        @Override
        public boolean isVisible() { return !myList.isEmpty(); } 
    };

I guess this is why there's EmptyPanel. Without knowing about your code more I can only say that what I think you're doing is something I'd do with combination of some child of AbstractRepeater and Fragment. If you're willing to tell more about what you want to do and maybe provide some code too, I'll be happy to help as much as I can.

you can call setVisible(false); on the component you want to hide.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top