0

column当我在中时,我似乎无法触发事件dataTable。这是我的简单演示

<h:form id="form">
    <!--This section of p:tree here seems to be the reason causing the event not fired when click the command button-->
    <p:tree value="#{viewBean.root}" var="node" dynamic="true" cache="false"  
                selectionMode="checkbox"  selection="#{treeBean.selectedNode}">  

        <p:ajax event="expand" update=":form:messages" listener="#{viewBean.onNodeExpand}" />  
        <p:ajax event="collapse" update=":form:messages" listener="#{viewBean.onNodeCollapse}" />  
        <p:ajax event="select" update=":form:messages" listener="#{viewBean.onNodeSelect}" />  
        <p:ajax event="unselect" update=":form:messages" listener="#{viewBean.onNodeUnselect}" />  

        <p:treeNode>  
            <h:outputText value="#{node}" />  
        </p:treeNode>  
    </p:tree>
    <h:panelGroup id="mygroup">
            <p:dataTable id="mytable" value="#{viewBean.foodList}" var="item">
                <p:column>
                    #{item}
                </p:column>
                <p:column>
                    <p:commandButton value="delete" 
                                     action="#{viewBean.delete}"
                                     update=":form:mygroup">
                        <f:setPropertyActionListener target="#{viewBean.selectedFood}"
                                                     value="#{item}"/>
                    </p:commandButton>
                </p:column>
            </p:dataTable>
    </h:panelGroup>
</h:form>

这是我的托管豆

@ManagedBean
@ViewScoped
public class ViewBean {

    private TreeNode root;  

    private TreeNode selectedNode;  
    private List<String> foodList;

    private String selectedFood;

    @PostConstruct
    public void init(){

        foodList = new ArrayList<String>();
        foodList.add("Pizza");
        foodList.add("Pasta");
        foodList.add("Hamburger");

        root = new DefaultTreeNode("Root", null);  
        TreeNode node0 = new DefaultTreeNode("Node 0", root);  
        TreeNode node1 = new DefaultTreeNode("Node 1", root);  
        TreeNode node2 = new DefaultTreeNode("Node 2", root);  

        TreeNode node00 = new DefaultTreeNode("Node 0.0", node0);  
        TreeNode node01 = new DefaultTreeNode("Node 0.1", node0);  

        TreeNode node10 = new DefaultTreeNode("Node 1.0", node1);  
        TreeNode node11 = new DefaultTreeNode("Node 1.1", node1);  

        TreeNode node000 = new DefaultTreeNode("Node 0.0.0", node00);  
        TreeNode node001 = new DefaultTreeNode("Node 0.0.1", node00);  
        TreeNode node010 = new DefaultTreeNode("Node 0.1.0", node01);  

        TreeNode node100 = new DefaultTreeNode("Node 1.0.0", node10);  
    }

    public void delete(){

        foodList.remove(selectedFood);

    }
    //setter and getter

    public void onNodeExpand(NodeExpandEvent event) {  
       FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Expanded", event.getTreeNode().toString());  

       FacesContext.getCurrentInstance().addMessage(null, message);  
    }  

    public void onNodeCollapse(NodeCollapseEvent event) {  
       FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Collapsed", event.getTreeNode().toString());  

       FacesContext.getCurrentInstance().addMessage(null, message);  
    }  

    public void onNodeSelect(NodeSelectEvent event) {  
       FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", event.getTreeNode().toString());  

       FacesContext.getCurrentInstance().addMessage(null, message);  
    }  

    public void onNodeUnselect(NodeUnselectEvent event) {  
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Unselected", event.getTreeNode().toString());  

        FacesContext.getCurrentInstance().addMessage(null, message);  
    }  
}

我的问题似乎是事件没有被触发,这意味着delete永远不会被调用。

我环顾四周,知道 Mojarra 在嵌套 UIData 组件方面有很多问题,所以我尝试了 Mojarra 2.1.4,仍然没有解决问题。我试过Myfaces 2.1.5,还是不行!!!这是 Primefaces 的错误吗?我什h:panelGroup至用作包装器,update但它仍然不起作用。

编辑:事实证明,p:tree上面的dataTable是单击 时未触发事件的根本原因commandButton。如果我删除p:tree它,它会立即工作。请注意p:tree,当我单击结帐时,它本身工作得很好,在我的托管 bean 上触发了事件。

4

1 回答 1

5

这是您自己的代码中的错误。

<partial-response>
  <error>
    <error-name>class javax.el.ELException</error-name>
    <error-message><![CDATA[/index.xhtml @15,84 selection="#{viewBean.selectedNode}": Cannot convert [Lorg.primefaces.model.TreeNode;@fd9b4d of type class [Lorg.primefaces.model.TreeNode; to interface org.primefaces.model.TreeNode]]></error-message>
  </error>
</partial-response>

具体的异常消息表明应该<p:tree selection>引用类型TreeNode[]而不是类型的属性TreeNode(注意[消息中类标识符上的前缀表示数组类型)。所以,相应地修复它:

<p:tree selection="#{treeBean.selectedNodes}">

private TreeNode[] selectedNodes;

无论如何,你都在<p:tree><p:dataTable>一个“神”形式中。因此,它们中的任何一个中的任何操作也将提交另一个组件上的所有内容。如果树和表没有任何关系,那么它们应该各自放在自己的<h:form>. 或者,您必须process向命令 links/buttons 添加一个属性以仅处理包含的感兴趣的组件,例如process="mytable"在表格内的按钮上。

于 2011-12-28T21:23:09.497 回答