This alternative InputFocus.script specifies the actual input field
to give the focus to. The target input field is identified by an informal
parameter named component. The script then uses
${expression} element name insertions to complete the JavaScript.
Note when using this script, the target input field component must be declared
before the script component in the HTML template, and within the
Form block, so that the target field component can be
resolved by the Script component.
<body jwcid="@Body">
<form jwcid="@Form" listener="ognl:listener.formSubmit">
<table cellpadding=
"4">
<tr><td>Username:</td><td><input jwcid="usernameTextField@TextField" value="ognl:visit.username" size="12"/></td>
</tr>
<tr><td>Password:</td><td><input jwcid="@TextField" value="ognl:visit.password" hidden="ognl:true" size="12"/></td>
</tr>
<tr align="right">
<td colspan="2"><input type="submit" value="Login"/></td>
</tr>
</table>
<span jwcid="@Script" script="/com/mycorp/scripts/FormFocus.script" component="ognl:components.usernameTextField"/>
</form>
</body>
<!-- Sets the focus to the specified input field if not hidden and not disabled. -->
<!-- /com/mycorp/scripts/InputFocus.script -->
<?xml version="1.0"?>
<!DOCTYPE script PUBLIC
"-//Apache Software Foundation//Tapestry Script Specification 3.0//EN"
"http://jakarta.apache.org/tapestry/dtd/Script_3_0.dtd">
<!--
Selects the specified form input field on body load if the input type is not
"hidden" and the input field is not disabled.
Input symbols:
component: the component input field to give focus
-->
<input-symbol key="component" class="org.apache.tapestry.form.AbstractFormComponent" required="yes"/>
<let key="formObject"> document.${component.form.name} </let>
<let key="componentObject"> ${formObject}.${component.name} </let>
<script>
<body>
function setFocus() {
var inputField = ${componentObject.name};
if (inputField.type != "hidden") {
if (inputField.disabled != true) {
inputField.focus();
}
} else {
window.alert('InputFocus.script cannot set focus on a hidden field');
}
}
</body>
<initialization>
setFocus();
</initialization>
</script>
|