Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic Columns in md table #6159

Closed
ghost opened this issue Jul 31, 2017 · 10 comments
Closed

Dynamic Columns in md table #6159

ghost opened this issue Jul 31, 2017 · 10 comments

Comments

@ghost
Copy link

ghost commented Jul 31, 2017

Bug, feature request, or proposal:

I guess is a bug

What is the expected behavior?

  1. Load columns definitions via httpService
  2. Load data.
  3. Show Data Table.
  4. Be happy.

What is the current behavior?

1.Load columns definitions via httpService.
2. Crash -> Error: cdk-table: Could not find column with id "FooColumn".
3. No Show Data Table.
4. Be sad and cry for two hours.

What are the steps to reproduce?

table.template.html

<md-table [dataSource]="dataSource"> <ng-container *ngFor="let attr of meta.attributes"> <ng-container [cdkColumnDef]="attr.name"> <md-header-cell *cdkHeaderCellDef >{{attr.label}}</md-header-cell> <md-cell *cdkCellDef="let row"> {{row[attr.name]}} </md-cell> </ng-container> </ng-container> <md-header-row *cdkHeaderRowDef="displayedColumns"></md-header-row> <md-row *cdkRowDef="let row; columns: displayedColumns;"></md-row> </md-table>

table.component.ts
`
export class ProductosComponent implements OnInit {

displayedColumns = [];
private dataSource: ProductoDataSource | null;

meta: any = {
    attributes: []
};


constructor(private displayService: DisplayService) {
   }


ngOnInit() {
    this.displayService
        .ObtenerTabla("PRODUCTOS")
        .subscribe(meta => {
            meta.Columnas.forEach(x => {
                this.meta.attributes.push({ label: x.Nombre, name: x.Propiedad });
                this.displayedColumns.push(x.Nombre);
            });
            //this.dataSource = new ProductoDataSource(this.apiService);
        });
}

}
`

Is there anything else we should know?

I found this example
https://plnkr.co/edit/WKlClMq39ZOUBaQ7NhKx?p=preview
just add *ngIf="meta.attributes.length' for load the table when all is loaded,
but when I do this the table don't displayed.
Sorry for my english.
Thanks in Advance.

@andrewseguin
Copy link
Contributor

Dynamic column definitions (using ngFor to stamp out column definitions) were added in #5545 and are not yet released. Plunker uses a build based off the latest master so that's why it works there.

To install the latest build, run this to install and save it to your npm package:

npm install --save angular/material2-builds angular/cdk-builds

@ghost
Copy link
Author

ghost commented Jul 31, 2017

@andrewseguin Thank you

@fnotess
Copy link

fnotess commented Aug 28, 2017

<md-table #table [dataSource]="dataSource" mdSort>
	    <ng-container *ngFor="let col of displayedColumns" cdkColumnDef={{col}}>
		    <md-header-cell  *cdkHeaderCellDef md-sort-header> {{ col }}</md-header-cell>
		    <md-cell  *cdkCellDef="let row"> {{row[col]}}</md-cell>
	    </ng-container>

this is how i did it..

@wooddani
Copy link

Any additional documentation on how to get this working? neither @Fnote 's comment nor the https://material.angular.io/components/table/examples seem to show how dynamic columns would work.

@andrewseguin
Copy link
Contributor

There's no extra documentation yet, but if you have any specific questions feel free to ask them here. You should be able to dynamically change the column definitions as well as change the header and row's column inputs

@wooddani
Copy link

wooddani commented Sep 11, 2017 via email

@willshowell
Copy link
Contributor

@wooddani there's an open issue (#5927) for adding docs on this

Maybe #5927 (comment) will help you.

@romulof
Copy link

romulof commented Feb 20, 2018

MatTable don't support empty rows and/or columns, which is my case right before an initial request is responded.

@mojtaba-ramezani
Copy link

@Fnote tnx

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jun 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants