Wicket: Is it possible to display components of a panel based on page the panel is added to?

。_饼干妹妹 提交于 2019-12-11 08:56:21

问题


I'm pretty new to Wicket, so please excuse me if this is a simple task. I tried to look for examples but came up empty.

Here's the scenario. Lets say that I have a customer information panel with 25 data points (name, dob, address, phone, etc). I want to reuse this panel in many different pages on a site. Now lets say I have the following criteria on 3 different pages the panel appears on:

  • Page 1 - All fields are visible and editable
  • Page 2 - Date of Birth, Gender, Customer Name are visible but not editable, all other fields are visible and editable
  • Page 3 - Date of Birth and Gender are not visible, Address is visible and editable, all other fields are visible but not editable.

Is there a way to use the same panel (to reduce code duplication), but control its components based on the current page the panel is loaded into?

Thanks for any help!


回答1:


I would suggest against changing the visibility and editability implicitly like this. Instead consider making it explicit based on constructor arguments for the component or additional methods on the custom component.




回答2:


Inside your panel you can call the getPage() method. Either you can check if the page is instance of a certain class our you can get the page class by calling getPageClass().

The visibility of your components inside the panel can be set by calling the setVisible() method or by overriding the isVisible() method. If components should be editable can be controlled by using the isEnabled() method (or overriding setEnabled()).

class YourPanel extends Panel {
    public YourPanel() {
        add(new TextField("name") {
                @Override
                boolean isVisible() {
                    return getPage().getPageClass().equals(Page2.class);
                }
        }
        TextField genderTextField = new TextField("gender");
        genderTextField.setVisible(!getPage().getPageClass().equals(Page3.class));
        add(genderTextField);
    }
}

As you can see, for many components and many cases to check the code will be quite complex. Maybe you can get cleaner code if you create custom panels, add the components according to their visibility and editability rules and control for the whole panels if they are visible and/or editable.




回答3:


Per the other answer, you can call setVisible() and the like to customize the appearance.

I don't recommend you couple your panel to the displaying page, however; this will introduce circular dependency, and each time you want to reuse the panel on a new type of page, you'd have to add in another check for the new page. Instead, just make the panel configurable (best to do this at construction time by passing parameters such as boolean addressVisible).



来源:https://stackoverflow.com/questions/11193088/wicket-is-it-possible-to-display-components-of-a-panel-based-on-page-the-panel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!