Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding routing for page navigation in angular tree

I need help in adding route to angular tree.

HTML Code:

<mat-tree [dataSource]="dataSource" class="tree-container" [treeControl]="treeControl">
     <mat-tree-node class="btnLinks" *matTreeNodeDef="let node" matTreeNodePadding>
      <button mat-icon-button disabled></button>
      {{node.name}}
    </mat-tree-node>
    <mat-tree-node class="btnLinks" *matTreeNodeDef="let node;when: hasChild" matTreeNodePadding>
      <button mat-icon-button matTreeNodeToggle
              [attr.aria-label]="'toggle ' + node.name">
        <mat-icon class="mat-icon-rtl-mirror">
          {{treeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}}
        </mat-icon>
      </button>
      {{node.name}}
    </mat-tree-node>
  </mat-tree>

.Ts file

interface ProfileList {
  name: string;
  children?: ProfileList[];
}

const PROFILE_DATA: ProfileList[] = [
  {
    name: 'General',
  }, 
  {
    name: 'Degrees',
  },
];

/** Flat node with expandable and level information */
interface TreeFlatNode {
  expandable: boolean;
  name: string;
  level: number;
}

@Component({
  selector: 'mpn-profile-landing',
  templateUrl: './profile-landing.component.html',
  styleUrls: ['./profile-landing.component.css']
})

export class ProfileLandingComponent {

  public selectedItem : String = '';

  constructor(private breakpointObserver: BreakpointObserver) {
    this.dataSource.data = PROFILE_DATA;
  }

private transformer = (node: ProfileList, level: number) => {
    return {
      expandable: !!node.children && node.children.length > 0,
      name: node.name,
      level: level,
    };
  }

I need to add routing like when I click on Degrees, I need to navigate to another page, I'm not sure where to place the router link. Is it possible to do this? Thank you.

like image 550
PremKumar Avatar asked May 16 '19 06:05

PremKumar


People also ask

Does Angular add routing?

At the basic level, routing allows angular to display different "pages" or components. You probably want to have it, if you want to navigate across pages in your application. It shouldn't hurt anything if you add it, but don't use it.

What is the correct way to add a basic route in Angular?

First, add links to the two components. Assign the anchor tag that you want to add the route to the routerLink attribute. Set the value of the attribute to the component to show when a user clicks on each link. Next, update your component template to include <router-outlet> .

What is the difference between routing and navigation in Angular?

Angular provides extensive set of navigation feature to accommodate simple scenario to complex scenario. The process of defining navigation element and the corresponding view is called Routing. Angular provides a separate module, RouterModule to set up the navigation in the Angular application.

How do I redirect a page to another page in Angular 8?

You can simply redirect to page in angular 6, angular 7, angular 8, angular 9, angular 10, angular 11, angular 12, angular 13 and angular 14 application with this solution. It's pretty simple to redirect to another route url using redirectTo in angular application.


1 Answers

You can wrap the {{node.name}} with <span> like:

<span click="navigateToPage(node.name)">{{node.name}}</span>

then in your TS code add private router: Router to your constructor and add a method something like this:

constructor(
  private breakpointObserver: BreakpointObserver,
  private router: Router
) {
    this.dataSource.data = PROFILE_DATA;
}

navigateToPage(name: string) {
  if (name === 'Degrees') {
    this.router.navigate([<THE_ROUTE_YOU_WISH_TO_NAVIGATE_TO>]);
  }
}
like image 83
OST Avatar answered Sep 25 '22 01:09

OST