6

I am trying to create a view that displays a list of clients and I'm running into an issue due to the fact that it's an object and because of Ionic's html types.

Here is my object:

x = { Consultation: [{name: "Joe Smith}, {name: "Jane Doe"}],
   Re-evaluation: [{name: "Joe Smith2}, {name: "Jane Doe2"}],
   Meeting: [{name: "Joe Smith3}, {name: "Jane Doe3"}],
   Testing: [{name: "Joe Smith4}, {name: "Jane Doe4"}]
}

and I define appointment_types as Object.keys(x) aka ["Consultation", "Re-evaluation", "Meeting", "Testing"].

In my view:

<ion-list>
  <ion-list-header *ngFor="let type of appointment_types">{{ type }}</ion-list-header>

  <!-- type is no longer defined after ion-list-header closes -->
  <ion-item-sliding class="shaded-slider" *ngFor="let client of appointment_types[type]"> 
    <ion-item>{{ client.name }}</ion-item>
  </ion-item-sliding>
</ion-list>

Is there something I can do to stay inside the appointment_types loop?

2
  • Iin order to access type object in <ion-item-sliding, you should put the first *ngFor in the ion-list. Commented May 23, 2017 at 21:21
  • Try adding a div with your ngFor that encompasses all. like <div *ngFor="let type of appointment_type><ion-item-sliding *ngFor...></ion-item-sliding></div> Commented May 24, 2017 at 6:47

1 Answer 1

6

You can solve this by having your iteration of the type in ion-list:

<ion-list *ngFor="let type of appointment_types">

and then when you have the type and your original object x, you can iterate the x object with inner arrays like:

<ion-item-sliding class="shaded-slider" *ngFor="let client of x[type]"> 

So your complete template would look like this:

<ion-list *ngFor="let type of appointment_types">
  <ion-list-header >{{ type }}</ion-list-header>
  <ion-item-sliding class="shaded-slider" *ngFor="let client of x[type]"> 
    <ion-item>{{ client.name }}</ion-item>
  </ion-item-sliding>
</ion-list>

Here's a PLUNKER

Sign up to request clarification or add additional context in comments.

2 Comments

I was thinking that, but I know their <ion-list> elements have pre-determined CSS and I wasn't sure if I really wanted to re-create a list with each iteration
Not really understanding what you mean with your comment, but that might very well be because of that I haven't used ionic except here on SO :) But if you want to somehow build this differently, you could also wrap it with <ng-container *ngFor="....."></ng-container> but I don't know how that affects the rest of the code css-wise. But you can experiment and see how it would play out :)

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.