I have a table and I want when they click on a button it takes them to the end of the page.
Table:
<p:datatable>
...
<p:column style="width:32px;text-align: center">
<p:commandButton id="btn" update="form" oncomplete="ScrollPage('anchorSecondPanel')" icon="ui-icon-search" title="View">
</p:commandButton>
</p:column>
</p:datatable>
Below I have the anchor:
<a id="anchorSecondPanel"/>
The javascript function:
function ScrollPage(location) {
window.location.hash=location;
}
The problem is that when I click on the button the first time it works, but the second time it doesn't direct to that anchor anymore.
Does anyone know the reason?
The problem is that when you change the value of the
hash
, it works the first time but when you click on that button again, thehash
is the same so the URL will be the same and the browser will do nothing (because there is no real change to apply ).One solution (I don't know if it's the best, but it will work) is to change the
hash
twice: first to a value that doesn't exist, and then to the value you want to go to. That way, with the first change the browser will reset thehash
and with the second (which occurs immediately after) the scroll itself will be done:Without PrimeFaces, but here you can see a demo with the code working: