JSF scroll bar at the top of the page
Asked Answered
M

2

5

I was wondering how to make the scroll bar of the browser. go to top of page. I am developing a registration screen that is too big, and it contains some fields that are needed. It displayed an error message to the user if the required fields are not informed. The problem is that the scroll bar does not go up, and I want her to go to the top of the page. I'm using Primefaces and JQuery, but no this working.

Morganmorgana answered 14/2, 2013 at 19:59 Comment(1)
correcting: I want the scrollbar climb to the top of the page, for to do so in jsf this a little tricky. Thank you ... =]Morganmorgana
M
6
<div id="msg">

Content page

 onclick="javascript:window.location='#msg'"
Morganmorgana answered 15/2, 2013 at 14:15 Comment(0)
G
2

I must say that your question looks like some discussion between two people, but I will try to give an answer. I suppose you have p:commandButton for submit the form.

First add JavaScript function:

<script type="text/javascript">
  function handleResponse(xhr, status, args) {
    if (args.validationFailed) {
      window.scrollTo(0, 0);
    }
  }
</script>

validationFailed is callback parameter which is added implicitly by PrimeFaces in case hen validation fails.

Now, commandButton:

<p:commandButton value="Submit" actionListener="#{myBean.submit}" oncomplete="handleResponse(xhr, status, args)"/>

This will call JavaScript function after AJAX request is completed.

This is as much as I can suggest, you didn't provide much information. Adapt this code to your needs.

Genitourinary answered 14/2, 2013 at 21:13 Comment(7)
this almost everything ok, the problem is when called window.scrollTo (0, 0); does not work. The scroll bar does not go to the top of the page.Morganmorgana
That function should be supported on all mayor browsers, problem must be in something else. Do you see any errors in JavaScript console?Genitourinary
Shows no error on the console. I put an alert () inside the function to see if it worked. And it works, but when I use window.scrollTo (0, 0); does not work.Morganmorgana
<p:commandButton id="salvarCommandButton" icon="ui-icon-disk" value="#{wds['button.create']}" update="@form" actionListener="#{controladorAtivo.salvar}" oncomplete="handleResponse(xhr, status, args)" process="@this :ativoTabView:formCriar:tabView:dadosGeraisTab" />Morganmorgana
Than maybe just validationFailed is not true. Debug value of this field.Genitourinary
ValidationFailed return the value true when there are validation errors, and it is false when there is no validation error.Morganmorgana
console.log(args.validationFailed); retorn true!Morganmorgana

© 2022 - 2024 — McMap. All rights reserved.