文本区域无法滚动到最底部。

huangapple go评论71阅读模式
英文:

TextArea does not scroll to maximum bottom

问题

我有一个带有长文本的文本区域我希望在视图显示时滚动到最底部

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"); //or scrollHeight`

`text.getElement().executeJs("this.scrollTop = 1000"); //or scrollHeight`

`text.getStyle().set("overflow-y", "auto ! important");`

textArea.getElement()
        .executeJs("this.inputElement.selectionStart= 1000;this.inputElement.selectionEnd= 1001;");

但这些都不起作用

这是我的完整视图

@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();
    }
}

我的 vaadin.version: `<vaadin.version>14.1.17</vaadin.version>`

我应该怎么做才能实现这个
英文:

I have a text area with long text and I want when the view shows up to be scrolled to maximum bottom:

TextArea textArea = new TextArea();
textArea.setValue(createLongText());
textArea.setHeight(&quot;500px&quot;);
textArea.setMaxHeight(&quot;500px&quot;);
textArea.setWidthFull();
textArea.setReadOnly(false);
textArea.getElement().executeJs(&quot;this.inputElement.scrollTop = 500&quot;);
//text.setReadOnly(true);
textArea.getStyle().set(&quot;overflow-y&quot;, &quot;auto ! important&quot;);
add(currentComponent = textArea);

I tried all of the followings based on things I found on the web:

text.getElement().executeJs(&quot;this.inputElement.scrollTop = 1000&quot;); //or scrollHeight

text.getElement().executeJs(&quot;this.scrollTop = 1000&quot;); //or scrollHeight

text.getStyle().set(&quot;overflow-y&quot;, &quot;auto ! important&quot;);

textArea.getElement()
.executeJs(&quot;this.inputElement.selectionStart= 1000;this.inputElement.selectionEnd= 1001;&quot;);

but none of these works.

Here is my complete view:

@Route(&quot;&quot;)
@PWA(name = &quot;Project Base for Vaadin Flow with Spring&quot;, shortName = &quot;Project Base&quot;)
@Secured(&quot;ROLE_ADMIN&quot;)
public class DashboardView extends VerticalLayout {
private Component currentComponent;
@Autowired
public DashboardView() {
Button longButtonText = new Button(&quot;Long textarea&quot;);
longButtonText.addClickListener(e -&gt; {
if (currentComponent != null)
remove(currentComponent);
TextArea textArea = new TextArea();
textArea.setValue(createLongText());
textArea.setHeight(&quot;500px&quot;);
textArea.setMaxHeight(&quot;500px&quot;);
textArea.setWidthFull();
textArea.setReadOnly(false);
textArea.getElement().executeJs(&quot;this.inputElement.scrollTop = 500&quot;);
//			text.setReadOnly(true);
textArea.getStyle().set(&quot;overflow-y&quot;, &quot;auto ! important&quot;);
add(currentComponent = textArea);
});
Button logoutButton = new Button(&quot;logout&quot;);
logoutButton.addClickListener(e -&gt; {
UI.getCurrent().getSession().close();
SecurityContextHolder.getContext().setAuthentication(null);
SecurityContextHolder.clearContext();
UI.getCurrent().getPage().setLocation(&quot;/login&quot;);
});
add(new HorizontalLayout(longButtonText, logoutButton));
longButtonText.click();
}
private String createLongText() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i &lt; 2000; i++) {
sb.append(UUID.randomUUID().toString());
sb.append(System.lineSeparator());
}
return sb.toString();
}
}

My vaadin.version: &lt;vaadin.version&gt;14.1.17&lt;/vaadin.version&gt;

What should I do in order to achieve it?

答案1

得分: 1

要滚动的实际元素位于DOM层次结构的中间,而不是顶层组件或inputElement。我没有找到直接访问此元素的方法,因此您需要使用一些间接方式,即this.inputElement.parentElement.parentElement

因此,要滚动到底部,您需要执行以下操作:textArea.getElement().executeJs("this.inputElement.parentElement.parentElement.scrollTop = this.inputElement.parentElement.parentElement.scrollHeight");

还应该提到,这种方法依赖于与组件的内部结构相关的实现细节。这意味着存在一种风险,即在将来的某个版本中更改了结构,而该更改不被视为破坏性更改。

英文:

The element that actually scrolls is in the middle of the DOM hierarchy rather than either the top-level component or the inputElement. I didn't spot any direct way of accessing this element, so you need to use a little indirection in the form of this.inputElement.parentElement.parentElement.

To scroll to the end, you thus need to do textArea.getElement().executeJs(&quot;this.inputElement.parentElement.parentElement.scrollTop = this.inputElement.parentElement.parentElement.scrollHeight&quot;);.

It should also be mentioned this approach is dependent on implementation details related to the internal structure of the component. This means that there's a risk that the structure is changed in some future version without that change being treated as a breaking change.

huangapple
  • 本文由 发表于 2020年9月10日 21:18:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63830578.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定