1

我正在尝试使用 rxjs 构建 n 级树视图。我用普通的javascript实现了同样的效果。在这里找到js代码

var x = [{"id":1,"name":"Admin","parentDepartmentId":null},{"id":2,"name":"Development","parentDepartmentId":1},{"id":3,"name":"Research and Development","parentDepartmentId":1},{"id":4,"name":"FE","parentDepartmentId":2},{"id":5,"name":"BE","parentDepartmentId":2},{"id":6,"name":"Testing","parentDepartmentId":1},{"id":7,"name":"Unit Test","parentDepartmentId":6},{"id":8,"name":"Integration Test","parentDepartmentId":6},{"id":9,"name":"HR","parentDepartmentId":null}];

function getRecords(parents) {
    parents.forEach((parent) => {
      var result = getChildData(parent);
      parent.items = result;
      if (result.length > 0) {
        getRecords(result);
      }
    })
  return parents;
}
  
function getChildData(parent) {
    return x.filter(record => record.parentDepartmentId == parent.id)
}

function getParents(x){
    return  x.filter(record => record.parentDepartmentId == null);
}

var parents = getParents(x)
var result = getRecords(parents);
console.log(result);

有人可以对此进行一些说明。

4

1 回答 1

0

这是我可以从https://jvilk.com/MakeTypes/生成的内容。不过,您可能需要对其进行一些清理,例如使 Data 和 Data1 使用相同的类,从而使 isNode 成为可选。当你必须做很多这样的事情时,转换器可以派上用场。

    Also you may want to make these as class instead of interface if what you want is class.

   export interface Weather {
  id: number;
  name: string;
  parentDepartmentId?: null;
  items?: (ItemsEntity | null)[] | null;
}
export interface ItemsEntity {
  id: number;
  name: string;
  parentDepartmentId: number;
  items?: (ItemsEntity1 | null)[] | null;
}
export interface ItemsEntity1 {
  id: number;
  name: string;
  parentDepartmentId: number;
  items?: (null)[] | null;
}
于 2018-05-22T11:03:16.020 回答