简体   繁体   English

在JSF / Primefaces页面中自动滚动

[英]AutoScrolling in the JSF/Primefaces page

I am using primefaces and JSF2.0 to built page. 我正在使用primefaces和JSF2.0来构建页面。

In the page I have two Panels when I click on the button "Click-me" one panel one, it rendered the second panel in the same page, what I want is when I click the button "Click-me" page should also scroll down automatically and should bring the Second Panel Header in front of the user, Currently user need to manually scroll down to see the second panel. 在页面中我有两个面板,当我点击“Click-me”按钮时,一个面板一个,它在同一页面呈现第二个面板,我想要的是当我点击按钮“Click-me”页面时也应该滚动自动向下并应将第二个面板标题放在用户面前,目前用户需要手动向下滚动才能看到第二个面板。 Code is as below 代码如下

XHTML Page XHTML

<h:form id="form1">
    <p:panel header="Panel One" id="PanelOne" >
        <h:panelGrid styleClass="panelGrid" columns="2" id="PanelOneGrid" width="90%" >

            <h:outputText value="First Input Field Panel One" ></h:outputText>
            <p:inputTextarea rows="20" cols="80"></p:inputTextarea>

            <h:outputText value="Second Input Field Panel One" ></h:outputText>
            <p:inputTextarea rows="20" cols="80"></p:inputTextarea>

            <h:outputText value="" ></h:outputText>
                <p:commandButton value="Click-me" update="form1" action="#{autoScrollBean.clickAction}">
                </p:commandButton>

        </h:panelGrid>
    </p:panel>  

    <p:panel header="Second One" id="PanelTwo" rendered="#{autoScrollBean.renderPanelTwo}">
        <h:panelGrid styleClass="panelGrid" columns="2" id="PanelTwoGrid" width="90%" >

            <h:outputText value="First Input Field Panel Two" ></h:outputText>
            <p:inputTextarea rows="20" cols="80"></p:inputTextarea>

            <h:outputText value="Second Input Field Panel Two" ></h:outputText>
            <p:inputTextarea rows="20" cols="80"></p:inputTextarea>

        </h:panelGrid>
    </p:panel>  

</h:form>

Managed Bean is as below Managed Bean如下

@ManagedBean
@ViewScoped
public class AutoScrollBean {

private boolean renderPanelTwo;

public boolean isRenderPanelTwo() {
    return renderPanelTwo;
}

public void setRenderPanelTwo(boolean renderPanelTwo) {
    this.renderPanelTwo = renderPanelTwo;
}

public void clickAction () {
    System.out.println("Inside Method");
    setRenderPanelTwo(true);
}

} }

EDIT: 编辑:

<p:focus /> 

By using above focus command, curosr goes to the first input field and that input field is shown to the user after auto scroll, But I want to show the user from Panel Header NOT from the first input field. 通过使用上面的焦点命令,curosr转到第一个输入字段,并且在自动滚动后向用户显示该输入字段,但我想从第一个输入字段向Panel Header NOT显示用户。

I did it by using JavaScript and tested in IE only as my requirement doesn't required all browser compatibility hence I didn't tested for other browser. 我是通过使用JavaScript并在IE中测试的,因为我的要求不需要所有浏览器兼容性因此我没有测试其他浏览器。

The JavaScript function need to placed in the header (h:head) section if you are using template structure in the JSF2.0 如果在JSF2.0中使用模板结构,则需要将JavaScript函数放在标题(h:head)部分中

<script type="text/javascript">
    function ScrollPage(location) {
        window.location.hash=location;
    }
</script>

Create the anchor in page where you want to scroll or anchor the page 在页面中创建要滚动或锚定页面的锚点

<a id="anchorSecondPanel">

While clicking command button call this function on oncomplete event 单击命令按钮时会在oncomplete事件上调用此函数

oncomplete="ScrollPage('anchorSecondPanel')"

The complete modified code for xhtml is as below xhtml的完整修改代码如下

<h:form id="form1">
    <p:panel header="Panel One" id="PanelOne" >
        <h:panelGrid styleClass="panelGrid" columns="2" id="PanelOneGrid" width="90%" >

            <h:outputText value="First Input Field Panel One" ></h:outputText>
            <p:inputTextarea rows="20" cols="80"></p:inputTextarea>

            <h:outputText value="Second Input Field Panel One" ></h:outputText>
            <p:inputTextarea rows="20" cols="80"></p:inputTextarea>

            <h:outputText value="" ></h:outputText>
                <p:commandButton value="Click-me" update="form1" action="#{autoScrollBean.clickAction}" oncomplete="ScrollPage('FeatureMainPanel')">
                </p:commandButton>

        </h:panelGrid>
    </p:panel>

    <a id="anchorSecondPanel">
    <p:panel header="Second One" id="PanelTwo" rendered="#{autoScrollBean.renderPanelTwo}">
        <h:panelGrid styleClass="panelGrid" columns="2" id="PanelTwoGrid" width="90%" >

            <h:outputText value="First Input Field Panel Two" ></h:outputText>
            <p:inputTextarea rows="20" cols="80"></p:inputTextarea>

            <h:outputText value="Second Input Field Panel Two" ></h:outputText>
            <p:inputTextarea rows="20" cols="80"></p:inputTextarea>

        </h:panelGrid>
    </p:panel>  

</h:form>

Well just add some javascript at your xhtml. 好吧,只需在您的xhtml添加一些JavaScript。

<script>        
    function scroll()
    {
        var grid = $("#form1\\:PanelTwo .panelGrid");       
        $('html, body').animate({
            scrollTop : grid.offset().top
        },4000);                
    }           
</script>

And call this when you push the button: 按下按钮时调用它:

<p:commandButton value="Click-me" update="form1" action="#{autoScrollBean.clickAction}"  oncomplete="scroll()" >

This will do the scrolltrick 这将做scrolltrick

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM