• Create Dynamic Data Table In Angular 4

    Posted by Yash Arora | Last Updated: 28-Jun-18

    In this blog, I am going to explain how to generate dynamic drop-down option values and how to show dynamic table data on selecting any of the options from the drop-down list

    Let's Start:-

    1. Dynamic drop-down options values

    As an example, I am taking hard-coded values to explain but you can take values from API response to show data in the drop-down

    component.ts file:-
    initialize this variable in the class and either this can be the response from API So you can show dynamic option in the drop-down

    this.companydepartments =[

    component.html file


    <div class="dropdown">
    <select id="events" [(ngModel)]= "selectedoption" (ngModelChange)="getDetails($event)" class="form-control">
     <option *ngFor="let department of companydepartment" [value]="">{{}}</option>

    Here companydepartment is same field as initialized in component.ts file 
    It will create dynamic drop-down option and if we want to select any one as default. Add this lines in component.ts file
    this.selectedoption='It'     selectedoption is same as mentioned in ngModel in .html file

    2.Generate dynamic table data on the selection of particular drop-down option


    Add in component.ts file
    public DropDownCollector : any;

    if(DropDownCollector == 'HR'){this.showHrTable()}
    if(DropDownCollector == 'It'){this.showItTable()}


    here getDetails() is the same function as specified in div section in .html file


    Now in showHrTable() and showItTable() you can get the response from api and collect the response in some variable like ResponseApi

    To explain better i am taking hardcoded values in function

      "Profile":"Assocaite HR",



    Now to show the table data according to selected drop-down option

    component.html file
     <table style="width:100%" class="order-history-table">
                            <tr *ngFor="let response of  ResponseApi ">

    Now it will show dynamic data according to selected option from the drop-down.


Tags: Angular2