Hi Matthew,

this solution works perfectly well, thanks a lot.

Philipp
You can write a behaviour for it so it will be more reusable.
The following is based on
http://cwiki.apache.org/WICKET/request-focus-on-a-specific-form-component.html
and
works for both regular and ajax requests.

private final class MyLink extends AjaxFallbackLink
  {
          public void onClick(AjaxRequestTarget target)
      {
          onShowForm(target);
          myField.add(new FocusBehavior());
      }
  }

public class FocusBehavior extends AbstractBehavior
{
/**
 *
 */
private static final long serialVersionUID = 1L;

@Override
public void bind(Component component) {
super.bind(component);
component.setOutputMarkupId(true);
final Response response = component.getResponse();
if (response instanceof WebResponse && !((WebResponse)response).isAjax()) {
component.setComponentBorder(new IComponentBorder() {
/**
 *
 */
private static final long serialVersionUID = 1L;
 public void renderBefore(Component component) {
}
 public void renderAfter(Component component) {
final Response response = component.getResponse();
response.write(
"<script type=\"text/javascript\"
language=\"javascript\">document.getElementById(\"" +
component.getMarkupId() +
"\").focus()</script>");
}
});
}
this.component = component;
}
 private Component component;
 @Override
public void renderHead(IHeaderResponse response) {
if (response.getResponse() instanceof WebResponse &&
((WebResponse)response.getResponse()).isAjax()) {
response.renderOnLoadJavascript("document.getElementById('" +
component.getMarkupId() + "').focus()");
}

}

@Override
public boolean isTemporary() {
return true;
}
}

On Thu, Jan 29, 2009 at 4:07 PM, Philipp Daumke <dau...@averbis.de> wrote:

Hi Michael,

thanks for your help, but it doesn't work yet. My code:

TextField myField = new TextField("text");
myField.setOutputMarkupId(true);
myField.setOutputMarkupPlaceholderTag(true);
add(myField);

  private final class MyLink extends AjaxFallbackLink
  {
          public void onClick(AjaxRequestTarget target)
      {
          onShowForm(target);
          target.appendJavascript("getElementById('"+myField.getMarkupId()+
"').focus()");
      }
  }

Is there something obviously wrong? Correct Javascript syntax and semantic?
Is there an alternative "wicket" approach so that I don't have to use
Javascript myself?
Thanks for help
Philipp


 use target.(pre|append)Javascript if you want additional JS executed
before/after doing the wicket-ajax stuff

if you only want to set the focus (without any ajax involved), don't use
ajaxlink, use e.g. a simple WebmarkupContainer

hth,
michael


Philipp Daumke-2 wrote:


Dear all,

I try to show and focus on a text field when clicking on a link. The
focus however doesn't work. I tried to follow an example in cwiki (
http://cwiki.apache.org/WICKET/calling-javascript-function-on-wicket-components-onclick.html)
but I somehow do something wrong. I tried to add some javascript attribute
via AttributeAppender, but then I get a null pointer exception in onClick
("target is null"). When I comment that line out (so don't add the
Attribute), everything works fine except that focus doesn't work.

Any help appreciated.
Thanks a lot
Philipp

Here's my code:

in a WebMarkupContainer:
link.add(new AttributeAppender("onClick", new
Model("getElementById('"+textfield.getMarkupId()+ "').onFocus();"), ";"));

in my custom Link Class:
public void onClick(AjaxRequestTarget target)
       {
           showFormThatContainsTextField(target);
 }

void showFormThatContainsTextField(AjaxRequestTarget target)
   {
       // toggle the visibility
        ...
       // redraw the add container.
       target.addComponent(this);
   }

--

Averbis GmbH
c/o Klinikum der Albert-Ludwigs-Universität
Stefan-Meier-Strasse 26
D-79104 Freiburg

Fon: +49 (0) 761 - 203 6707
Fax: +49 (0) 761 - 203 6800
E-Mail: dau...@averbis.de

Geschäftsführer: Dr. med. Philipp Daumke, Kornél Markó
Sitz der Gesellschaft: Freiburg i. Br.
AG Freiburg i. Br., HRB 701080


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org





-----
Michael Sparer
http://techblog.molindo.at


--

Averbis GmbH
c/o Klinikum der Albert-Ludwigs-Universität
Stefan-Meier-Strasse 26
D-79104 Freiburg

Fon: +49 (0) 761 - 203 6707
Fax: +49 (0) 761 - 203 6800
E-Mail: dau...@averbis.de

Geschäftsführer: Dr. med. Philipp Daumke, Kornél Markó
Sitz der Gesellschaft: Freiburg i. Br.
AG Freiburg i. Br., HRB 701080


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org






--

Averbis GmbH
c/o Klinikum der Albert-Ludwigs-Universität
Stefan-Meier-Strasse 26
D-79104 Freiburg

Fon: +49 (0) 761 - 203 6707
Fax: +49 (0) 761 - 203 6800
E-Mail: dau...@averbis.de

Geschäftsführer: Dr. med. Philipp Daumke, Kornél Markó
Sitz der Gesellschaft: Freiburg i. Br.
AG Freiburg i. Br., HRB 701080


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to