簡體   English   中英

Wicket:如何在textarea的onkeyup上更改標簽的文字?

[英]Wicket: How to change label's text on textarea's onkeyup?

如何在textarea的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") );
    }
  }

萊奧尼德幾乎就在那里。 結果代碼是:

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輕量級標記語言沙箱。

感謝幫助!

如果要在AJAX事件后更新組件,則必須執行以下兩項操作:

  1. 可更新組件必須具有setset標志setOutputMarkupId == true;
  2. 您必須將此組件添加到目標onEvent方法

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

PS我不了解你代碼的很多部分。

而不是resultDiv.renderComponent(); 嘗試resultDiv.modelChanged();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM