PrimeFaces:如何从树中引用dataTable,两者都嵌套在同一个tabView中

时间:2014-01-12 12:08:17

标签: jsf jsf-2 primefaces

我需要一些帮助来“ajaxify”一个树和一个dataTable,它们都嵌套在同一个tabView中。我搜索了primefaces论坛(did a post about it there)和stackoverflow,但没有找到相关的问题。

基本上,我有一个p:tabView组件,其选项卡包含树组件和dataTable组件。当我选择另一个选项卡时,树正确更新。但是当我从树组件中选择一个节点时,我无法工作的是更新dataTable。

的确,如果我在p:tree中添加一个p:ajax标签:

<p:ajax event="select"
update=":form:myTabView:#{myBean.currentTabIndex}:myDataTable"
listener="#{myController.doSelectNode}" />

为了触发此更新,页面不再加载,给我例外:

找不到表达式为“:form:myTabView:0:myDataTable”的组件,引自“form:myTabView:0:tree”。

从生成的html中我可以看到它生成<table id="form:myTabView:0:myDataTable">

,这看起来很奇怪

我已尝试更新属性的所有其他可能值(“myTabView:0:myDataTable”,“myDataTable”......),但无济于事。

环境:PrimeFaces 4.0 / JSF 2.1.2 / Tomcat 7.0.47

这是代码。请注意,它在tabView之外运行良好,p:ajax更新值为“:form:myDataTable”。

简化的xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui">
<h:head>
    <title>Bienvenue</title>
</h:head>
<h:body>
    <h:form id="form">
        <p:tabView id="myTabView" value="#{myBean.tabs}" var="currentTab"
            dynamic="true" cache="true" activeIndex="#{myBean.currentTabIndex}">

            <p:tab id="tab" title="#{currentTab.title}">
                <h:panelGrid columns="2">
                    <h:panelGroup>
                        <p:tree id="tree" value="#{myBean.treeList[myBean.currentTabIndex]}" var="node"
                            selectionMode="single" selection="#{myBean.selectedNode}"
                            cache="true">

                            <p:ajax event="select"
                                update=":form:myTabView:#{myBean.currentTabIndex}:myDataTable"
                                listener="#{myController.doSelectNode}" />

                            <p:treeNode>
                                <h:outputText value="#{node}" />
                            </p:treeNode>
                        </p:tree>
                    </h:panelGroup>
                    <h:panelGroup>
                        <h:dataTable id="myDataTable" value="#{myBean.selectedUIModelList}"
                            var="uIModel">
                            <h:column>
                                <p:panel header="" closable="true">
                                    <h:outputText value="#{uIModel.name}" />
                                </p:panel>
                            </h:column>
                        </h:dataTable>
                    </h:panelGroup>
                </h:panelGrid>
            </p:tab>

        </p:tabView>
    </h:form>

</h:body>
</html>

MyBean.java

@ManagedBean
@SessionScoped
public class MyBean {
    private List<MyTab> tabs;
    private int currentTabIndex;
    private TreeNode selectedNode;
    private List<TreeNode> treeList;
    private List<UIModel> selectedUIModelList;

    public MyBean() {
        currentTabIndex = 0;

        // build tabs list
        tabs = new ArrayList<>();
        MyTab tab = new MyTab();
        tab.setTitle("Tab1");
        tabs.add(tab);
        tab = new MyTab();
        tab.setTitle("Tab2");
        tabs.add(tab);

        treeList = new ArrayList<>();

        // build trees
        // tree for Tab1
        TreeNode root = new DefaultTreeNode("Root", null);
        TreeNode parentNode1_1 = new DefaultTreeNode("tab1_node1", root);
        TreeNode childNode1_1_1 = new DefaultTreeNode("tab1_node1_1", parentNode1_1);
        TreeNode childNode1_1_2 = new DefaultTreeNode("tab1_node1_2", parentNode1_1);
        TreeNode parentNode1_2 = new DefaultTreeNode("tab1_node2", root);
        TreeNode childNode1_2_1 = new DefaultTreeNode("tab1_node2_1", parentNode1_2);
        treeList.add(root);

        // tree for Tab2        
        TreeNode root2 = new DefaultTreeNode("Root", null);
        TreeNode parentNode2_1 = new DefaultTreeNode("tab2_node1", root2);
        TreeNode childNode2_1_1 = new DefaultTreeNode("tab2_node1_1", parentNode2_1);
        TreeNode childNode2_1_2 = new DefaultTreeNode("tab2_node1_2", parentNode2_1);
        TreeNode parentNode2_2 = new DefaultTreeNode("tab2_node2", root2);
        TreeNode childNode2_2_1 = new DefaultTreeNode("tab2_node2_1", parentNode2_2);
        treeList.add(root2);        

        //instantiate UIModel list
        selectedUIModelList = new ArrayList<>();
    }

        ... Getters and setters ...

}

MyTab.java

public class MyTab {
    private String title;

        ... Getters and setters ...
}

UIModel.java

public class UIModel {
    private String name;

        ... Getters and setters ...
}

MyController.java

@ManagedBean
public class MyController {
    @ManagedProperty("myBean")
    private MyBean myBean;

    public void doSelectNode(NodeSelectEvent event) {
        String selectedStringNode = ((String)event.getTreeNode().getData());
        UIModel newUIModel = new UIModel();
        newUIModel.setName(selectedStringNode);
        myBean.getSelectedUIModelList().add(newUIModel);
    }

        ... Getters and setters ... 
}

你能说明我犯了错误的地方,还是指向相关的资源?

感谢您的考虑。

1 个答案:

答案 0 :(得分:1)

已经尝试过没有隐式索引? :form:myTabView:myDataTable。我知道你必须在编码视图中指明层次结构,而不是在生成的层次结构中指明