Anchors in DataTable
Hi all,
I did quite a bit of research on this.
I have a datatable, with a valuechange field in each row. the datable has 50 rows on the page.
So if the user enters 37th row, the valueChangeListener refreshes the page and brings it to the top of the page again.
To overcome this, i tried to set focus on the element that caused this valuechangelistener even to fire up. and i did it successfully, BUT, after the focus is set, the page keeps on refreshing for some reason.
Then i tried to defined anchors for each row using <h:outputText> and when the page loads i am
using :
<script>
function setFocus(){
var fieldname ="";
fieldname ="form2:payments:23:dYear";// this is just to test the anchor.
document.location.href="#"+fieldname;
}
setFocus();
</script>
again, this works, i am able to set focus on to that row, but again it refreshes the page and brings it to the top.
any ideas?
# 1
Nothing works.
I tried in all possible ways. the entire HTML of the JSP looks fine.
[code]
<a name='f23'></a>
<script>
function setFocus(){
var fieldname = "";
fieldname = "f23"; // this is just to test the anchor.
document.location.href="#"+fieldname;
}
setFocus();
</script>
It takes me to that row, but then again goes back to the top of the page. this thing is eating me.
# 8
> even without the timeout
No .. You have to try to use it with setTimeout().
<f:verbatim>
<script>
setTimeout("setFocusAnchor()", 100);
</script>
</f:verbatim>
</body>
Just give the page some time to finish rendering. Try 100ms, 50ms or even 10ms.
# 9
Ok.
i tried even with 500ms.no luck. were you able to go to the anchor successfully?
let me show you what i did.
i defined anchor using <h:outputText>
and it shows up in the end html as :
<span id="form2:emps:23:anchor"><a name='f23'></a></span>
and in the setFocus function:
function setFocus(){
location.href="#"+fieldname;
}
and i used what you gave me:
</TABLE>
<f:verbatim>
<script>
setTimeout("setFocus()", 500);
</script>
</f:verbatim>
</BODY>
am i doing anything wrong here?
the anchor i showed you was for one particular row.
# 10
function setFocus()
{
location.href="#" + fieldname;
}
<script>setTimeout("setFocus()", 500);</script>
The parameter fieldname is missing. Try
function setFocus(fieldname)
{
location.href="#" + fieldname;
}
<script>setTimeout('setFocus(\'nameofmyancor\')', 500);</script>
(I didn't investigate your problem, have never used DataTable and don't know, if this is the solution. I saw just the missing parameter.)
You probably have to create the script dynamically due to the needed name of the ancor.
In javascript the parameter can be omitted in the signature of the function but it must be passed through the call.