Wicket:如何更改文本区域 onkeyup 上的标签文本?

发布于 2024-08-19 00:05:12 字数 2120 浏览 5 评论 0原文

如何更改文本区域 onkeyup 上的标签文本?我已经尝试过这个,但不起作用:

Form form;
TextArea ta;
MyLabel resultDiv;


  /**
   * Constructor that is invoked when page is invoked without a session.
   */
  public HomePage(final PageParameters parameters) {

      this.form = new Form("form");
      this.ta = new TextArea("text");
      this.resultDiv = new MyLabel("result");

      this.ta.add( new AjaxEventBehavior( "onKeyUp" ) {
        protected void onEvent( AjaxRequestTarget target ) {
          System.out.println( "Ajax!" );
          resultDiv.setText("Foobar");
          resultDiv.renderComponent();
        }
      } );


      form.add( ta );
      form.add( resultDiv );
      add( form );

  }// const

  public class MyLabel extends Label {
    private String text = "original";
    public String getText() {      return text;    }
    public void setText( String text ) {      this.text = text;    }
    public MyLabel( String id ) {
      super( id );
      this.setModel( new PropertyModel(this,"text") );
    }
  }

解决方案

leonidv 几乎就在那里。结果代码是:

Form form;
TextArea ta;
Label resultDiv = new Label( "result", new PropertyModel(this,"labelText") ){
  { setOutputMarkupId( true ); }
};

private String labelText = "original";


/**
 * Constructor that is invoked when page is invoked without a session.
 */
public HomePage(final PageParameters parameters) {

    this.form = new Form("form");

    this.ta = new TextArea("text");
    this.ta.add( new AjaxEventBehavior( "onKeyUp" ) {
      protected void onEvent( AjaxRequestTarget target ) {
        System.out.println( "Ajax!" );
        labelText = "Foobar";  // Doesn't even need get/set, which is great.
        target.addComponent( resultDiv );
        //resultDiv.renderComponent(); // WRONG!!
      }
    } );

    form.add( ta );
    form.add( resultDiv );
    add( form );

}// const

最后一个问题是我对添加 renderComponent() 的糟糕直觉 - 由于某种原因,标签保持不变。

顺便说一句,结果将很快作为 JTexy 轻量级标记语言沙箱提供。

感谢您的帮助!

How do I change label's text on textarea's onkeyup? I've tried this but does not work:

Form form;
TextArea ta;
MyLabel resultDiv;


  /**
   * Constructor that is invoked when page is invoked without a session.
   */
  public HomePage(final PageParameters parameters) {

      this.form = new Form("form");
      this.ta = new TextArea("text");
      this.resultDiv = new MyLabel("result");

      this.ta.add( new AjaxEventBehavior( "onKeyUp" ) {
        protected void onEvent( AjaxRequestTarget target ) {
          System.out.println( "Ajax!" );
          resultDiv.setText("Foobar");
          resultDiv.renderComponent();
        }
      } );


      form.add( ta );
      form.add( resultDiv );
      add( form );

  }// const

  public class MyLabel extends Label {
    private String text = "original";
    public String getText() {      return text;    }
    public void setText( String text ) {      this.text = text;    }
    public MyLabel( String id ) {
      super( id );
      this.setModel( new PropertyModel(this,"text") );
    }
  }

Solution

leonidv was almost there. The resulting code is:

Form form;
TextArea ta;
Label resultDiv = new Label( "result", new PropertyModel(this,"labelText") ){
  { setOutputMarkupId( true ); }
};

private String labelText = "original";


/**
 * Constructor that is invoked when page is invoked without a session.
 */
public HomePage(final PageParameters parameters) {

    this.form = new Form("form");

    this.ta = new TextArea("text");
    this.ta.add( new AjaxEventBehavior( "onKeyUp" ) {
      protected void onEvent( AjaxRequestTarget target ) {
        System.out.println( "Ajax!" );
        labelText = "Foobar";  // Doesn't even need get/set, which is great.
        target.addComponent( resultDiv );
        //resultDiv.renderComponent(); // WRONG!!
      }
    } );

    form.add( ta );
    form.add( resultDiv );
    add( form );

}// const

The last problem was my bad intuition about adding renderComponent() - that, for some reason, kept the label unchanged.

By the way, the result will serve soon as JTexy lightweight markup language sandbox.

Thanks for help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

指尖上得阳光 2024-08-26 00:05:12

如果你想在 AJAX 事件后更新组件,你必须做两件事:

  1. 可更新组件必须设置标志 setOutputMarkupId == true;
  2. 你必须将此组件添加到目标 onEvent 方法

    this.resultDiv.setMarkupOutputId(true);
    
    protected void onEvent( AjaxRequestTarget 目标 ) {
          System.out.println("Ajax!");
          //resultDiv.setModel( );
          resultDiv.setText("Foobar");
          resultDiv.renderComponent();
          target.add(resultDiv);
    }
    

PS 我不明白您代码的很多部分。

If you want to update components after AJAX event, you must to do 2 things:

  1. Updatable components must have setted flag setOutputMarkupId == true;
  2. You must add this components to target onEvent method

    this.resultDiv.setMarkupOutputId(true);
    
    protected void onEvent( AjaxRequestTarget target ) {
          System.out.println( "Ajax!" );
          //resultDiv.setModel(  );
          resultDiv.setText("Foobar");
          resultDiv.renderComponent();
          target.add(resultDiv);
    }
    

PS I don't understand many parts of your code.

胡渣熟男 2024-08-26 00:05:12

而不是 resultDiv.renderComponent();
尝试 resultDiv.modelChanged();

instead of resultDiv.renderComponent();
try resultDiv.modelChanged();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文