views:

640

answers:

5

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?

+1  A: 

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.

Esko
A: 

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

miaubiz
+2  A: 

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

tetsuo
+1  A: 

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(); } 
    };
jgormley
More from the wicket docs (https://cwiki.apache.org/WICKET/lifecycle-of-a-wicket-application.html):Component.render() follows these steps to render a component:1. Determine component visibility. If the component is not visible,the RequestCycle's Response is changed to NullResponse.getInstance(),which is a Response implementation that simply discards its output.
jgormley
+2  A: 

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

Karussell
Thanks, thats seems like a good solution!
theomega