生活随笔
收集整理的這篇文章主要介紹了
zTree菜单的排序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
zTree菜單的排序,查詢出來為所有的菜單項
循環遍歷取的時候,為了保證它的順序,所以先做個排序
只要保證相同父節點下的,所有的子節點,都是有序的,取的時候,從根節點開始得到的菜單就是有序的
private static List<SystemResource>
convertToOrder(List<SystemResource> resources){List<String> setParentId=
new ArrayList<>();
for(
int i=
0;i<resources.size();i++){String ParentId=resources.
get(i).getParentId().toString();
if(!setParentId.contains(ParentId)){setParentId.add(ParentId);}}Map<String, List<SystemResource>> arraySource=
new HashMap<String, List<SystemResource>>();
for(
int i=
0;i<setParentId.size();i++){List <SystemResource> systemResourceList=
new ArrayList<>();arraySource.put(setParentId.
get(i).toString(),systemResourceList);}
for(
int i=
0;i<resources.size();i++){
if(arraySource.containsKey(resources.
get(i).getParentId().toString())){arraySource.
get(resources.
get(i).getParentId().toString()).add(resources.
get(i));}}List<SystemResource> resourceList=
new ArrayList<>();
for(
int i=
0;i<setParentId.size();i++){List<SystemResource> systemResourceList=
new ArrayList<>();
if (arraySource.
get(setParentId.
get(i)).size()>
0){systemResourceList=orderSystemResourceList(arraySource.
get(setParentId.
get(i)));}resourceList.addAll(systemResourceList);}
return resourceList;
}
private static List<SystemResource>
orderSystemResourceList(List<SystemResource> systemResourceList){Map<String,SystemResource> resourceMap=
new HashedMap();
for(
int i=
0;i<systemResourceList.size();i++){SystemResource systemResource=
new SystemResource();resourceMap.put(systemResourceList.
get(i).getWeight().toString(),systemResourceList.
get(i));}
int arrayInt[]=
new int[systemResourceList.size()];
for(
int i=
0;i<systemResourceList.size();i++){arrayInt[i]=systemResourceList.
get(i).getWeight();}
for (
int i =
0; i < arrayInt.length; i++) {
for (
int j = i+
1; j < arrayInt.length; j++) {
if (arrayInt[i] < arrayInt[j]) {
int temp = arrayInt[i];arrayInt[i] = arrayInt[j];arrayInt[j] = temp;}}}List<SystemResource> systemResourceListByWeight=
new ArrayList<>();
for (
int i=
0;i<arrayInt.length;i++){systemResourceListByWeight.add(resourceMap.
get(String.valueOf(arrayInt[i])));}
return systemResourceListByWeight;
}
上述代碼,是用不到的
直接在查詢的時候,排序即可
select * from SYS_RESOURCE ORDER BY PARENT_ID,WEIGHT
總結
以上是生活随笔為你收集整理的zTree菜单的排序的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。