Content projection inside angular-material table
Instead of a regular way of displaying data to table. I'm trying to create my custom-table component and project the data in the material table via .
like this:
<table mat-table [dataSource]="dataSource">
<!-- I want to Render the header and cell data here -->
<ng-content></ng-content>
<mat-header-row *matHeaderRowDef="headers; sticky: true"></mat-header-row>
<mat-row *matRowDef="let row; columns: headers;"></mat-row>
</table>
So I can call this customized component like this:
<app-customized-table>
<ng-container matColumnDef="id">
<mat-header-cell *matHeaderCellDef> Id </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.Id}} </mat-cell>
</ng-container>
...etc
</app-customized-table>
However, it won't detect the content. Here's a stackblitz example that i'm trying to do.
https://stackblitz.com/edit/angular-qwvcln?file=src%2Fapp%2Fcustom-table.component.ts
Is there a way to do this?
Thanks.
angular angular-material
add a comment |
Instead of a regular way of displaying data to table. I'm trying to create my custom-table component and project the data in the material table via .
like this:
<table mat-table [dataSource]="dataSource">
<!-- I want to Render the header and cell data here -->
<ng-content></ng-content>
<mat-header-row *matHeaderRowDef="headers; sticky: true"></mat-header-row>
<mat-row *matRowDef="let row; columns: headers;"></mat-row>
</table>
So I can call this customized component like this:
<app-customized-table>
<ng-container matColumnDef="id">
<mat-header-cell *matHeaderCellDef> Id </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.Id}} </mat-cell>
</ng-container>
...etc
</app-customized-table>
However, it won't detect the content. Here's a stackblitz example that i'm trying to do.
https://stackblitz.com/edit/angular-qwvcln?file=src%2Fapp%2Fcustom-table.component.ts
Is there a way to do this?
Thanks.
angular angular-material
add a comment |
Instead of a regular way of displaying data to table. I'm trying to create my custom-table component and project the data in the material table via .
like this:
<table mat-table [dataSource]="dataSource">
<!-- I want to Render the header and cell data here -->
<ng-content></ng-content>
<mat-header-row *matHeaderRowDef="headers; sticky: true"></mat-header-row>
<mat-row *matRowDef="let row; columns: headers;"></mat-row>
</table>
So I can call this customized component like this:
<app-customized-table>
<ng-container matColumnDef="id">
<mat-header-cell *matHeaderCellDef> Id </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.Id}} </mat-cell>
</ng-container>
...etc
</app-customized-table>
However, it won't detect the content. Here's a stackblitz example that i'm trying to do.
https://stackblitz.com/edit/angular-qwvcln?file=src%2Fapp%2Fcustom-table.component.ts
Is there a way to do this?
Thanks.
angular angular-material
Instead of a regular way of displaying data to table. I'm trying to create my custom-table component and project the data in the material table via .
like this:
<table mat-table [dataSource]="dataSource">
<!-- I want to Render the header and cell data here -->
<ng-content></ng-content>
<mat-header-row *matHeaderRowDef="headers; sticky: true"></mat-header-row>
<mat-row *matRowDef="let row; columns: headers;"></mat-row>
</table>
So I can call this customized component like this:
<app-customized-table>
<ng-container matColumnDef="id">
<mat-header-cell *matHeaderCellDef> Id </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.Id}} </mat-cell>
</ng-container>
...etc
</app-customized-table>
However, it won't detect the content. Here's a stackblitz example that i'm trying to do.
https://stackblitz.com/edit/angular-qwvcln?file=src%2Fapp%2Fcustom-table.component.ts
Is there a way to do this?
Thanks.
<table mat-table [dataSource]="dataSource">
<!-- I want to Render the header and cell data here -->
<ng-content></ng-content>
<mat-header-row *matHeaderRowDef="headers; sticky: true"></mat-header-row>
<mat-row *matRowDef="let row; columns: headers;"></mat-row>
</table>
<table mat-table [dataSource]="dataSource">
<!-- I want to Render the header and cell data here -->
<ng-content></ng-content>
<mat-header-row *matHeaderRowDef="headers; sticky: true"></mat-header-row>
<mat-row *matRowDef="let row; columns: headers;"></mat-row>
</table>
<app-customized-table>
<ng-container matColumnDef="id">
<mat-header-cell *matHeaderCellDef> Id </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.Id}} </mat-cell>
</ng-container>
...etc
</app-customized-table>
<app-customized-table>
<ng-container matColumnDef="id">
<mat-header-cell *matHeaderCellDef> Id </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.Id}} </mat-cell>
</ng-container>
...etc
</app-customized-table>
angular angular-material
angular angular-material
asked Nov 16 '18 at 10:27
jedionjedion
130214
130214
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You would have to take a very different approach to get this to work. As you have noticed, mat-table can't have it's content wrapped in something else. A possibility is to provide just the data to your custom component rather than the the DOM as content. For example:
Component:
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-custom-table',
template: `
<div>
{{name}}
<div class="example-container mat-elevation-z8">
<table mat-table [dataSource]="dataSource">
<mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
<ng-container *ngFor="let column of displayedColumns; index as i" >
<ng-container [matColumnDef]="column">
<mat-header-cell *matHeaderCellDef>{{ columnLabels[i] }}</mat-header-cell>
<mat-cell mat-cell *matCellDef="let element"> {{element[valueKeys[i]]}} </mat-cell>
</ng-container>
</ng-container>
</table>
</div>
asd
</div>
`
})
export class CustomTable implements OnInit {
@Input() public columnLabels: any;
@Input() public displayedColumns;
@Input() public dataSource;
@Input() public name: any;
@Input() public valueKeys: any;
constructor() { }
ngOnInit() {
}
}
Usage:
<app-custom-table name="sdf"
[dataSource]="dataSource"
[displayedColumns]="displayedColumns"
[columnLabels]="columnLabels"
[valueKeys]="displayedColumns">
</app-custom-table>
Where:
displayedColumns = ['position', 'name', 'weight', 'symbol'];
dataSource = ELEMENT_DATA;
columnLabels = ['No.', 'Name', 'Weight', 'Symbol'];
But even this way there might be some more challenges.
Thanks. This is a nice one., but you're right challenges will come along the way, and one is that it's hard to customize data per cell. Well, that actually is my aim in the first place.
– jedion
Dec 4 '18 at 15:01
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335929%2fcontent-projection-inside-angular-material-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You would have to take a very different approach to get this to work. As you have noticed, mat-table can't have it's content wrapped in something else. A possibility is to provide just the data to your custom component rather than the the DOM as content. For example:
Component:
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-custom-table',
template: `
<div>
{{name}}
<div class="example-container mat-elevation-z8">
<table mat-table [dataSource]="dataSource">
<mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
<ng-container *ngFor="let column of displayedColumns; index as i" >
<ng-container [matColumnDef]="column">
<mat-header-cell *matHeaderCellDef>{{ columnLabels[i] }}</mat-header-cell>
<mat-cell mat-cell *matCellDef="let element"> {{element[valueKeys[i]]}} </mat-cell>
</ng-container>
</ng-container>
</table>
</div>
asd
</div>
`
})
export class CustomTable implements OnInit {
@Input() public columnLabels: any;
@Input() public displayedColumns;
@Input() public dataSource;
@Input() public name: any;
@Input() public valueKeys: any;
constructor() { }
ngOnInit() {
}
}
Usage:
<app-custom-table name="sdf"
[dataSource]="dataSource"
[displayedColumns]="displayedColumns"
[columnLabels]="columnLabels"
[valueKeys]="displayedColumns">
</app-custom-table>
Where:
displayedColumns = ['position', 'name', 'weight', 'symbol'];
dataSource = ELEMENT_DATA;
columnLabels = ['No.', 'Name', 'Weight', 'Symbol'];
But even this way there might be some more challenges.
Thanks. This is a nice one., but you're right challenges will come along the way, and one is that it's hard to customize data per cell. Well, that actually is my aim in the first place.
– jedion
Dec 4 '18 at 15:01
add a comment |
You would have to take a very different approach to get this to work. As you have noticed, mat-table can't have it's content wrapped in something else. A possibility is to provide just the data to your custom component rather than the the DOM as content. For example:
Component:
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-custom-table',
template: `
<div>
{{name}}
<div class="example-container mat-elevation-z8">
<table mat-table [dataSource]="dataSource">
<mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
<ng-container *ngFor="let column of displayedColumns; index as i" >
<ng-container [matColumnDef]="column">
<mat-header-cell *matHeaderCellDef>{{ columnLabels[i] }}</mat-header-cell>
<mat-cell mat-cell *matCellDef="let element"> {{element[valueKeys[i]]}} </mat-cell>
</ng-container>
</ng-container>
</table>
</div>
asd
</div>
`
})
export class CustomTable implements OnInit {
@Input() public columnLabels: any;
@Input() public displayedColumns;
@Input() public dataSource;
@Input() public name: any;
@Input() public valueKeys: any;
constructor() { }
ngOnInit() {
}
}
Usage:
<app-custom-table name="sdf"
[dataSource]="dataSource"
[displayedColumns]="displayedColumns"
[columnLabels]="columnLabels"
[valueKeys]="displayedColumns">
</app-custom-table>
Where:
displayedColumns = ['position', 'name', 'weight', 'symbol'];
dataSource = ELEMENT_DATA;
columnLabels = ['No.', 'Name', 'Weight', 'Symbol'];
But even this way there might be some more challenges.
Thanks. This is a nice one., but you're right challenges will come along the way, and one is that it's hard to customize data per cell. Well, that actually is my aim in the first place.
– jedion
Dec 4 '18 at 15:01
add a comment |
You would have to take a very different approach to get this to work. As you have noticed, mat-table can't have it's content wrapped in something else. A possibility is to provide just the data to your custom component rather than the the DOM as content. For example:
Component:
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-custom-table',
template: `
<div>
{{name}}
<div class="example-container mat-elevation-z8">
<table mat-table [dataSource]="dataSource">
<mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
<ng-container *ngFor="let column of displayedColumns; index as i" >
<ng-container [matColumnDef]="column">
<mat-header-cell *matHeaderCellDef>{{ columnLabels[i] }}</mat-header-cell>
<mat-cell mat-cell *matCellDef="let element"> {{element[valueKeys[i]]}} </mat-cell>
</ng-container>
</ng-container>
</table>
</div>
asd
</div>
`
})
export class CustomTable implements OnInit {
@Input() public columnLabels: any;
@Input() public displayedColumns;
@Input() public dataSource;
@Input() public name: any;
@Input() public valueKeys: any;
constructor() { }
ngOnInit() {
}
}
Usage:
<app-custom-table name="sdf"
[dataSource]="dataSource"
[displayedColumns]="displayedColumns"
[columnLabels]="columnLabels"
[valueKeys]="displayedColumns">
</app-custom-table>
Where:
displayedColumns = ['position', 'name', 'weight', 'symbol'];
dataSource = ELEMENT_DATA;
columnLabels = ['No.', 'Name', 'Weight', 'Symbol'];
But even this way there might be some more challenges.
You would have to take a very different approach to get this to work. As you have noticed, mat-table can't have it's content wrapped in something else. A possibility is to provide just the data to your custom component rather than the the DOM as content. For example:
Component:
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-custom-table',
template: `
<div>
{{name}}
<div class="example-container mat-elevation-z8">
<table mat-table [dataSource]="dataSource">
<mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"></mat-row>
<ng-container *ngFor="let column of displayedColumns; index as i" >
<ng-container [matColumnDef]="column">
<mat-header-cell *matHeaderCellDef>{{ columnLabels[i] }}</mat-header-cell>
<mat-cell mat-cell *matCellDef="let element"> {{element[valueKeys[i]]}} </mat-cell>
</ng-container>
</ng-container>
</table>
</div>
asd
</div>
`
})
export class CustomTable implements OnInit {
@Input() public columnLabels: any;
@Input() public displayedColumns;
@Input() public dataSource;
@Input() public name: any;
@Input() public valueKeys: any;
constructor() { }
ngOnInit() {
}
}
Usage:
<app-custom-table name="sdf"
[dataSource]="dataSource"
[displayedColumns]="displayedColumns"
[columnLabels]="columnLabels"
[valueKeys]="displayedColumns">
</app-custom-table>
Where:
displayedColumns = ['position', 'name', 'weight', 'symbol'];
dataSource = ELEMENT_DATA;
columnLabels = ['No.', 'Name', 'Weight', 'Symbol'];
But even this way there might be some more challenges.
answered Nov 16 '18 at 18:00
G. TranterG. Tranter
5,3221625
5,3221625
Thanks. This is a nice one., but you're right challenges will come along the way, and one is that it's hard to customize data per cell. Well, that actually is my aim in the first place.
– jedion
Dec 4 '18 at 15:01
add a comment |
Thanks. This is a nice one., but you're right challenges will come along the way, and one is that it's hard to customize data per cell. Well, that actually is my aim in the first place.
– jedion
Dec 4 '18 at 15:01
Thanks. This is a nice one., but you're right challenges will come along the way, and one is that it's hard to customize data per cell. Well, that actually is my aim in the first place.
– jedion
Dec 4 '18 at 15:01
Thanks. This is a nice one., but you're right challenges will come along the way, and one is that it's hard to customize data per cell. Well, that actually is my aim in the first place.
– jedion
Dec 4 '18 at 15:01
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335929%2fcontent-projection-inside-angular-material-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown