0

I'm the begginer with angular2, and i try to build bootstrap table with nested components, the child component is display in single cell. Probably I'm doing something wrong with ngFor loop. This is my child component:

import { Component, OnInit, ViewEncapsulation, Input } from '@angular/core';
import { Customer } from '../customer';
import { CustomersComponent } from '../customers.component'

@Component({
  selector: 'single-customer',
  encapsulation: ViewEncapsulation.None,
  inputs:['customer'],
  templateUrl: './single-customer.component.html',
  styleUrls: ['./single-customer.component.css']
})
export class SingleCustomerComponent implements OnInit {
    customer: Customer;

  constructor() { }

  ngOnInit() {
  }
}

and template:

<td>
        {{customer.surname | uppercase}}
    </td>
    <td>
        {{customer.name}}
    </td>
    <td>
        {{customer.phone}}
    </td>
    <td>
        {{customer.mail}}
    </td>
    <td>
        {{customer.comments}}
    </td>
    <td><button type="button" class="btn btn-primary btn-xs" data-toggle="modal" data-target=".update-customer-modal" ng-click="setCustomerData(customer)">Edytuj</button></td>
    <td><button type="button" class="btn btn-danger btn-xs" data-toggle="modal" data-target=".delete-customer-modal" ng-click="setCustomerData(customer)">Usuń</button></td>
<!-- </tr> -->

Parent component:

import { Component, OnInit, Directive, ViewEncapsulation } from '@angular/core';
import { NgFor } from '@angular/common';
import { SingleCustomerComponent } from './single-customer/single-customer.component';
import { Customer } from './customer';
import { CustomersService } from './customers.service';

@Component({
  selector: 'app-customers',
  encapsulation: ViewEncapsulation.None,
  templateUrl: './customers.component.html',
  styleUrls: ['./customers.component.css'],
  providers: [CustomersService], 
})

export class CustomersComponent implements OnInit {

    customers: Customer[];
    customersLength: number;

    constructor(private _customersService: CustomersService) {
     }

    ngOnInit() {
        this.getCustomers();
    }

    getCustomers(){
        this._customersService.getCustomers().then((res) => {
            this.customers = res;
            this.customersLength = this.customers.length;
    });
}
}

parent template:

<div class="col-md-12">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr class="info">
                            <td>ID</td>
                            <td>NAZWISKO</td>
                            <td>IMIĘ</td>
                            <td>TELEFON</td>
                            <td>MAIL</td>
                            <td>URODZINY</td>
                            <td>UWAGI</td>
                            <td></td>
                            <td></td>
                        </tr>
                    </thead>
                    <tbody>
                        <tr *ngFor="let customer of customers">
                       <single-customer [customer]="customer"></single-customer>
                        </tr>

                    </tbody>
                </table>
            </div>
        </div>

and parents module:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CustomersComponent } from './customers.component';
import { SingleCustomerComponent } from './single-customer/single-customer.component'

@NgModule({
  imports: [
    CommonModule,
  ],
  declarations: [CustomersComponent, SingleCustomerComponent]
})
export class CustomersModule { }

What I'm doing wrong?

3
  • Hello, what error you are getting please specify that Commented Apr 20, 2017 at 4:44
  • when I put the component as a table row it's not display as a row, but each instant of component is in one cell and the table has one column Commented Apr 20, 2017 at 6:26
  • Not getting it clear. Commented Apr 20, 2017 at 6:31

1 Answer 1

3

Table element in HTML just allows thead, tbody, tfoot and tr as children.

You can change your children component's selector to:

@Component({
    selector: '[single-customer]',
    ...
})
export class SingleCustomerComponent implements OnInit {
    ...

And change your parent template to:

...    
<tbody>
    <tr *ngFor="let customer of customers"
        single-customer
        [customer]="customer">
    </tr>
</tbody>
...
Sign up to request clarification or add additional context in comments.

3 Comments

I'm trying to do something very similar. Except, I need two tr elements for each customer. So I can't attach the ngFor to the <tr> because it shows only one <tr> and I can't wrap two <tr>s in a component because then I get the extra tag around the <tr>s which breaks the table.
Did you try to wrap your two <tr> inside a <ng-container>? It doesn't create a wrapper element
This other post actually gave me the exact answer I needed: stackoverflow.com/a/39097560/1174250

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.