在JSF中锚/跳转

问题描述 投票:1回答:3

我使用JSF + Primefaces 3.2.1。

页面上有一个p:datatable,每行都有一个按钮“Edit”。当我单击该按钮时,在页面的页脚中呈现一个用于编辑该行的表单。然后我需要向下滚动来改变值..

但是我需要浏览器在基本HTML工作中单击“锚点”之类的“编辑”按钮后自动滚动到那里。

我发现了这个决定:

FacesContext.getCurrentInstance().getExternalContext().redirect("pProvidersPriceAppointment.xhtml#anchor1");

它工作,但随着我的更新=“@形式”无法正常工作..因此底部的表单不呈现。它在刷新页面后呈现。

我怎么能用p:commandButton或h:commandButton?)

我的按钮:

<p:commandButton id="providerEdit" actionListener="#{providersPriceAppointment.setEditProvider(provider.id)}" icon="iconEdit" update="@form"/>

豆法:

public void setEditProvider(int id) throws IOException {
    for (int i = 0; i < providersList.size(); i++) {
        ProvidersExt p = providersList.get(i);
        if (p.getId() == id) {
            providerForEdit = p;
            break;
        }
    }
    enableEdit = true;
    FacesContext.getCurrentInstance().getExternalContext().redirect("pProvidersPriceAppointment.xhtml#anchor1");
}

页脚中的表单:

<a name="anchor1"/>
<p:fieldset id="editFieldset" legend="blablabla" rendered="#{providersPriceAppointment.enableEdit}"/>
    ...
</p:fieldset>
jsf primefaces
3个回答
3
投票

在JSF或Primefaces中没有实现这样的东西。但是,由于您在应用程序(Primefaces)中运行了jQuery Framework,因此可以使用jQuery Animate功能。

要想知道如何实现这样的事情,你可以看看这个答案:

jQuery scroll to Element

对于您的应用程序,将以某种方式:

将此添加到您的<head>元素:

function scrollToAnchor() {
    jQuery('html, body').animate({
        scrollTop: jQuery("#editFieldset").offset().top
    }, 2000);
}

这将是按钮部分:

<p:commandButton id="providerEdit" 
    actionListener="# {providersPriceAppointment.setEditProvider(provider.id)}" 
    icon="iconEdit" onComplete="scrollToAnchor();" update="@form"/>

2
投票

使用PrimeFaces RequestContext和scrollTo来定位组件ID:

https://www.primefaces.org/showcase/ui/misc/requestContext.xhtml

public void save() { 
    RequestContext context = RequestContext.getCurrentInstance();

    ...
    //scroll to panel
    context.scrollTo("form:panel");

0
投票

我在PrimeFaces ShowCase上找到的简单决定:

<p:fieldset id="edit" legend="Here you can edit.." rendered="#{providersPriceAppointment.enableEdit}">
    <p:focus context="panel"/>  
</p:fieldset>

渲染此字段集时,浏览器只需跳转到此处)https://www.primefaces.org/showcase/ui/misc/focus.xhtml

希望,有人会觉得这很有帮助(“,)

© www.soinside.com 2019 - 2024. All rights reserved.