jsr372-experts@javaserverfaces-spec-public.java.net

[jsr372-experts] Re: Handling focus after an Ajax update

From: Michael Müller <michael.mueller_at_mueller-bruehl.de>
Date: Fri, 03 Apr 2015 14:08:43 +0200

Remembering the last active element would be a fine solution.

Or, we might introduce something like
<f:ajax execute="..." render="..." focus="elementId"/>

elementId = someId|_at_this|_at_current

The focus will be set only, and only if this optional attribute is provided.

Such an attribute might offer more flexibility than just the current
element.

-----

But focus and re-rendering fields might be a much bigger issue from the
poit of an application developer (or an application user).

1.) if an ajax call is made to one elment due to a change in this
element, than it would be great to re-focus this element

If the user is still entering info, than the next character shall be
inserted at the former caret position. Thus, only re-focussing the
element is not enough.
When re-rendering that element, all the input made in between the
request and the response might get lost. This happens, if the user types
fast and/or the request/response cycle takes some more time.

2.) A data record might contain a list. This list is displayed as a
<h:datatable .../>. Every field should be editable. And the table shall
always contain an empty last line to allow the user to enter additional
data. Thus, if the user starts to enter some data in that empty line, a
new line shall be added. On re-rendering, focus and caret have to be set
onto the right field and position. Here, a focus="@this" will solve the
focus problem. But the caret position and data-loss problem are still
not tackled. To solve this problem, it would be nice to modify the ajax
call: Do not send the whole table, but the new line (keeping focus on
the current element).

The data-loss problem might be solved, if the active component is
excluded from re-rendering.
<f:ajax render="@form" renderExclude="@this"/>
renders the form except this element (keeps it un-touched within the DOM.
<f:ajax render="tableId" renderExclude="@current"/>
re-renders the table with the given id, except the current cell (it's
just an idea, I haven't checked yet, whether this would be possible at all)

Herzliche Grüße - Best Regards,

Michael Müller

Read my book "Web Development with Java and JSF": https://leanpub.com/jsf


Am 02.04.2015 um 19:05 schrieb Josh Juneau:
> +1 - I agree that it would be great to have a solution for this issue.
>
> Josh Juneau
> juneau001_at_gmail.com
> http://jj-blogger.blogspot.com
> https://www.apress.com/index.php/author/author/view/id/1866
>
>
> On Thu, Apr 2, 2015 at 10:34 AM, Kito Mann <kito.mann_at_virtua.com> wrote:
>
>> Interesting. That won't work with input controls that use JS, HTML, and
>> CSS like the PrimeFaces SelectOneMenu, though.
>>
>>
>> On Thursday, April 2, 2015, Bauke Scholtz <balusc_at_gmail.com> wrote:
>>
>>> Hi,
>>>
>>> Haven't checked MyFaces, but this problem doesn't occur in Mojarra. I
>>> recall that this problem was indeed addressed about two years ago, I only
>>> can't find the associated issue report and fix version anymore. The jsf.js
>>> was been altered to recognize input elements and then only copy the
>>> attributes instead of the whole element.
>>>
>>> Cheers, Bauke
>>>
>>>
>>> On Thu, Apr 2, 2015 at 10:31 AM, Thomas Andraschko <
>>> andraschko.thomas_at_gmail.com> wrote:
>>>
>>>> Hi,
>>>>
>>>> in PrimeFaces we do the following:
>>>> - remember the current activeElement id before processing the changes
>>>> node / replacing or adding elements to the DOM
>>>> - replace/add DOM elements
>>>> - re-focus last active element
>>>>
>>>> I'm not sure if it's the perfect solution but it works fine for simple
>>>> cases. It might not work when you replace inputs, which are completely new
>>>> and has different ids.
>>>>
>>>> Regards,
>>>> Thomas
>>>>
>>>> 2015-04-02 10:18 GMT+02:00 arjan tijms <arjan.tijms_at_gmail.com>:
>>>>
>>>>> Hi,
>>>>>
>>>>> On Thursday, April 2, 2015, Kito Mann <kito.mann_at_virtua.com> wrote:
>>>>>
>>>>>> Am I really the only one with an opinion on this?
>>>>>>
>>>>>
>>>>> As an application developer I can indeed confirm this is an issue.
>>>>> JavaScript/DOM stuff is however not my expertise, so I wouldn't be the best
>>>>> candidate on the EG to propose a solution for this, unfortunately.
>>>>>
>>>>> Kind regards,
>>>>> Arjan Tijms
>>>>>
>>>>>
>>>>>>
>>>>>> ___
>>>>>>
>>>>>> Kito D. Mann | @kito99 | Author, JSF in Action
>>>>>> Virtua, Inc. | http://www.virtua.com | JSF/Java EE training and
>>>>>> consulting
>>>>>> http://www.JSFCentral.com | @jsfcentral
>>>>>> +1 203-998-0403
>>>>>>
>>>>>> * Listen to the Enterprise Java Newscast: *http://
>>>>>> <http://blogs.jsfcentral.com/JSFNewscast/>enterprisejavanews.com
>>>>>> <http://ww.enterprisejavanews.com>*
>>>>>> * JSFCentral Interviews Podcast:
>>>>>> http://www.jsfcentral.com/resources/jsfcentralpodcasts/
>>>>>> * Sign up for the JSFCentral Newsletter:
>>>>>> http://oi.vresp.com/?fid=ac048d0e17
>>>>>>
>>>>>> On Thu, Mar 19, 2015 at 10:20 AM, Kito Mann <kito.mann_at_virtua.com>
>>>>>> wrote:
>>>>>>
>>>>>>> Hello everyone,
>>>>>>>
>>>>>>> One of the annoying things about Ajax updates with JSF is the fact
>>>>>>> that focus can easily get lost. One of my clients actually resorted to
>>>>>>> synchronous "Ajax" requests to avoid this problem (to my chagrin). Here's a
>>>>>>> good description/solution to the problem:
>>>>>>> http://www.knitelius.com/2014/07/15/keeping-focus-on-element-with-jsf-2-ajax-render/
>>>>>>> .
>>>>>>>
>>>>>>> Given that this is a common pain point, shouldn't we provide a
>>>>>>> solution out of the box?
>>>>>>> ___
>>>>>>>
>>>>>>> Kito D. Mann | @kito99 | Author, JSF in Action
>>>>>>> Virtua, Inc. | http://www.virtua.com | JSF/Java EE training and
>>>>>>> consulting
>>>>>>> http://www.JSFCentral.com | @jsfcentral
>>>>>>> +1 203-998-0403
>>>>>>>
>>>>>>> * Listen to the Enterprise Java Newscast: *http://
>>>>>>> <http://blogs.jsfcentral.com/JSFNewscast/>enterprisejavanews.com
>>>>>>> <http://ww.enterprisejavanews.com>*
>>>>>>> * JSFCentral Interviews Podcast:
>>>>>>> http://www.jsfcentral.com/resources/jsfcentralpodcasts/
>>>>>>> * Sign up for the JSFCentral Newsletter:
>>>>>>> http://oi.vresp.com/?fid=ac048d0e17
>>>>>>>
>>>>>>
>>>>>>
>>>>
>>>
>>
>> --
>> ___
>>
>> Kito D. Mann | @kito99 | Author, JSF in Action
>> Virtua, Inc. | http://www.virtua.com | JSF/Java EE training and consulting
>> http://www.JSFCentral.com | @jsfcentral
>> +1 203-998-0403
>>
>> * Listen to the Enterprise Java Newscast: *http://
>> <http://blogs.jsfcentral.com/JSFNewscast/>enterprisejavanews.com
>> <http://ww.enterprisejavanews.com>*
>> * JSFCentral Interviews Podcast:
>> http://www.jsfcentral.com/resources/jsfcentralpodcasts/
>> * Sign up for the JSFCentral Newsletter:
>> http://oi.vresp.com/?fid=ac048d0e17
>>
>>
>