2

I have a drop-down list that currently displays objects in arrayed fashion. The JSON data comes from a service which is injected into the component. I filter the data according to iso_id and upon selecting an option from the drop-down, I display the filtered data. I'm trying to display this data only when a button (Array) is clicked after selecting the respective option. Right now, selecting an option from drop down displays the data and clicking on the Array button makes it disappear. How do I pass this data into the button click? Here's some sample code-

HTML

<md-select [(ngModel)]="selectedISO" (ngModelChange)="onSelect(selectedISO)" placeholder="TSO" [(ngModel)]="containerDisplay" >
      <md-option  value="Charge Only"  > Charge Only </md-option>
      <md-option  value="PJM"  >PJM  </md-option>
      <md-option  value="Not in ISO Market"  > Not in ISO Market </mdoption>
      <md-option  value="UCSD"> UCSD </md-option>
    </md-select>

<button md-button (click)="onClickArray(selectedISO)"  [(ngModel)]="containerDisplay" ngDefaultControl> 
             Array  
          </button>

<div class="ui-ivs-container" *ngIf="containerDisplay"  >
            <div class="ui-ivs-resources">
                <div  *ngFor="let resource of isoToShow; let i = index;"   
                [ngClass]="{...}">     
                       //Array of objects gets displayed//
                </div>
            </div>

My TS file looks like this-

    @Component({
      selector: 'app-home',
      templateUrl: './home.component.html',
      styleUrls: ['./home.component.scss']
    })
    export class HomeComponent implements OnInit {
       containerDisplay:boolean;
       selectedISO;
constructor(private service: Service) {
       this.isoToShow=this.isoArray; // gets populated by subscribing to service
                     }

    onSelect(val){
      console.log(val);
      this.onClickArray(val);
    }
    onClickArray(val){
    this.isoToShow=this.isoArray.filter(resource => resource.iso_id===val)
    }
    }

2 Answers 2

1

There are few issues in your code.

  1. In the onSelect() function, you have to set the selectISO value and set the containerDisplay tofalse, since each time the select item changes the data div should be hidden and only shown when the 'Array' button is clicked.

  2. In the onClickArray(), you have to set the containerDisplay totrue.

  3. I am not sure why you need [(ngModel)]="containerDisplay" inside <button>. You can toggle containerDisplay from the component.

I have created this plunker example for demo

Code snippets from the example:

ts:

containerDisplay:boolean;
  selectedISO;

  constructor(private appState: AppState){ }

  ngOnInit(){
    this.getData();
  }

  getData(): void {
    this.appState
        .fetchFilterFields()
        .then(data => {
          // console.log(data)
          this.appState.setData(data);
          this.isoArray = data
        });
  }

  onSelect(val){
    console.log(val);
    this.selectedISO = val;
    this.containerDisplay = false;
  }

  onClickArray(val){
    this.containerDisplay = true;
    if(this.isoArray){
      this.isoToShow = this.isoArray.filter(resource => resource.iso_id === val);
    }

  }

html:

<md-select [(ngModel)]="selectedISO" (ngModelChange)="onSelect($event) placeholder="TSO">
      <md-option  value="Charge Only"  > Charge Only </md-option>
      <md-option  value="PJM"  >PJM  </md-option>
      <md-option  value="Not in ISO Market"> Not in ISO Market </md-option>
      <md-option  value="UCSD"> UCSD </md-option>
</md-select>

<p></p>
<button md-raised-button 
        (click)="onClickArray(selectedISO)"> 
        Array  
</button>

<p></p>

<div class="ui-ivs-container" *ngIf="containerDisplay"  >
    <div class="ui-ivs-resources">
      <div  *ngFor="let resource of isoToShow; let i = index;">     
           {{ resource | json}}
      </div>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

5 Comments

Perfect!! Exactly what I wanted! Thank you so much :)
You can upvote both answers too now, would really appreciate :)
Hi, What if I want to pass that same event to 3 different buttons? Array, Map or List and render the display according to the type of view selected?
@Nitz1308, Please create a separate question. Asking a question in a comment doesn't help other users.
I've created a separate question. Thank you
1

Change your onSelect method to fix this problem.

  onSelect(val) {
      console.log(val);
      this.selectedISO = val; 
  }

1 Comment

Hi, What if I want to pass that same event to 3 different buttons? Array, Map or List and render the display according to the type of view selected?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.