Cascading Drop-Down In Table Data

I am a newbie for Angular development and when I have started learning Angular and developing applications, the requirement was to implement cascading dropdown and at a beginning level, I was facing difficulties to implement it.

Now after my research I have successfully implemented it and am sharing with you all so if anyone of you need to,  then you can go through it and implement as per your requirements. 

Step 1

Create a new project and install required node modules.

Step 2

Create HTML template for the component in which you are going implement this

I have created a new project for this demo so I doing changes in app component. You can do it in your respective component

  1. <div style="text-align:center">  
  2.   <h1>  
  3.     Cascading Dropdown with table fromat data  
  4.   </h1>  
  5. <table width="500px">  
  6.     <tr>  
  7.       <th>  
  8.         Category  
  9.       </th>  
  10.       <th>  
  11.         Sub Category  
  12.       </th>  
  13.     </tr>  
  14.     <th>  
  15.     </th>  
  16.     <tr>  
  17.       <td>  
  18.         <label>Caregory:</label>  
  19.         <select id="ddlCategory" style="width:100px;">  
  20.         <option value="0">--Select--</option>  
  21.       </select>  
  22.       </td>  
  23.       <td>  
  24.         <div>  
  25.           <label>Subcategory:</label>  
  26.           <select id="ddlSubCategory" style="width:100px;">  
  27.           <option value="0">--Select--</option>            
  28.         </select>  
  29.         </div>  
  30.       </td>  
  31.     </tr>  
  32. </table>  
  33. </div>  

Step 3

Create new service and functions into the service those will be used for getting data to fill our drop-downs 
  1. import { Injectable } from '@angular/core';  
  2. import { Category } from '../Models/category';  
  3. import { SubCategory } from '../Models/SubCategory';  
  4.   
  5. @Injectable()  
  6. export class Service {  
  7.   getCategories() {  
  8.     return [  
  9.      new Category(1, 'Expense'),  
  10.      new Category(2, 'Income'),  
  11.      new Category(3, 'Miscellaneous')   
  12.     ];  
  13.   }  
  14.   getSubCategories() {  
  15.    return [  
  16.      new SubCategory(1, 1, 'Expense 1'),  
  17.      new SubCategory(2, 1, 'Expense 2'),  
  18.      new SubCategory(3, 1, 'Expense 3'),  
  19.      new SubCategory(4, 1, 'Expense 4'),  
  20.      new SubCategory(5, 2, 'Income 1'),  
  21.      new SubCategory(6, 2, 'Income 2'),  
  22.      new SubCategory(7, 2, 'Income 3'),  
  23.      new SubCategory(8, 3, 'Miscellaneous 1'),  
  24.      new SubCategory(9, 3, 'Miscellaneous 2'),  
  25.      new SubCategory(10, 3, 'Miscellaneous 3')  
  26.     ];  
  27.   }  
  28. }  
Step 4

Create a model of Category.
 
  1. export class Category {  
  2.   constructor(public Category_Id: number, public Category_Name: string) { }  
  3. }  
Step 5

Create a model of SubCategory. 
  1. export class SubCategory {  
  2.   constructor(public SubCategory_ID: number, public Category_ID: number, public SubCategory_Name: string) { }  
  3. }  
Step 6

Do the code in Component.ts file to get Category data.

  • Import models
  • Import Service

Call service to get data of Categories and assign it to Categories 

  1. import { Component } from '@angular/core';
  2. import { Service } from './Service/service';  
  3. import { Category } from './Models/Category';  
  4. import { SubCategory } from './Models/SubCategory';  
  5.   
  6. @Component({  
  7.   selector: 'app-root',  
  8.   templateUrl: './app.component.html',  
  9.   styleUrls: ['./app.component.css'],  
  10.   providers: [Service]  
  11. })  
  12. export class AppComponent {    
  13.   Categories: Category[];  
  14.   numbers: number[] = [1,2,3];  
  15.   constructor(private _dataService: Service) {      
  16.     this.Categories = this._dataService.getCategories();  
  17.   }  
  18. }  
Step 7

Fill categories Drop down in HTML template 

  1. <select id="ddlCategory" style="width:100px;">  
  2.           <option value="0">--Select--</option>  
  3.           <option *ngFor="let category of Categories" value={{category.Category_Id}}>{{category.Category_Name}}</option>  
  4.         </select>  
Step 8

Now we want to fill sub categories on the selection of Category so we will make the function that will be called on Category selection.

In app.component.ts we will create a function

  1. onSelect(Category_ID,index) {  
  2.     this.Subcategories[index] = this._dataService.getSubCategories().filter((item)=> item.Category_ID == Category_ID);  
  3.   }  
 Assign that function on change event of Category drop-down 
  1. <select id="ddlCategory" (change)="onSelect($event.target.value, number)" style="width:100px;">  
  2.           <option value="0">--Select--</option>  
  3.           <option *ngFor="let category of Categories" value={{category.Category_Id}}>{{category.Category_Name}}</option>  
  4.         </select>  
Step 9

Fill data of sub category in HTML template

  1. <select id="ddlSubCategory" style="width:100px;">  
  2.          <option *ngIf='selectedCategory[number] == 0' value="0">--Select--</option>  
  3.           <option *ngFor="let sub of Subcategories[number]" value={{sub.SubCategory_ID}}>{{sub.SubCategory_Name}}</option>         
  4.         </select>  
Step 10

Now run your project and you will get your desired output.

output

Hope this will help you.