andPat
andPat

Reputation: 4503

Ajax link change visibility with setVisible() in onClick() method

I'm working with wicket 1.4 and I've two ajax links A and B. I want to show them alternately, I mean that when I click on link B, A visibility is set to false (A disappears) and viceversa. I tried with setVisibility() on the OnClick() method of the links but it doesn't work, that is, a component only disappears, visibility is only deactivated never activated: setVisible(false) in onClick() method works and setVisible(true) not. Can You help me??

Upvotes: 0

Views: 1519

Answers (3)

Robert Niestroj
Robert Niestroj

Reputation: 16131

Like @polypiel s answer but setting visibility in onConfigure() which is a best practise.

public class MyPanel extends Panel{

   private boolean showLinkA = true;
   AjaxLink aLink;
   AjaxLink bLink;

   public MyPanel(String id) {
      super(id);
      add(aLink = aLink());
      add(bLink = bLink());
   }

   private AjaxLink aLink() {
      AjaxLink al = new AjaxLink("aLink") {

         @Override
         protected void onConfigure() {
            super.onConfigure();
            setVisible(showLinkA);
         }

         @Override
         public void onClick(AjaxRequestTarget target) {
            showLinkA = false;
            target.add(aLink, bLink);
         }
      };
      al.setOutputMarkupPlaceholderTag(true);
      return al;
   }

   private AjaxLink bLink() {
      AjaxLink bl = new AjaxLink("aLink") {

         @Override
         protected void onConfigure() {
            super.onConfigure();
            setVisible(!showLinkA);
         }

         @Override
         public void onClick(AjaxRequestTarget target) {
            showLinkA = true;
            target.add(aLink, bLink);
         }
      };
      bl.setOutputMarkupPlaceholderTag(true);
      return bl;
   }

}

Upvotes: 2

polypiel
polypiel

Reputation: 2341

You can achieve it redfining the isVisible methods of both links. And when one link is clicked you only change the boolean variable and repaint the links.

Here is a draft, but I hardly remember classes and methods names:

// indicates which link is showed
boolean showA;
// I don't remember exactly the class name
AjaxLink linkA, linkB;

// For each link redefine onClick and isVisible
linkA = new AjaxLink() {
  @Override boolean isVisible() { return showA; }
  @Override void onClick(target) {
     showA = false;
     target.addComponent(linkA);
     target.addComponent(linkB);
  }
}
linkB = new AjaxLink() {
  @Override boolean isVisible() { return !showA; }
  @Override void onClick(target) {
     showA = true;
     target.addComponent(linkA);
     target.addComponent(linkB);
  }
}

Upvotes: 2

Mohamed Amine
Mohamed Amine

Reputation: 2304

You can try with jQuery methods show() and hide(). More details here and here.

Upvotes: 1

Related Questions