Content projection inside angular-material table












1















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.










share|improve this question



























    1















    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.










    share|improve this question

























      1












      1








      1








      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.










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 10:27









      jedionjedion

      130214




      130214
























          1 Answer
          1






          active

          oldest

          votes


















          1














          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.






          share|improve this answer
























          • 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














          Your Answer






          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          1














          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.






          share|improve this answer
























          • 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


















          1














          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.






          share|improve this answer
























          • 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
















          1












          1








          1







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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





















          • 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






















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python