You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Joakim Olsson <jo...@unbound.se> on 2011/03/13 13:11:05 UTC

Re: Focus on first field in added row in AjaxFormLoop

Hi,

answering myself in case it helps someone else in the future.

Inspired by a reply by Josh Canfield to another question in the same
area I came up with the following solution.

I created a PartialMarkupRendererFilter-implementation that takes a
JavaScriptSupport and the field to put focus on:
public class FieldFocusFilter implements PartialMarkupRendererFilter {
    private final JavaScriptSupport javaScriptSupport;
    private final AbstractField field;

    public FieldFocusFilter(final JavaScriptSupport javaScriptSupport,
final AbstractField field) {
        this.javaScriptSupport = javaScriptSupport;
        this.field = field;
    }

    @Override
    public void renderMarkup(final MarkupWriter writer, final JSONObject reply,
            final PartialMarkupRenderer renderer) {
        renderer.renderMarkup(writer, reply);
        this.javaScriptSupport.autofocus(FieldFocusPriority.OVERRIDE,
this.field.getClientId());
    }
}

In my page-class I then inject the PageRenderQueue, JavaScriptSupport
and the field to put focus on:
    @Inject
    private PageRenderQueue pageRenderQueue;

    @Inject
    private JavaScriptSupport javaScriptSupport;

    @Component
    private TextField focusField;

In my onAddRowFromXxx-method I add a new instance of the
FieldFocusFilter to the PageRenderQueue:
        this.pageRenderQueue.addPartialMarkupRendererFilter(new
FieldFocusFilter(this.javaScriptSupport, this.focusField));

Thanks for the inspiration Josh. :-)

Regards,
Joakim


On Sat, Feb 26, 2011 at 6:16 PM, Joakim Olsson <jo...@unbound.se> wrote:
> Hi,
>
> I would like to get focus on the first available field in the added
> row in an AjaxFormLoop. Is it possible in an easy way?
>
> Regards,
> Joakim
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Focus on first field in added row in AjaxFormLoop

Posted by Joakim Olsson <jo...@unbound.se>.
I was ofcourse a bit quick with my solution. I didn't test it enough
after a bit of refactoring and I now get ClassCastExceptions probably
due to the AbstractField being enhanced a bit.

I got around that by some reflection-perfection but there is probably
a better solution to getting the clientId.

New implementation of the filter looks like this:
public class FieldFocusFilter implements PartialMarkupRendererFilter {
    private final JavaScriptSupport javaScriptSupport;
    private final Object field;

    public FieldFocusFilter(final JavaScriptSupport javaScriptSupport,
final Object field) {
        this.javaScriptSupport = javaScriptSupport;
        this.field = field;
    }

    @Override
    public void renderMarkup(final MarkupWriter writer, final JSONObject reply,
            final PartialMarkupRenderer renderer) {
        renderer.renderMarkup(writer, reply);
        final String clientId = this.getClientId();
        this.javaScriptSupport.autofocus(FieldFocusPriority.OVERRIDE, clientId);
    }

    private String getClientId() {
        String clientId;
        try {
            final Method method =
this.field.getClass().getMethod("getClientId");
            clientId = String.valueOf(method.invoke(this.field));
        } catch (final Exception e) {
            clientId = null;
        }

        return clientId;
    }
}

If someone have a better solution than the reflection-hack, please enlighten me.

/Joakim


On Sun, Mar 13, 2011 at 1:11 PM, Joakim Olsson <jo...@unbound.se> wrote:
> Hi,
>
> answering myself in case it helps someone else in the future.
>
> Inspired by a reply by Josh Canfield to another question in the same
> area I came up with the following solution.
>
> I created a PartialMarkupRendererFilter-implementation that takes a
> JavaScriptSupport and the field to put focus on:
> public class FieldFocusFilter implements PartialMarkupRendererFilter {
>    private final JavaScriptSupport javaScriptSupport;
>    private final AbstractField field;
>
>    public FieldFocusFilter(final JavaScriptSupport javaScriptSupport,
> final AbstractField field) {
>        this.javaScriptSupport = javaScriptSupport;
>        this.field = field;
>    }
>
>    @Override
>    public void renderMarkup(final MarkupWriter writer, final JSONObject reply,
>            final PartialMarkupRenderer renderer) {
>        renderer.renderMarkup(writer, reply);
>        this.javaScriptSupport.autofocus(FieldFocusPriority.OVERRIDE,
> this.field.getClientId());
>    }
> }
>
> In my page-class I then inject the PageRenderQueue, JavaScriptSupport
> and the field to put focus on:
>    @Inject
>    private PageRenderQueue pageRenderQueue;
>
>    @Inject
>    private JavaScriptSupport javaScriptSupport;
>
>    @Component
>    private TextField focusField;
>
> In my onAddRowFromXxx-method I add a new instance of the
> FieldFocusFilter to the PageRenderQueue:
>        this.pageRenderQueue.addPartialMarkupRendererFilter(new
> FieldFocusFilter(this.javaScriptSupport, this.focusField));
>
> Thanks for the inspiration Josh. :-)
>
> Regards,
> Joakim
>
>
> On Sat, Feb 26, 2011 at 6:16 PM, Joakim Olsson <jo...@unbound.se> wrote:
>> Hi,
>>
>> I would like to get focus on the first available field in the added
>> row in an AjaxFormLoop. Is it possible in an easy way?
>>
>> Regards,
>> Joakim
>>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org