Show data based on the id of another table


#1

I have

initializeOccurrences() {
    this.service.getOccurrences()
      .subscribe(
        data => this.occurrences = console.log(data['cause_id']),
        errr => console.log(errr)
      );

I want to get this.occurrence by field "cause_id"
occurrence.cause_id = id exemplo


#2

I don’t believe console.log returns anything, so you should just be assigning directly: data => this.occurrences = data['cause_id'].


#3

Sorry, I think I did not know explain, lets go, Suppose tha I have one table state
States
id name

City
id name id_state

my file home.html

<ion-grid>
    <ion-row>
      <ion-col col-6 col-sm-9 col-md-6 col-lg-4 col-xl-3 id="state" *ngFor="let state of states">
        <a (click)="navigateTo(state)">
        <h5><strong>State</strong></h5>
        <h4><strong>{{ state.name }}</strong></h4>
      </a>
      </ion-col>
    </ion-row>
  </ion-grid>

in my home.ts I have

export class HomePage {

  states: any[];

  constructor(public navCtrl: NavController, public navParams: NavParams, public service: ServiceProvider) {
    this.getData();
  }

  getData() {
    this.service.getStates()
      .subscribe(
        data => this.states = data,
        err => console.log(err)
      );
  }

  navigateTo(state) {
    this.navCtrl.push(CitiesComponent, {
      state: state
    });
  }

}

in my citiesComponent.ts I’m using Searchbar

export class CitiesComponent {

  state: any;
  searchQuery: string = '';
  cities: any[];

  constructor(public navParams: NavParams, public navCtrl: NavController, public service: ServiceProvider) {
    this.state= navParams.get('state');
    this.initializecities();
  }

  initializeCities() {
    this.service.getCities()
      .subscribe(
        data => this.cities= data,
        err => console.log(err)
      );
  }

  getOccurrences(eve: any) {
    this.initializeCities();
    let val = eve.target.value;
    if(val && val.trim() != '') {
      this.cities= this.cities.filter((city) => {
         if (city.name.toLowerCase().indexOf(val.toLowerCase()) > -1 || city.something.toLowerCase().indexOf(val.toLowerCase()) > -1)
         return true;
           else
             return false;
      })
    }
  }

  navigateTo(occurrence) {
    this.navCtrl.push(CityComponent, {
      city: city
    });
  }


}

And now to show the cities in view based in this id State

<ion-searchbar (ionInput)="getCities($event)" placeholder="Search by Cities"></ion-searchbar>
  <ion-list>
    <a *ngFor="let city of cities" (click)="navigateTo(city)" style="text-decoration: none;">
      <ion-item >
        <h2 style="font-weight: 900"><strong>{{ city.name}}</strong></h2>
        <ion-icon name="arrow-forward" item-end></ion-icon>
      </ion-item>
    </a>
  </ion-list>