1

I'm trying to create a table with a nested component for rows, here is what I have:

Parent file:

<table class="table">
  <thead>
    <tr>
      <th>ID</th>
      <th>TITLE</th>
      <th>DESCRIPTION</th>
      <th>USER</th>
      <th>OWNER</th>
    </tr>
  </thead>
  <app-ticket *ngFor="let ticket of tickets" [ticket]="ticket"></app-ticket>
</table>

app-ticket:

<tr>
  <td>{{ticket.id}}</td>
  <td>{{ticket.title}}</td>
  <td>{{ticket.description}}</td>
  <td>{{ticket.email}}</td>
  <td>{{ticket.owner}} <button class="btn btn-warning">TEST</button></td>
</tr>

But bootstrap style on rows don't work, I've searched and people suggest putting the tr / td's in the root component, but this is not feasible sometimes, specially with multi-level components, is there a good solution for this?

1 Answer 1

1

One way to achieve this is to make the component a directive-like component:

@Component({
    selector: 'tr[app-ticket]',
    templateUrl: './app-ticket.component.html',
    styleUrls: ['./app-ticket.component.scss']
})
export class AppTicketComponent {

    @Input() ticket: Ticket;

}

html template:

<td>{{ticket.id}}</td>
<td>{{ticket.title}}</td>
<td>{{ticket.description}}</td>
<td>{{ticket.email}}</td>
<td>{{ticket.owner}} <button class="btn btn-warning">TEST</button></td>

and then use it like this:

<tr app-ticket *ngFor="let ticket of tickets" [ticket]="ticket"></tr>

This will directly render the tr inside of the table instead of leaving a tag for your component.

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

Comments

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.