Javascript TextArea不会滚动到最大底部

Javascript TextArea不会滚动到最大底部,javascript,java,spring-boot,vaadin,Javascript,Java,Spring Boot,Vaadin,我有一个长文本的文本区域,我想在视图显示时滚动到最大底部: TextArea textArea = new TextArea(); textArea.setValue(createLongText()); textArea.setHeight("500px"); textArea.setMaxHeight("500px"); textArea.setWidthFull(); textArea.setReadOnly(false); textArea.ge

我有一个长文本的文本区域,我想在视图显示时滚动到最大底部:

TextArea textArea = new TextArea();
textArea.setValue(createLongText());
textArea.setHeight("500px");
textArea.setMaxHeight("500px");
textArea.setWidthFull();

textArea.setReadOnly(false);
textArea.getElement().executeJs("this.inputElement.scrollTop = 500");
//text.setReadOnly(true);
textArea.getStyle().set("overflow-y", "auto ! important");
add(currentComponent = textArea);
根据我在网上找到的东西,我尝试了以下所有方法:

text.getElement().executeJs(“this.inputElement.scrollTop=1000”)//或滚动高度

text.getElement().executeJs(“this.scrollTop=1000”)//或滚动高度

text.getStyle().set(“overflow-y”、“auto!important”)

但这些都不管用

以下是我的完整观点:

@Route("")
@PWA(name = "Project Base for Vaadin Flow with Spring", shortName = "Project Base")
@Secured("ROLE_ADMIN")
public class DashboardView extends VerticalLayout {

    private Component currentComponent;

    @Autowired
    public DashboardView() {
        Button longButtonText = new Button("Long textarea");
        longButtonText.addClickListener(e -> {
            if (currentComponent != null)
                remove(currentComponent);

            TextArea textArea = new TextArea();
            textArea.setValue(createLongText());
            textArea.setHeight("500px");
            textArea.setMaxHeight("500px");
            textArea.setWidthFull();

            textArea.setReadOnly(false);
            textArea.getElement().executeJs("this.inputElement.scrollTop = 500");
//          text.setReadOnly(true);
            textArea.getStyle().set("overflow-y", "auto ! important");
            add(currentComponent = textArea);
        });

        Button logoutButton = new Button("logout");
        logoutButton.addClickListener(e -> {
            UI.getCurrent().getSession().close();
            SecurityContextHolder.getContext().setAuthentication(null);
            SecurityContextHolder.clearContext();
            UI.getCurrent().getPage().setLocation("/login");
        });

        add(new HorizontalLayout(longButtonText, logoutButton));

        longButtonText.click();
    }

    private String createLongText() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 2000; i++) {
            sb.append(UUID.randomUUID().toString());
            sb.append(System.lineSeparator());
        }
        return sb.toString();
    }
}
@Route(“”)
@PWA(name=“带弹簧的瓦丁流项目基础”,shortName=“项目基础”)
@安全(“角色\管理员”)
公共类仪表板视图扩展了垂直布局{
私有组件;
@自动连线
公共仪表板视图(){
按钮longButtonText=新按钮(“长文本区域”);
longButtonText.addClickListener(e->{
如果(currentComponent!=null)
移除(当前组件);
TextArea TextArea=新建TextArea();
setValue(createLongText());
textArea.setHeight(“500px”);
textArea.setMaxHeight(“500px”);
textArea.setWidthFull();
textArea.setReadOnly(false);
textArea.getElement().executeJs(“this.inputElement.scrollTop=500”);
//text.setReadOnly(true);
textArea.getStyle().set(“overflow-y”、“auto!important”);
添加(currentComponent=textArea);
});
按钮注销按钮=新按钮(“注销”);
注销按钮。添加ClickListener(e->{
UI.getCurrent().getSession().close();
SecurityContextHolder.getContext().setAuthentication(null);
SecurityContextHolder.clearContext();
UI.getCurrent().getPage().setLocation(“/login”);
});
添加(新水平布局(longButtonText,logoutButton));
longButtonText.click();
}
私有字符串createLongText(){
StringBuilder sb=新的StringBuilder();
对于(int i=0;i<2000;i++){
sb.append(UUID.randomuid().toString());
sb.append(System.lineSeparator());
}
使某人返回字符串();
}
}
My vaadin.version:
14.1.17


我应该做些什么来实现它?

< P>实际滚动的元素位于DOM层次结构的中间,而不是顶级组件或<代码>输入元素< /代码>。我没有发现任何直接访问此元素的方法,因此您需要以
this.inputElement.parentElement.parentElement
的形式使用一些间接方式

因此,要滚动到末尾,您需要执行
textArea.getElement().executeJs(“this.inputElement.parentElement.parentElement.scrollTop=this.inputElement.parentElement.parentElement.scrollHeight”)

还应该提到,这种方法取决于与组件内部结构相关的实现细节。这意味着在未来的版本中存在结构更改的风险,而这种更改不会被视为破坏性更改

@Route("")
@PWA(name = "Project Base for Vaadin Flow with Spring", shortName = "Project Base")
@Secured("ROLE_ADMIN")
public class DashboardView extends VerticalLayout {

    private Component currentComponent;

    @Autowired
    public DashboardView() {
        Button longButtonText = new Button("Long textarea");
        longButtonText.addClickListener(e -> {
            if (currentComponent != null)
                remove(currentComponent);

            TextArea textArea = new TextArea();
            textArea.setValue(createLongText());
            textArea.setHeight("500px");
            textArea.setMaxHeight("500px");
            textArea.setWidthFull();

            textArea.setReadOnly(false);
            textArea.getElement().executeJs("this.inputElement.scrollTop = 500");
//          text.setReadOnly(true);
            textArea.getStyle().set("overflow-y", "auto ! important");
            add(currentComponent = textArea);
        });

        Button logoutButton = new Button("logout");
        logoutButton.addClickListener(e -> {
            UI.getCurrent().getSession().close();
            SecurityContextHolder.getContext().setAuthentication(null);
            SecurityContextHolder.clearContext();
            UI.getCurrent().getPage().setLocation("/login");
        });

        add(new HorizontalLayout(longButtonText, logoutButton));

        longButtonText.click();
    }

    private String createLongText() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 2000; i++) {
            sb.append(UUID.randomUUID().toString());
            sb.append(System.lineSeparator());
        }
        return sb.toString();
    }
}