Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display the dropdown values based on other dropdown value using typescript in Angular 2 application

I am developing angular 2 application, in my current project I want the functionality for displaying dropdown values based on year, make, model. For example if I select Honda then display only Honda models in model dropdown.

By default I will bind the complete data for year, make and model.

This is my view.

enter image description here

VehicleComponent.html

<div class="row form-group">
    <div class="col-md-3">
        <label  class="control-label">Year*</label><br />
        <select  friendly-name="Year" id="year" name="year" class="col-md-6 form-control"  ng-required="true" onchange='OnChange()' >
            <option>Select</option>
            
            <option *ngFor='let type of lookupdetailsvehicleyearinfo'>{{type.LookupValue}}</option>
           
        </select>
    </div>
    <div class="col-md-3">
        <label  class="control-label">Make*</label><br />
        <select friendly-name="Make" class="col-md-6 form-control" ng-required="true" >
            <option>Select</option>
            <option *ngFor='let type of lookupdetailsvehiclemakeinfo'>{{type.LookupValue}}</option>
            </select>
    </div>
    <div class="col-md-3">
        <label  class="control-label">Model*</label>
        <select  friendly-name="Model" class="col-md-6 form-control" ng-required="true" >
            <option>Select</option>
            <option *ngFor='let type of lookupdetailsvehiclemodelinfo'>{{type.LookupValue}}</option>
           </select>
    </div>                                      
</div>

I will get the above data from My Own API's.

Can you please tell me how to write the typescript code in angular 2 application for the above functionality.

like image 552
Pradeep Avatar asked Dec 05 '16 12:12

Pradeep


1 Answers

The only thing you have to do is, track the change event of your select-input and change the source of another select-input. Angular2 will do the rest.

Using the selected value:

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>

      <select (change)="firstDropDownChanged($event.target.value)" >
        <option>Select</option>
        <option *ngFor='let v of _values1'>{{ v }}</option>
      </select>

      <select >
        <option>Select</option>
        <option *ngFor='let v of _values2'>{{ v }}</option>
      </select>
    </div>
  `,
})
export class App {
  name:string;

  private _values1 = ["1", "2", "3"];
  private _values2 = [];

  constructor() {
    this.name = 'Angular2'
  }

  firstDropDownChanged(val: any) {
    console.log(val);

    if (val == "1") {
      this._values2 = ["1.1", "1.2", "1.3"];
    }
    else if (val == "2") {
      this._values2 = ["2.1", "2.2", "2.3"];
    }
    else if (val == "3") {
      this._values2 = ["3.1", "3.2", "3.3"];
    }
    else {
      this._values2 = [];
    }
  }
}

live-demo: https://plnkr.co/edit/GDXsPt4aiS7vus6oPvuU?p=preview

UPDATE

Or you can use the selectedIndex: (note that -1 cause your first "Select"-item.

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>

      <select (change)="firstDropDownChanged($event.target.selectedIndex - 1)" >
        <option>Select</option>
        <option *ngFor="let v of _values1">{{ v.val }}</option>
      </select>

      <select >
        <option>Select</option>
        <option *ngFor='let v of _values2'>{{ v }}</option>
      </select>
    </div>
  `,
})
export class App {
  name:string;

  private _values1 = [
    { id: 1, val: "huhu" },
    { id: 2, val: "val2" },
    { id: 3, val: "yep" },
    { id: 4, val: "cool" }
  ];
  private _values2 = [];

  constructor() {
    this.name = 'Angular2'
  }

  firstDropDownChanged(val: any) {
    const obj = this._values1[val];
    console.log(val, obj);

    if (!obj) return;

    if (obj.id == 1) {
      this._values2 = ["1.1", "1.2", "1.3"];
    }
    else if (obj.id == 2) {
      this._values2 = ["2.1", "2.2", "2.3"];
    }
    else if (obj.id == 3) {
      this._values2 = ["3.1", "3.2", "3.3"];
    }
    else {
      this._values2 = [];
    }
  }
}

live-demo: https://plnkr.co/edit/wugNC6S27FB48EtRN906?p=preview

like image 161
slaesh Avatar answered Nov 12 '22 23:11

slaesh