chethu
chethu

Reputation: 374

change second dropdown options based on first dropdown selection using angular 4

component.html

<select data-placeholder="All Categories" class="category" value="category.category.categoryName">
  <option selected="selected" style="display:none">Select Category</option>
  <option class="select" *ngFor="let category of categories" value="category">
    {category.categoryName}}
  </option>
</select>
<select data-placeholder="All Cities" class="services location" value="category.categoryName">
  <option selected="selected" style="display:none">select type of services</option>
  <option *ngFor="let category of selected.categoryServicemodel; let i =
      index" value="category">{{category.serviceName}}
  </option>
</select>

component.ts

export class Component {
  categories: any[];
  services: any[];
  cities: any[];
  selected: any = {};

  constructor() {
    getAllCategories();
    {
      this.postService.getAllCategories()
        .subscribe(data => {
          this.categories = data.json();
          console.log(this.categories);
        });
    }

    getAllService();
    {
      this.postService.getAllServices()
        .subscribe(res => {
          this.services = res.json();
        });
    }
  }
}

response

[
  {
    "categoryId": 1,
    "categoryName": "Painting",
    "categoryDesc": "Painting of all types",
    "categoryServicemodel": [
      {
        "serviceId": 1,
        "serviceName": "Test12",
        "serviceDesc": "test12",
        "isActive": 1
      },
      {
        "serviceId": 3,
        "serviceName": "TESTINGEXAMPLE ",
        "serviceDesc": "TESTINGEXAMPLE Details 
        Information
        ",
        "isActive": 1
      },
      {
        "serviceId": 12,
        "serviceName": "New Painters",
        "serviceDesc": "office paintings ",
        "isActive": 2
      },
      {
        "serviceId": 11,
        "serviceName": "ABC Painters",
        "serviceDesc": "painting of all types",
        "isActive": 1
      }
    ],
    "active": 1
  },
  {
    "categoryId": 2,
    "categoryName": "string",
    "categoryDesc": "string",
    "categoryServicemodel": [
      {
        "serviceId": 2,
        "serviceName": "Test15",
        "serviceDesc": "test15",
        "isActive": 1
      }
    ],
    "active": 0
  },
  {
    "categoryId": 4,
    "categoryName": "carpenter",
    "categoryDesc": "Carpenter",
    "categoryServicemodel": [
      {
        "serviceId": 5,
        "serviceName": "Test Carpenter ",
        "serviceDesc": "Test carpenter Description",
        "isActive": 1
      }
    ],
    "active": 0
  }
]

My question is when i select first category name for ex. painting(in my response) all service name under that category should be changed in second dropdown And again If i select Second category then all service name under that category should be available inside second dropdown

Upvotes: 2

Views: 1260

Answers (1)

I did some a little changes in your code.

  • In your html I replaced "value" for "[value]". In this your code the option value is always the "category" string. If your use brackets, you will refer to the contents of a variable
  • I created a buildService method for populate your services array
  • Finally I call buildService when I selected the first component

Sample code: https://stackblitz.com/edit/service-category

component.html

<select data-placeholder="All Categories" class="category" value="category.categoryName" [(ngModel)]="selected" (change)="buildServices($event)">
  <option selected="selected" style="display:none">Select Category</option>
  <option class="select" *ngFor="let category of categories" [value]="category.categoryId">
    {{category.categoryName}}
  </option>
</select>
<select data-placeholder="All Cities" class="services location" value="category.categoryName">
  <option selected="selected" style="display:none">select type of services</option>
  <option *ngFor="let service of services; let i =
      index" value="service.serviceId">{{service.serviceName}}
  </option>
</select>

component.ts

  public buildServices() {
    this.services = [];
    this.categories.forEach((category) => {
      if (category.categoryId == this.selected) {
        this.services = category.categoryServicemodel;
      }  
    }); 
  }

Upvotes: 1

Related Questions