1. 程式人生 > >Jquery EasyUI Tree樹形結構的Java實現(實體轉換VO)

Jquery EasyUI Tree樹形結構的Java實現(實體轉換VO)

優勢 con control 項目 util turn ttr real org

前一陣做的OA項目,有一個是組織架構的樹,因為是分開做的,我做的是Controller和頁面,其他組做的Service和Dao,因為之前一直沒有商量頁面用什麽框架做比較好,導致,Dao層取出來的數據都不是Easyui Tree所能識別的,其實後臺返回的也是樹形的結構,但是他們返回來的 **id,而不是Easyui Tree所能識別的id;他們返回的是name,,而不是Easyui Tree所能識別的text,他們返回的是****,,而不是Easyui Tree所能識別的children,因為別人已經做好了,所以也不方便讓別人重新去定義格式,所以我這邊只能通過轉VO,把Service返回的對象重新進行封裝,使之支持Easyui ,話不多說,直接上代碼。

這段是VO代碼;其中主要的就是ID,text,children,要轉的就是這三個

public class OrganizationVo implements Serializable{

    private Integer id;
    private Integer pid;
    private Integer teamId;
    private String text;
    private String state;
    private String leader;
    private Integer sort;
    private List<OrganizationVo> children;

    public OrganizationVo(){}

    public OrganizationVo(Integer id,Integer pid, Integer teamId,String leader,Integer sort,String text,String state, List<OrganizationVo> children) {
        this.id = id;
        this.pid = pid;
        this.teamId = teamId;
        this.text = text;
        this.sort = sort;
        this.leader = leader;
        this.state = state;
        this.children = children;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getTeamId() {
        return teamId;
    }

    public void setTeamId(Integer teamId) {
        this.teamId = teamId;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public String getLeader() {
        return leader;
    }

    public void setLeader(String leader) {
        this.leader = leader;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public List<OrganizationVo> getChildren() {
        return children;
    }

    public void setChildren(List<OrganizationVo> children) {
        this.children = children;
    }

    public Integer getPid() {
        return pid;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }
}

  這個是轉換的工具類(只要組織,不帶成員)

public class TreeNoteUtil {

    /**
     * 獲取父節點菜單
     * @param treesList 所有樹菜單集合
     * @return
     */
    public final static List<OrganizationVo> getFatherNode(List<TeamOrganizational> treesList){
        List<OrganizationVo> newTrees = new ArrayList<OrganizationVo>();

        
for (TeamOrganizational teamOrganizational : treesList) { if (teamOrganizational.getFatherId()==null) {//如果pId為空,則該節點為父節點 //遞歸獲取父節點下的子節點 OrganizationVo newTree = new OrganizationVo(); newTree.setId(teamOrganizational.getId()); newTree.setText(teamOrganizational.getName()); newTree.setTeamId(teamOrganizational.getTeamId()); List<TeamOrganizational> treesLists = teamOrganizational.getSonTeamOrganizationals(); newTree.setChildren(getChildrenNode(teamOrganizational.getId(), treesLists)); newTrees.add(newTree); } } return newTrees; } /** * 遞歸獲取子節點下的子節點 * @param pId 父節點的ID * @param treesLists 所有菜單樹集合 * @return */ private final static List<OrganizationVo> getChildrenNode(Integer pId, List<TeamOrganizational> treesLists){ List<OrganizationVo> newTrees = new ArrayList<OrganizationVo>(); for (TeamOrganizational mt : treesLists) { if (mt.getFatherId() != null) { if (mt.getFatherId() == pId) { OrganizationVo newTree = new OrganizationVo(); //遞歸獲取子節點下的子節點,即設置樹控件中的children newTree.setChildren(getChildrenNode(mt.getId(), treesLists)); //設置樹控件attributes屬性的數據 newTree.setId(mt.getId()); newTree.setText(mt.getName()); newTree.setPid(mt.getFatherId()); newTree.setTeamId(mt.getTeamId()); newTree.setSort(mt.getSort()); newTree.setLeader(mt.getLeader()); newTrees.add(newTree); } } } return newTrees; } }

這個是轉換的工具類(帶組織,帶成員)

public class TreeNoteUtil2 {

    /**
     * 獲取父節點菜單
     * @param treesList 所有樹菜單集合
     * @return
     */
    public final static List<OrganizationVo> getFatherNode(List<TeamOrganizational> treesList){
        List<OrganizationVo> newTrees = new ArrayList<OrganizationVo>();

        for (TeamOrganizational teamOrganizational : treesList) {

            if (teamOrganizational.getFatherId()==null) {//如果pId為空,則該節點為父節點
                //遞歸獲取父節點下的子節點
                OrganizationVo newTree = new OrganizationVo();
                newTree.setId(teamOrganizational.getId());
                newTree.setText(teamOrganizational.getName());
                newTree.setTeamId(teamOrganizational.getTeamId());

                List<TeamOrganizational> treesLists = teamOrganizational.getSonTeamOrganizationals();
                List<OaUser> treesUserLists = teamOrganizational.getOaUsers();
                newTree.setChildren(getChildrenNode(teamOrganizational.getId(), treesLists,treesUserLists));
                newTrees.add(newTree);
            }
        }

        return newTrees;
    }

    /**
     * 遞歸獲取子節點下的子節點
     * @param pId 父節點的ID
     * @param treesLists 所有菜單樹集合
     * @return
     */
    private final static List<OrganizationVo> getChildrenNode(Integer pId, List<TeamOrganizational> treesLists,List<OaUser> treesUserLists) {
        List<OrganizationVo> newTrees = new ArrayList<OrganizationVo>();



        for (TeamOrganizational mt : treesLists) {
            if (mt.getFatherId() != null) {

                if (mt.getFatherId() == pId) {
                    OrganizationVo newTree = new OrganizationVo();
                    //遞歸獲取子節點下的子節點,即設置樹控件中的children
                    newTree.setChildren(getChildrenNode(mt.getId(), treesLists,treesUserLists));
                    //設置樹控件attributes屬性的數據
                    newTree.setId(mt.getId());
                    newTree.setText(mt.getName());
                    newTree.setPid(mt.getFatherId());
                    newTree.setTeamId(mt.getTeamId());
                    newTree.setSort(mt.getSort());
                    newTree.setLeader(mt.getLeader());
                    if(mt.getOaUsers().size()>0){
                        newTree.setChildren(getOaUsers(mt.getOaUsers()));
                    }
                    newTrees.add(newTree);
                }
            }
            }
        if(treesUserLists.size()>0){
            for (OaUser oaUser:treesUserLists){
                OrganizationVo newTree = new OrganizationVo();
                newTree.setId(oaUser.getId());
                newTree.setText(oaUser.getRealname());
                newTrees.add(newTree);
            }
        }

        return newTrees;
    }

    public static List<OrganizationVo> getOaUsers(List<OaUser> oaUsers){
        List<OrganizationVo> newTrees = new ArrayList<OrganizationVo>();
        if (oaUsers.size()>0){
            for (OaUser oaUser:oaUsers){
                OrganizationVo newTree = new OrganizationVo();
                newTree.setId(oaUser.getId());
                newTree.setText(oaUser.getRealname());
                newTrees.add(newTree);
            }
        }else{

        }
        return newTrees;
    }


}

代碼就這麽多,主要是運用了遞歸的思想,因為在你不知道有多少層級的情況下,還是用遞歸比較方便,雖然循環比遞歸的速度快,占用內存小,但遞歸還是有其優勢的;

同時,仔細看那兩個工具類,第二個工具類比第一個工具類多了添加員工的操作,但是這裏就不會使用遞歸了,因為員工下面不會再有員工了,為什麽我會強調這個,因為我先寫的是第一個工具類,總想著如何遞歸,寫第二個的時候,人員總是重復添加,好久都找不到原因,後來才忽然想到,員工下面是沒有員工的,於是恍然大悟。。。。

Jquery EasyUI Tree樹形結構的Java實現(實體轉換VO)