简体   繁体   中英

Vaadin - How can I use the UriFragmentUtility class?

How can I make this? https://vaadin.com/book/-/page/advanced.urifu.html#figure.advanced.urifu

I would like to use a label (eg like in this page "Figure 11.12, Application State Management with URI Fragment Utility") and if the user click on it, then the browser focuses that paragraph.

One way is to use method UI.scrollIntoView(Component) after receiving UriFragmentChangedEvent . Another way would be to use JavaScript. SSCCE (MainUI class):

VerticalLayout layout = new VerticalLayout();
Label up = new Label("Up");
Label middle = new Label("Middle");
Label down = new Label("Down");


@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = QwertUI.class)
public static class Servlet extends VaadinServlet {
}

@Override
protected void init(VaadinRequest request) {
    Button buttonUp = new Button("Up");
    Button buttonMiddle = new Button("Middle");
    Button buttonDown = new Button("Down");
    buttonUp.addClickListener(this);
    buttonMiddle.addClickListener(this);
    buttonDown.addClickListener(this);
    up.setHeight("666px");
    middle.setHeight("666px");
    down.setHeight("666px");
    layout.addComponents(buttonUp, buttonMiddle, buttonDown, up, middle, down);
    setContent(layout);
    getPage().addUriFragmentChangedListener(
            new UriFragmentChangedListener() {
        public void uriFragmentChanged(
                UriFragmentChangedEvent source) {
            enter(source.getUriFragment());
         }
     });
     enter(getPage().getUriFragment());
}

private void enter(String uriFragment){

    UI ui = up.getUI();
    if(uriFragment != null){
        switch(uriFragment){
            case "Up": ui.scrollIntoView(up);; break;
            case "Middle": ui.scrollIntoView(middle); break;
            case "Down": ui.scrollIntoView(down); break;
        }
    }
}

@Override
public void buttonClick(ClickEvent event)
{
    getPage().setUriFragment(event.getButton().getCaption());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM