[ http://issues.apache.org/struts/browse/WW-1350?page=comments#action_38026
]
Chris Mathews commented on WW-1350:
-----------------------------------
This is actually very valuable and I have implemented this as a feature in our
in-house Web Framework similar to AppFuse (built on top of WebWork 2.2 and
Sitemesh).
I would recommend staying away from a focus attribute on the tag though because
it makes it really difficult to implement for the developer that is putting in
custom logic to enable/disable fields during form creation. I would opt to go
down more of a css route for disabling/enabling forms/fields "eligible" for
focus.
Example Javascript (uses Prototype):
hasClassApplied = function(element, classname) {
if (element != undefined && (Element.classNames(element) != undefined ||
Element.classNames(element) != null)) {
return Element.classNames(element).include(classname);
}
return false;
}
focusFirstPageInput = function(excludeFormClassname) {
for (i = 0; i < document.forms.length; i++) {
if (!hasClassApplied(document.forms[i], excludeFormClassname)) {
focusFirstFormInput(document.forms[i]);
return;
}
}
}
focusFirstFormInput = function(form) {
for (i = 0; i < form.length; i++) {
if (form[i].type != "hidden" && form[i].type != "submit" && form[i].type !=
"button") {
if (!form[i].disabled) {
Field.activate(form[i]);
return;
}
}
}
}
We then used an "event" tag (could fit well in ww:head also) that drops an
onload event looking similar to:
addLoadEvent( function() {
focusFirstPageInput('exclude-from-focus');
});
So basically what this does is puts focus on the first element that is enabled
on the first form that isn't marked with the "exclude-from-focus" css class.
You could take it another step and allow exclusion on the individual field
level if you really wanted to but I haven't encountered a case where we would
need that... typically you either want focus applied to the first active form
field or not to the entire form.
Also if you are opening this field focus discussion then you will probably want
to add a similar feature for focusing on the first field error if any are found
(we did this too). This is a very similar problem and if you solve one it is
easy to solve the other. The only problem I ran into was that WebWork did not
apply an error style to inputs with validation errors... it only marked the
input label with an error style. This was somewhat painful since getting the
"for" attribute off the label in order to apply field focus is different in IE
and FireFox and just activating the label is not enough. Ultimately though
this was a simple workaround but I would hope in the future WebWork begins to
apply css styles a bit more uniformly.
> Autofocus first enabled field on a form
> ---------------------------------------
>
> Key: WW-1350
> URL: http://issues.apache.org/struts/browse/WW-1350
> Project: Struts 2
> Issue Type: New Feature
> Components: Views
> Reporter: Ted Husted
> Fix For: 2.0.0
>
>
> The SAF2 UI tags do not support identifying a field to focus. In SAF1, this
> is done with a little Javascript. For SAF2, we migth consider using a
> different Javascript [1] that automatically seeks the first enabled field on
> the first form. Implementing the feature implies access to the body tag.
> References:
> [1] http://www.codeproject.com/jscript/FocusFirstInput.asp
> [2] http://forums.opensymphony.com/thread.jspa?messageID=67350𐜖
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://issues.apache.org/struts/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira