How to display Parent and Child ( two different ob

2019-05-31 02:26发布

问题:

I am using Treetable component to display parent and child objects. I can able to display the parent content by using this tutorial.

In my case, I am using two different objects to display the contents in the TreeTable component. Those objects are following.

  1. University Detail - It is my parent Object. It contains (Id and Name of each university) as well as child colleges.
  2. College Detail - It is my child Object. It contains (Id and Name) of each college.

UniversityDetail Class

public class UniversityDetail implements Serializable {

    /**
     * serialVersionUID -   
     */
    private static final long serialVersionUID = 1L;
    /**
     * UniversityId -   
     */
    private Integer UniversityId;
    /**
     * UniversityName   -   
     */
    private String UniversityName;
    /**
     * CollegeDetail    -
     */
    private List<CollegeDetail> CollegeDetail   =   new ArrayList<CollegeDetail>();

    public Integer getUniversityId() {
        return UniversityId;
    }

    public void setUniversityId(Integer UniversityId) {
        this.UniversityId = UniversityId;
    }

    public String getUniversityName() {
        return UniversityName;
    }

    public void setUniversityName(String UniversityName) {
        this.UniversityName = UniversityName;
    }

    public List<CollegeDetail> getCollegeDetail() {
        return CollegeDetail;
    }

    public void setCollegeDetail(
            List<CollegeDetail> collegeDetail) {
        this.CollegeDetail = collegeDetail;
    }

}

College Detail

public class CollegeDetail implements Serializable{

    /**
     * CollegeId    -   
     */
    private Integer CollegeId;
    /**
     * CollegeName  -
     */
    private String CollegeName;

    public Integer getCollegeId() {
        return CollegeId;
    }

    public void setCollegeId(Integer CollegeId) {
        this.CollegeId = CollegeId;
    }

    public String getCollegeName() {
        return CollegeName;
    }

    public void setCollegeName(String CollegeName) {
        this.CollegeName = CollegeName;
    }

}

How to display these detail classes with the treetable component ? I dont know how to iterate the child elements(College Detail)

I have the following xhtml code to display the parent and child. I could render the parent detail. But I am failing to render the child class detail. Any help is much appreciated.

<p:treeTable value="#{ManagedBean.root}" var="object" id="multiSelect"  
             selection="#{ManagedBean.selectedNodes}" selectionMode="checkbox">  

    <f:facet name="header">  
        College Dropdown  
    </f:facet>  

    <p:column style="width:100px">  
        <f:facet name="header">  
            University Name  
        </f:facet>  
        <h:outputText value="#{object.universityName}" />  
    </p:column>  

    <p:column style="display:none">  
        <f:facet name="header">  
            University ID
        </f:facet>  
        <h:outputText value="#{object.universityId}" />  
    </p:column>  

</p:treeTable>

ManagedBean

RootTree = new CheckboxTreeNode("Root", null);
for(int i =0 ; i<CompanyDetail.size();i++) {
UniversityDetail universityDetail   = UniversityDetail.get(i);
    List<CollegeDetail> collegeDetail   =   universityDetail.getCollegeDetail();
    TreeNode parent = new CheckboxTreeNode(universityDetail,RootTree);
    for(int j=0; j<collegeDetail.size();j++) {
    TreeNode child = new CheckboxTreeNode(collegeDetail.get(j).getCollegeName(),collegeDetail.get(j), parent);                  
   }

回答1:

Your XHTML page is different form back bean. But in your case you can make another Class which have only those properties which is needs to be display. and use that class to be display on XHTML pages. lets us know if not work.



回答2:

Solution:

ManagedBean tree getter:

private TreeNode tree;

public TreeNode getTree() {
    if (tree == null) {

        tree = new CheckboxTreeNode("root", null);

        for (UniversityDetail u : universityDetailList) {
            TreeNode uNode = new CheckboxTreeNode("university" ,u ,tree);
            for (CollegeDetail c : u.getCollegeDetail()) {
                TreeNode cNode = new CheckboxTreeNode("collage" ,c ,uNode);
            }
        }
    }

    return tree;

}

xhtml:

<p:treeTable value="#{ManagedBean.tree}" var="object" nodeVar="node" id="multiSelect"  
             selection="#{ManagedBean.selectedNodes}" selectionMode="checkbox">  

    <f:facet name="header">  
        College Dropdown  
    </f:facet>  

    <p:column> 

        <p:treeNode type="university" rendered="#{node.type == 'university'}">
            <h:outputText value="#{object.universityName}" />
        </p:treeNode>

        <p:treeNode type="college" rendered="#{node.type == 'college'}">
            <h:outputText value="#{object.collegeName}" />
        </p:treeNode>

    </p:column>  

    <p:column> 

        <p:treeNode type="university" rendered="#{node.type == 'university'}">
            <h:outputText value="#{object.universityId}" />
        </p:treeNode>

        <p:treeNode type="college" rendered="#{node.type == 'college'}">
            <h:outputText value="#{object.collegeId}" />
        </p:treeNode>

    </p:column>  

</p:treeTable>

Tip: use rendered atribute with node type:

rendered="#{node.type == 'university'}"