滚动到树Primefaces中的选定节点

时间:2015-10-14 12:40:39

标签: jsf primefaces tree

我创建了一个树,并希望通过过滤器滚动选定节点。 如以下示例在互联网上找到的JSFIDDLE:TreeNodeJsFiddle

我的代码:

<p:accordionPanel id="apFilter">

<p:remoteCommand name="remoteFilter"
    actionListener="#{mybean.searchNode}"
    update="tree,:form:dataTree, :form:dataTree1, :form:chart" />
<p:outputLabel value="Search: " />
<p:inputText id="filterTree" value="#{myBean.filter}"
    style="width:100px"
    onkeypress="if (event.keyCode == 13) { test(); return false; }" />

<p:outputPanel id="scroll"
    style="width: 100%;height: 100%;overflow: auto;display: block">

    <!-- tree  -->
    <p:tree filter="true" value="#{myBean.root}" var="doc" id="tree"
        style="width:250px; font-size:10px" dynamic="true" cache="false"
        selectionMode="single" selection="#{myBean.selectedNode}">
        <p:ajax event="expand"
            update=":form:dataTree, :form:dataTree1, :form:chart"
            listener="#{myBean.onNodeExpand}" />
        <p:ajax event="select"
            update=":form:dataTree1,:form:dataTree, :form:chart"
            listener="#{myBean.onNodeSelect}" />
        <p:ajax event="unselect"
            update=":form:dataTree,:form:dataTree1,:form:chart"
            listener="#{myBean.onNodeUnselect}" />
        <p:ajax event="collapse"
            update=":form:dataTree1,:form:dataTree, :form:chart"
            listener="#{myBean.onNodeCollapse}" />

        <p:treeNode type="group1" expandedIcon="ui-icon-folder-open"
            collapsedIcon="ui-icon-folder-collapsed">
            <h:outputText value="#{doc.name}" />
        </p:treeNode>

        <p:treeNode type="group2" expandedIcon="ui-icon-folder-open"
            collapsedIcon="ui-icon-folder-collapsed">
            <h:outputText value="#{doc.name}" />
        </p:treeNode>

        <p:treeNode type="group3" icon="ui-icon-document">
            <h:outputText value="#{doc.name}" />
        </p:treeNode>


    </p:tree>
</p:outputPanel>

我尝试按照链接上的建议使用scrollTo: http://forum.primefaces.org/viewtopic.php?f=3&t=18856   但是没有成功。

 public void searchNode() {
    selectedNode = myBeanRN.searchFilter(filter);   
    RequestContext context = RequestContext.getCurrentInstance();
    context.scrollTo("apFilter:tree:"+selectedNode.getRowKey());
}

有没有人知道如何转到所选节点,等同于jsFiddle的例子?

1 个答案:

答案 0 :(得分:1)

我用过

 <p:scrollPanel>
 <p:tree>
  ...
 </p:tree>
 </p:scrollPanel>
选择树元素

和js

var selectedElement = treeWidgetVar.jq.find('span .ui-state-highlight');
if (selectedElement != null && selectedElement != undefined && selectedElement.position() != undefined) {
     var scrollPanel = treeWidgetVar.jq.parent();
     scrollPanel.scrollTop(selectedElement.position().top - scrollPanel.height() / 2);
}