首页 > 解决方案 > Angular 材质树格式问题

问题描述

在 Angular Tree 中,我正在从数据库中检索数据,但树格式与预期不符。下面是浏览器树视图。

浏览器树视图

根据上图,在国家节点中,我得到了子项中的每个元素,但我只想要不同的项,即单个印度项和单个澳大利亚项。

下面是我的组件代码:

GetTreeValue(): void {
debugger
this._StudentService.getRegionList(1,2).subscribe(data=>{
  this.RegionList=data;
  let tree_data:any=[];
  let country:any=[];
  let state:any=[];
  let district:any=[];
  let region:any=[];

  data.forEach(elem=>{
    let country_node:any={
      name:elem.Country
    }
    country.push(country_node);

    let state_node:any={
      name:elem.State
    }
    state.push(state_node);

    let district_node:any={
      name:elem.District
    }
    district.push(district_node);

    let region_node:any={
      name:elem.Region
    }
    region.push(region_node);
  });

  let root_obj:any={
    name:'data',
    children:[
      {  
        name: 'Country',
        children: country
      }, 
      
      { 
        name: 'State' ,
        children:state 
      },
      { 
        name: 'District' , 
        children: district
      },
      { name: 'Region' ,
        children: region
      }]
    
  };
  TREE_DATA.push(root_obj);
  this.dataSource.data =TREE_DATA;
});

}

标签: angularangular-materialtreeview

解决方案


推荐阅读