如何更改Wicket中的文字标签

问题描述 投票:0回答:1

我试图使用AjaxLink改变wicket中按钮上的文字,但它无法正常工作,请提供帮助,这是我的代码;WebMarkupContainer xyzContainer = new WebMarkupContainer。

<form name="depForm" wicket:id="depSummaryForm" action=""> 
<div class="uw_summary"> <div class="buttonContainer"> 
<span class="button_v2" wicket:id="newRecruitContainer"> 
<button class="internal_button" wicket:id="newRecruitEWLButton" 
type="button"> Create New Recruit EWL </button> </span> 

final WebMarkupContainer newRecruitContainer = new 
WebMarkupContainer("newRecruitContainer")
{
Label buttonlabel = new Label("newRecruitEWLButton", Model.of(" "));
buttonlabel.setOutputMarkupPlaceholderTag(true);

AjaxLink newRecruitEWLButton = new AjaxLink("newRecruitEWLButton") { 
@Override public void onClick(AjaxRequestTarget target) { 
buttonlabel.setDefaultModelObject(Model.of("Creating EWL")); 
target.add(buttonlabel); boolean isWorkItemCreated = 
NewRecruitEWLUtil.createNewRecruitWorkItem(appInfo); } };  
wicket
1个回答
1
投票

你需要更新Label的模型,而不是添加一个新的Label。

WebMarkupContainer xyzContainer = new WebMarkupContainer("xyzContainer") {

   @Override
   public void onConfigure() {  // [1]
      super.onConfigure();
      boolean isVisible = some code goes here
      setVisible(isVisible);
   }
};

final Label buttonlabel = new Label("clickMe", ""); // [2]
buttonlabel.setOutputMarkupId(true); // [3]

AjaxLink xyzButton = new AjaxLink("xyzButton") {
   @Override
   public void onClick(AjaxRequestTarget target) {         
       buttonlabel.setModelObject("Clicked")); // [4]
       target.add(buttonlabel);
   }
};


xyzContainer.add(xyzButton);
Form.add(xyzContainer);

以下是我建议的修改。

  1. 使用onConfigure()+setVisible(),因为isVisible()会被调用很多次,而且取决于 "一些代码在这里 "有多贵,它可能会影响整体性能。
  2. 设置一个初始标签--空字符串
  3. setOutputMarkupPlaceholderTag()应该在组件可以被设置为不可见时使用。如果它总是可见,那么setOutputMarkupId(true)就足够了。
  4. 使用Component.setModelObject(...)为同一个Label实例设置一个新的值。

更新:你的代码应该是这样的。

<form name="depForm" wicket:id="depSummaryForm"> 
   <div class="uw_summary"> 
     <div class="buttonContainer"> 
       <span class="button_v2" wicket:id="newRecruitContainer"> 
         <button class="internal_button" wicket:id="newRecruitEWLButton" 
type="button">
 <span wicket:id="label">Create New Recruit EWL</span></button> 
       </span>
     </div>
   </div>
</form>


final WebMarkupContainer newRecruitContainer = new 
WebMarkupContainer("newRecruitContainer");

Label buttonlabel = new Label("label", Model.of(" "));
   buttonlabel.setOutputMarkupPlaceholderTag(true);

   AjaxLink newRecruitEWLButton = new AjaxLink("newRecruitEWLButton") 
   { 
      @Override public void onClick(AjaxRequestTarget target) { 
          buttonlabel.setDefaultModelObject(Model.of("Creating EWL")); 
          target.add(buttonlabel); 
          boolean isWorkItemCreated = 
             NewRecruitEWLUtil.createNewRecruitWorkItem(appInfo); } 
   };
   newRecruitContainer.add(newRecruitEWLButton);
   newRecruitEWLButton.add(buttonlabel);

0
投票

在html文件中使用div而不是span来做按钮标签,在onclick setDefaultModel()中调用这个方法;这个方法在这个sceanario中不起作用--setDefaultModelModelObject()

© www.soinside.com 2019 - 2024. All rights reserved.