javascript - 如何编写查找父子的递归方法

标签 javascript java angularjs json spring

This is to save the details with recursive.

在这里,我想从数据库中获取详细信息并使用递归方法设置到我的 bean 中。所以我可以在 angularUi 树格式中显示。如何编写递归方法以设置到我的 bean 中。

我的数据库结构:- enter image description here

我用 rowId 将 parent 和 child 分开。您可以访问我的示例 screen

例如:- 父级的 Rowid 为 1 这个 1 的 child 是 1.1 1.1 的 child 是 1.1.1 这样它会延长,,。

我将所有的 parent 和 child 保存在一张图片上方的表格中。

每个对象(行)都有 items[]。如果父项有任何子项,则子项将被添加到该 items[] 数组中,如果该子项有任何子项,则该子项将被添加到该行的 items[ ]...像这样它会延长。

例如:- JSON 对象是:-

{
    "id": 1,
    "rowId": "1",
    "items": [
      {
        "id": 10,
        "rowId": "1.1",
        "items": [
          {
            "id": 100,
            "rowId": "1.1.1",
            "items": [
              {
                "id": 1000,
                "rowId": "1.1.1.1",
                "items": []
              }
            ]
          }
        ]
      },
      {
        "id": 11,
        "rowId": "1.2",
        "items": []
      }
    ]
  }

我已经使用 this answer. 保存了这些数据

但是在检索时我遇到了问题。问题是检索时不会有任何父项和子项,因为数据将保存在同一个表中。关系只是 rowid。为此,我需要编写一个递归方法,例如保存,并且需要将 child 添加到父 items[] 数组。

public class AdminComponentBean{

    List<MultiAdminComponent> componentListbean;
}

MultiAdminComponent.java:-

public class MultiAdminComponent {

    private String componentName;
    private String componentIdentification;
    private String componentType;
    private String componentState;
    private String componentUrl;
    private String rowId;
    private List<MultiAdminComponent> items;
}

在这里我尝试检索所有细节并尝试将 child 添加到父级。但它应该是一个递归方法

List<MultiAdminComponent> componentList=BaseDAO.getAdminComponentDAOObject().getComponentDetails();
   if(null != componentList) {
       for(MultiAdminComponent itemsList : componentList){
           if(itemsList.getRowId().length().equals() "1"){//here parent row will come
               //by considering rowid I need to find the child of the rowId
               //child of 1 is 1.1
               //if 1.1 is child of 1 then I need to add that 1.1 object to `items[]` array of 1
               //like this it should work recursve
            }
        }
    }

最佳答案

我建议采取额外步骤并将所有元素存储在 HashMap 中,而不是递归

// a map containing all elements searchable by the rowId
HashMap<String, MultiAdminComponent> idToItem = new HashMap<>();
// a set containing all elements that don't have a parent (id: 1, 2, 3, etc.)
Set<MultiAdminComponent> rootItems = new HashSet<>();

for (MultiAdminComponent item : componentList) {
    // build the id->item map
    idToItem.put(item.getRowId(), item);
}

for (MultiAdminComponent item : componentList) {
    String parentId = getParentId(item.getRowId());
    if (parentId == null) {
        // this item has no parent -> it is a root item
        rootItems.add(item);
    } else {
        // This item has a parent -> look the parent up
        MultiAdminComponent parent = idToItem.get(parentId);
        parent.getItems().add(item);
    }
}

// rootItems now contains all MultiAdminComponents which do not have a parent, with the correct hierarchy for all items

getParentId 可能是这样的:

private String getParentId(String id) {
    int lastDot = id.lastIndexOf(".");
    if (lastDot == -1) {
        return null;
    }
    return id.substring(0, lastDot);
}

如果你能保证 componentList 从父到子遍历列表,你可以结合两个 for 循环。

关于javascript - 如何编写查找父子的递归方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45633934/

相关文章:

javascript - Bulma Select 表单元素 - 选择选项时如何检测 "select"或更改事件

javascript - React JS onClick 设置点击索引的状态

java - java 类中带有静态方法的私有(private)构造函数

java - 使用色差比较图像

javascript - 多个 $http.get 请求,每个 JSON 文件由 HTML 中的 h1 分割

javascript - 拖放整个表格行

javascript - 使用javascript将二进制数据转换为base64

javascript - 桌面捕获 chrome 插件

java - 带有 javax.ws.rs.core.Application 的 MultiPartFeature

javascript - AngularJS ng-click 创建子范围,但在后续 ng-click 后推送到主范围