java-json
import com.alibaba.fastjson.JSONObject; import java.util.ArrayList; import java.util.List; /** * @Description: 菜单工具类 * @version: V1.0 */ public class MenuTreeUtil { /** * @return * @Author * @Param nodes :所有的节点列表 */ public List data(List<JSONObject> nodes) { ArrayList<JSONObject> rootNode = new ArrayList<>(); for (JSONObject node : nodes) { if (node.getString("parentId").equals("-1")) { node.remove("checked"); rootNode.add(node); } } for (JSONObject node : rootNode) { List<JSONObject> child = getChild(String.valueOf(node.getString("id")), nodes); if (child != null && !child.isEmpty() && child.size() > 0) { node.remove("checked"); } node.put("children", child); } return rootNode; } /** * @return * @Author * @Description //TODO 获取根节点的子节点 * @Param */ public List<JSONObject> getChild(String id, List<JSONObject> allNode) { //存放子菜单的集合 ArrayList<JSONObject> listChild = new ArrayList<>(); for (JSONObject node : allNode) { if (node.getString("parentId").equals(id)) { listChild.add(node); } } //递归: for (JSONObject node : listChild) { List<JSONObject> child = getChild(String.valueOf(node.getString("id")), allNode); if (child != null && !child.isEmpty() && child.size() > 0) { node.remove("checked"); } node.put("children", child); } if (listChild.size() == 0) { return null; } return listChild; } }
使用
MenuTreeUtil menuTreeUtil = new MenuTreeUtil(); List data = menuTreeUtil.data(JSON.parseArray(JSON.toJSONString(menus1), JSONObject.class));
java-object
import lombok.Data; import java.io.Serializable; import java.util.ArrayList; import java.util.List; @Data public class PermissionVo implements Serializable { private static final long serialVersionUID = 106717167007024517L; private String id; private String title; private String parentId; /** * 权限菜单是否选中 */ private boolean checked; /** * 是否展开,默认展开 */ private boolean spread=true; private List<PermissionVo> children = new ArrayList<>(); }
import java.util.ArrayList; import java.util.List; /** * @ClassName: PermissionVo * @Description: 菜单工具类 * @version: V1.0 */ public class MenuTreeUtil { /** * @return * @Author * @Description //TODO * @Param nodes :所有的节点列表 */ public List data(List<PermissionVo> nodes) { ArrayList<PermissionVo> rootNode = new ArrayList<>(); for (PermissionVo node : nodes) { if (node.getParentId().equals("-1")) { rootNode.add(node); } } for (PermissionVo node : rootNode) { List<PermissionVo> child = getChild(String.valueOf(node.getId()), nodes); node.setChildren(child); } return rootNode; } /** * @return * @Author * @Description //TODO 获取根节点的子节点 * @Param */ public List<PermissionVo> getChild(String id, List<PermissionVo> allNode) { //存放子菜单的集合 ArrayList<PermissionVo> listChild = new ArrayList<>(); for (PermissionVo node : allNode) { if (node.getParentId().equals(id)) { listChild.add(node); } } //递归: for (PermissionVo node : listChild) { node.setChildren(getChild(String.valueOf(node.getId()), allNode)); } if (listChild.size() == 0) { return null; } return listChild; } }
js
function recursion(nodes) { var temp = []; temp.push(nodes); var rootNode = [] for(var i = 0; i < temp.length; i++) { rootNode.push(temp[i]); } for(var i = 0; i < rootNode.length; i++) { var childArrs = []; childArrs = getChild(rootNode[i].id, nodes.children); rootNode[i].children = childArrs; } return rootNode; } function getChild(id, allNodes) { var childArr = []; if(allNodes){ for(var i = 0; i < allNodes.length; i++) { if(id == allNodes[i].parentId) { childArr.push(allNodes[i]); } } }
//递归 for(var i = 0; i < childArr.length; i++) { var child = []; child = getChild(childArr[i].id, allNodes); childArr[i].children = child; } if(!childArr) { return null; } return childArr; }