使用GwtQuery自动增长/自动调整TextArea大小

时间:2014-03-16 02:29:34

标签: java gwt gwtquery errai gquery

这是我自动调整大小的TextArea的代码(我使用Errai,所以TextArea注入了btw):

@Inject
@DataField
TextArea content;

@AfterInitialization
public void afterInit(){
content.getElement().setAttribute("wrap","off");

        content.addKeyUpHandler(new KeyUpHandler() {
            @Override
            public void onKeyUp(KeyUpEvent event) {
                $(content)
                        .css("overflow-y", "hidden")
                        .css("overflow-x", "auto")
                        .css(CSS.HEIGHT, "300px")
                        .css(CSS.HEIGHT, DOM.getElementPropertyInt(content.getElement(),"scrollHeight")  + "px");
            }
        });

        content.addValueChangeHandler(new ValueChangeHandler<String>() {
            @Override
            public void onValueChange(ValueChangeEvent<String> event) {
                $(content)
                        .css("overflow-y", "hidden")
                        .css("overflow-x", "auto")
                        .css(CSS.HEIGHT, "300px")
                        .css(CSS.HEIGHT, DOM.getElementPropertyInt(content.getElement(),"scrollHeight")  + "px");
            }
        });

        content.addMouseUpHandler(new MouseUpHandler() {
            @Override
            public void onMouseUp(MouseUpEvent event) {
                $(content)
                        .css("overflow-y", "hidden")
                        .css("overflow-x", "auto")
                        .css(CSS.HEIGHT, "300px")
                        .css(CSS.HEIGHT, DOM.getElementPropertyInt(content.getElement(),"scrollHeight")  + "px");
            }
        });
}

这段代码很好用,我想注意几个问题:

  • 代码看起来不优雅,代码冗余,有没有办法简化它
  • 键入使TextArea自动增长,同时CTRL + V命令也使其随内容增长,但是上下文菜单中的RIGHT-CLICK +粘贴项不会自动增长您必须再次单击TextArea才能触发它会成长。
  • 当TextArea增长时它非常陡峭,是否可以产生平滑效果?

0 个答案:

没有答案