BreadCrumb with PrimeFaces from database

469 views Asked by At

I'm working with folders within a database. Each folder is referenced by its parent :

      id  ref
root   0   0
me     1   0
folder 2   0
inme   3   1

Each folder are displayed when I click on their name. The "commandLink" has got a parameter : the folder ID. It works great but I don't know how to keep tracks of the path : I would like something like root > me > inme.

Is there any way to have virtually in the URL ".../root/me/inme" using backing-beans ?

Thanks

1

There are 1 answers

0
nosnhoj On BEST ANSWER

Just wrote it myself, check this:

Page:

<h:form id="form1">
    <h:outputText id="trackmsg" value="#{MyBean.trackMsg}"/>
    <p:dataTable value="#{MyBean.folderList}" var="folder" style="width: 300px">
        <p:column headerText="name">
            <p:commandLink value="#{folder.name}" action="#{MyBean.track(folder.id)}" update=":form1:trackmsg"/>
        </p:column>
        <p:column headerText="id">
            <h:outputText value="#{folder.id}"/>
        </p:column>
        <p:column headerText="ref">
            <h:outputText value="#{folder.ref}"/>
        </p:column>
    </p:dataTable>
</h:form>

Backing Bean:(Just two key methods:track() and findParent() )

private List<Folder> folderList=new ArrayList<>();
private String trackMsg="";//to show tracks of the path:  root>me>inme

@PostConstruct
public void init(){//Just initialize thr list
    folderList.add(new Folder("root", 0, 0));
    folderList.add(new Folder("me", 1, 0));
    folderList.add(new Folder("folder", 2, 0));
    folderList.add(new Folder("inme", 3, 1));
    folderList.add(new Folder("ininme", 4, 3));
}

public void track(int id){//to composite the track path
    trackMsg="";
    Folder selectedFolder=new Folder();
    for(Folder fold:folderList) //find the selected Folder
        if(fold.getId()==id)
        {
            selectedFolder=fold;
            if(selectedFolder.getId()!=selectedFolder.getRef())//if so, it means the root folder
            trackMsg=">"+selectedFolder.getName();
        }

    Folder parentFolder=findParent(selectedFolder);//get the parent folder of the selected folder

    while(parentFolder!=null && parentFolder.getId()!=parentFolder.getRef()){//while there is parent folder and not root folder
        trackMsg=">"+parentFolder.getName() + trackMsg;
        parentFolder=findParent(parentFolder);//find the parent of parent
    }

    trackMsg="root"+trackMsg;//the last, root folder
}

public Folder findParent(Folder child){//find the parent folder of the passing folder
    for(Folder fold:folderList)
        if(fold.getId()==child.getRef())
            return fold;
    return null;
}

When I click "ininme" in the dataTable, it looks like:

enter image description here