我对每个系统都分配了一个唯一的系统号,每个系统的下的菜单和它的子菜单的命名也按一定的规律
如果系统分配了3位数, 如100, 101
它的菜单对应于: 100系统的菜单10001, 10002, 10003, 1000101, 100010102....
使用dhtmlxtree它的实节点很容易获取到,但是它的虚节点获取时有点问题(采用某种方式时)
因此通过它的实节点获取到它对应的虚节点
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.clarancepeng;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
*
* @author pqs
*/
public class TyMenu {
private Collection<QxVo> getMenuList() {
//String ids_ = "1060204,106020401,10602040101,1060205,106020501,10602050101,106020502,10602050201,1060206,106020601,10602060101,10602060102,10602060103,10602060104,1060207"; //"10602,1060201,106020101,10602010101,106020102,10602010201,10602010202,10602010203,10602010204,106020103,10602010301,106020104,10602010401,1060202,106020201,10602020101,10602020102,10602020103,10602020104,1060203,106020301,10602030101,10602030102,10602030103,10602030104,1060204,106020401,10602040101,1060205,106020501,10602050101,106020502,10602050201,1060206,106020601,10602060101,10602060102,10602060103,10602060104,1060207";
String ids_ = "100010203,100010204,1000102,1000204,1000205,1000203,10301,103,10302,103020103,1030101,1030102,1000402,10705040605,1001028,10402020507";
String[] str = ids_.split(",");
String[] str2 = getOhterMenu(str);
System.out.println("-----------------------------------------------------------");
Arrays.sort(str2);
for(String s : str2) {
System.out.println(" " + s);
}
List<QxVo> list = new ArrayList<QxVo>();
if(str != null)
{
for(String s : str){
if(!"".equals(s) && s!=null)
{
QxVo vo = new QxVo();
vo.setRlid("abc");
vo.setPrvid(s);
vo.setLbl("0");
list.add(vo);
}
}
}
return list;
}
/**
* 首先对获取的节点进行排序
* 1. 如果他们的祖先已经被选择了,则他们的儿子及孙子等都不用再做判断了(只判断为3位数的顶级节点)
* 2. 如果他们的父亲已经被选择了,则此节点不用再做判断了
* 3. 如果相邻的两个节点有共同的父亲,后面的一个节点就不用判断了
* @param strArr
* @return
*/
private String[] getOhterMenu(String[] strArr) {
if(strArr == null || strArr.length == 0) return strArr;
List<String> menuList = Arrays.asList(strArr);
Collections.sort(menuList);
//祖先节点
String ancestorNode = null;
Set<String> otherMenu = new HashSet<String>();
String parent = null;
//上级菜单
String upperMenu = "";
String befores = "";
for(String s : menuList) {
System.out.println(" " + s);
if(s != null && !"".equals(s)) {
if(s.length() == 3) {
ancestorNode = s;
}
else if(s.length() > 3) {
if(ancestorNode != null
&& s.startsWith(ancestorNode)) {
//祖先已经被选节点
continue;
}
else {
ancestorNode = null;
if(s.substring(0, s.length() -2).equals(parent)) {
//同父节点
continue;
}
else {
if(befores.length() < s.length()) {
upperMenu = befores;
}
else if(befores.length() > s.length()) {
upperMenu = s;
}
parent = s.substring(0, s.length() -2);
//System.out.println("parent= " + parent );
//父节点已经被选择,不必在做添加
if(parent.equals(upperMenu)) {
continue;
}
if(!menuList.contains(parent)) {
otherMenu.add(parent);
}
String temp = parent;
while(temp.length() > 3) {
temp = temp.substring(0, temp.length()-2);
if(!menuList.contains(temp)) {
otherMenu.add(temp);
}
}
}
}
}
}
befores = s;
}
return otherMenu.toArray(new String[0]);
}
public static void main(String[] args) {
TyMenu tyMenu = new TyMenu();
tyMenu.getMenuList();
}
}
class QxVo implements Serializable
{
/**
*
*/
private static final long serialVersionUID = -4622465980226296915L;
private String rlid;
private String prvid;
private String lbl;
public String getPrvid()
{
return prvid;
}
public void setPrvid(String prvid)
{
this.prvid = prvid;
}
public String getRlid()
{
return rlid;
}
public void setRlid(String rlid)
{
this.rlid = rlid;
}
/**
* @return Returns the lbl.
*/
public String getLbl()
{
return lbl;
}
/**
* @param lbl The lbl to set.
*/
public void setLbl(String lbl)
{
this.lbl = lbl;
}
}