How to add columnDefinitions into md-table from my component content

Viewed 1291

I'm starting in Angular 2 and didn't found anything like this.

How I said in the title, I've a component with column definitions set in content, like this:

        <tabela-com-consulta [dataSource]="ds">
            <ng-container cdkColumnDef="id">
                <md-header-cell *cdkHeaderCellDef md-sort-header >ID</md-header-cell>
                <md-cell *cdkCellDef="let row">{{row.id}}</md-cell>
            </ng-container>
            <ng-container cdkColumnDef="nome">
                <md-header-cell *cdkHeaderCellDef md-sort-header>Name</md-header-cell>
                <md-cell *cdkCellDef="let row">{{row.name}}</md-cell>
            </ng-container>
        </tabela-com-consulta>

Now this is my component template:

<div id="ls" fxFlexFill>
    <md-table [dataSource]="dataSource" mdSort>
        <!-- Add this for each ContentChildren of my component
                <ng-container cdkColumnDef="id">
                    <md-header-cell *cdkHeaderCellDef md-sort-header>ID</md-header-cell>
                    <md-cell *cdkCellDef="let row">{{row.id}}</md-cell>
                </ng-container>
         -->
        <md-header-row *cdkHeaderRowDef="displayedColumns"></md-header-row>
        <md-row *cdkRowDef="let row; columns: displayedColumns;"></md-row>
    </md-table>
</div>

And the component ts code:

@Component({
    selector: 'tabela-com-consulta',
    templateUrl: 'tabela_com_consulta.component.html',
    styleUrls: ['tabela_com_consulta.component.css']
})
export class TabelaComConsultaComponent<T> implements OnInit, OnDestroy, AfterViewInit {

[...]

    @ContentChildren(CdkColumnDef) columnDefinitions: QueryList<CdkColumnDef>;

    ngAfterViewInit() {

    }
}

I can't figure out how to use the columnDefinitions taken from @ContentChildren to put in md-table content. And I don't know if I'm doing this right too. My goal is have the column definitions in content of my component and get it into the md-table. How to do that?

1 Answers
Related