How to use a ternary expression inside Angular Material Datatable interpolation to display html









up vote
0
down vote

favorite












I have an Angular Material Datatable setup in my project. My project uses several tables to display various dataSources (clients, providers, trainees, companies) so my aim is to use one datatable, in a component and pass the data, columns and such in dynamically from the given services.



<ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
<th mat-header-cell *matHeaderCellDef mat-sort-header> <span style="text-transform: capitalize"> column </span> </th>
<td mat-cell *matCellDef="let element">
element[column] // I need to use an expression here
</td>
</ng-container>


My columns are like so



 displayedColumns: string = ['name', 'address1', 'city', 'postcode', 'region', 'options'];


So this one block builds the full table contents dynamically. You notice I have the options column at the end. This field is not present in the datasource, it is to accommodate a button group so I can offer features such as delete or edit the respective row.



I cannot use a structural directive on the 'mat-cell' element as it already has a *matCellDef attributed. So I figured I could write a simple ternary expression within the interpolation braces to check if the column has the 'options' column-header and switch out the would-be data for my buttons.



<ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
<th mat-header-cell *matHeaderCellDef mat-sort-header> <span style="text-transform: capitalize"> column </span> </th>
<td mat-cell *matCellDef="let element">
column == 'options' ? '<button mat-raised-button color="accent">Edit</button>' : element[column]
</td>
</ng-container>


But my IDE complains of an 'unclosed string literal' immediately after I open the template literal to write the button and when rendered on the screen, it gives no errors but makes a fine mess of my table



Wonky Table



Yet if I do not return any HTML element from the ternary, it I get no errors and the table does as I'd expect



 column == 'options' ? 'options column' : element[column] 


Desired Result



I also tried calling a function from the ternary which returned the html into the template but it gets escaped.



Can anyone advise on how I can render a button in the rows that pass my ternary condition?



"@angular/material": "^7.0.3",
"@angular/core": "~7.0.0",


TIA










share|improve this question



























    up vote
    0
    down vote

    favorite












    I have an Angular Material Datatable setup in my project. My project uses several tables to display various dataSources (clients, providers, trainees, companies) so my aim is to use one datatable, in a component and pass the data, columns and such in dynamically from the given services.



    <ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
    <th mat-header-cell *matHeaderCellDef mat-sort-header> <span style="text-transform: capitalize"> column </span> </th>
    <td mat-cell *matCellDef="let element">
    element[column] // I need to use an expression here
    </td>
    </ng-container>


    My columns are like so



     displayedColumns: string = ['name', 'address1', 'city', 'postcode', 'region', 'options'];


    So this one block builds the full table contents dynamically. You notice I have the options column at the end. This field is not present in the datasource, it is to accommodate a button group so I can offer features such as delete or edit the respective row.



    I cannot use a structural directive on the 'mat-cell' element as it already has a *matCellDef attributed. So I figured I could write a simple ternary expression within the interpolation braces to check if the column has the 'options' column-header and switch out the would-be data for my buttons.



    <ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
    <th mat-header-cell *matHeaderCellDef mat-sort-header> <span style="text-transform: capitalize"> column </span> </th>
    <td mat-cell *matCellDef="let element">
    column == 'options' ? '<button mat-raised-button color="accent">Edit</button>' : element[column]
    </td>
    </ng-container>


    But my IDE complains of an 'unclosed string literal' immediately after I open the template literal to write the button and when rendered on the screen, it gives no errors but makes a fine mess of my table



    Wonky Table



    Yet if I do not return any HTML element from the ternary, it I get no errors and the table does as I'd expect



     column == 'options' ? 'options column' : element[column] 


    Desired Result



    I also tried calling a function from the ternary which returned the html into the template but it gets escaped.



    Can anyone advise on how I can render a button in the rows that pass my ternary condition?



    "@angular/material": "^7.0.3",
    "@angular/core": "~7.0.0",


    TIA










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have an Angular Material Datatable setup in my project. My project uses several tables to display various dataSources (clients, providers, trainees, companies) so my aim is to use one datatable, in a component and pass the data, columns and such in dynamically from the given services.



      <ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
      <th mat-header-cell *matHeaderCellDef mat-sort-header> <span style="text-transform: capitalize"> column </span> </th>
      <td mat-cell *matCellDef="let element">
      element[column] // I need to use an expression here
      </td>
      </ng-container>


      My columns are like so



       displayedColumns: string = ['name', 'address1', 'city', 'postcode', 'region', 'options'];


      So this one block builds the full table contents dynamically. You notice I have the options column at the end. This field is not present in the datasource, it is to accommodate a button group so I can offer features such as delete or edit the respective row.



      I cannot use a structural directive on the 'mat-cell' element as it already has a *matCellDef attributed. So I figured I could write a simple ternary expression within the interpolation braces to check if the column has the 'options' column-header and switch out the would-be data for my buttons.



      <ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
      <th mat-header-cell *matHeaderCellDef mat-sort-header> <span style="text-transform: capitalize"> column </span> </th>
      <td mat-cell *matCellDef="let element">
      column == 'options' ? '<button mat-raised-button color="accent">Edit</button>' : element[column]
      </td>
      </ng-container>


      But my IDE complains of an 'unclosed string literal' immediately after I open the template literal to write the button and when rendered on the screen, it gives no errors but makes a fine mess of my table



      Wonky Table



      Yet if I do not return any HTML element from the ternary, it I get no errors and the table does as I'd expect



       column == 'options' ? 'options column' : element[column] 


      Desired Result



      I also tried calling a function from the ternary which returned the html into the template but it gets escaped.



      Can anyone advise on how I can render a button in the rows that pass my ternary condition?



      "@angular/material": "^7.0.3",
      "@angular/core": "~7.0.0",


      TIA










      share|improve this question















      I have an Angular Material Datatable setup in my project. My project uses several tables to display various dataSources (clients, providers, trainees, companies) so my aim is to use one datatable, in a component and pass the data, columns and such in dynamically from the given services.



      <ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
      <th mat-header-cell *matHeaderCellDef mat-sort-header> <span style="text-transform: capitalize"> column </span> </th>
      <td mat-cell *matCellDef="let element">
      element[column] // I need to use an expression here
      </td>
      </ng-container>


      My columns are like so



       displayedColumns: string = ['name', 'address1', 'city', 'postcode', 'region', 'options'];


      So this one block builds the full table contents dynamically. You notice I have the options column at the end. This field is not present in the datasource, it is to accommodate a button group so I can offer features such as delete or edit the respective row.



      I cannot use a structural directive on the 'mat-cell' element as it already has a *matCellDef attributed. So I figured I could write a simple ternary expression within the interpolation braces to check if the column has the 'options' column-header and switch out the would-be data for my buttons.



      <ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
      <th mat-header-cell *matHeaderCellDef mat-sort-header> <span style="text-transform: capitalize"> column </span> </th>
      <td mat-cell *matCellDef="let element">
      column == 'options' ? '<button mat-raised-button color="accent">Edit</button>' : element[column]
      </td>
      </ng-container>


      But my IDE complains of an 'unclosed string literal' immediately after I open the template literal to write the button and when rendered on the screen, it gives no errors but makes a fine mess of my table



      Wonky Table



      Yet if I do not return any HTML element from the ternary, it I get no errors and the table does as I'd expect



       column == 'options' ? 'options column' : element[column] 


      Desired Result



      I also tried calling a function from the ternary which returned the html into the template but it gets escaped.



      Can anyone advise on how I can render a button in the rows that pass my ternary condition?



      "@angular/material": "^7.0.3",
      "@angular/core": "~7.0.0",


      TIA







      angular typescript angular-material-7






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 3:13

























      asked Nov 10 at 3:03









      Mark Bell

      757




      757






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You can use ngIf and else syntax as below:



          <button *ngIf="column == 'options';else other" mat-raised-button color="accent">Edit</button>
          <ng-template #other>element[column]</ng-template>





          share|improve this answer
















          • 1




            As simple as that! Thank you
            – Mark Bell
            Nov 10 at 11:00

















          up vote
          1
          down vote













          Yeah that's the beauty of structural directive you cannot have two directive in a same tag - but you can wrap a directive into another



          <ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
          <th mat-header-cell *matHeaderCellDef mat-sort-header> <span style="text-transform: capitalize"> column </span> </th>
          <ng-container *ngIf="column != 'options'">
          <td mat-cell *matCellDef="let element">
          element[column] // I need to use an expression here
          </td>
          </ng-container>
          <ng-container *ngIf="column === 'options'">
          <td mat-cell *matCellDef="let element">
          <button mat-raised-button color="accent">Edit</button>
          </td>
          </ng-container>
          </ng-container>


          This is the one way otherwise you can use if and else



          <ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
          <th mat-header-cell *matHeaderCellDef mat-sort-header> <span style="text-transform: capitalize"> column </span> </th>
          <ng-container *ngIf="column != 'options'; else opt">
          <td mat-cell *matCellDef="let element">
          element[column] // I need to use an expression here
          </td>
          </ng-container>
          <ng-template #opt>
          <td mat-cell *matCellDef="let element">
          <button mat-raised-button color="accent">Edit</button>
          </td>
          </ng-template>
          </ng-container>





          share|improve this answer




















          • Thanks, Rahul. I must have been at the keyboard too long yesterday as I'm sure ng-template was the first thing I tried. I'm going to accept @User3250's answer as it is achieved in less code but your answer does have the desired outcome. Thanks again buddy
            – Mark Bell
            Nov 10 at 10:57










          • No problem - happy coding!
            – Rahul Swamynathan
            Nov 10 at 11:59










          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',
          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%2f53235667%2fhow-to-use-a-ternary-expression-inside-angular-material-datatable-interpolation%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          You can use ngIf and else syntax as below:



          <button *ngIf="column == 'options';else other" mat-raised-button color="accent">Edit</button>
          <ng-template #other>element[column]</ng-template>





          share|improve this answer
















          • 1




            As simple as that! Thank you
            – Mark Bell
            Nov 10 at 11:00














          up vote
          1
          down vote



          accepted










          You can use ngIf and else syntax as below:



          <button *ngIf="column == 'options';else other" mat-raised-button color="accent">Edit</button>
          <ng-template #other>element[column]</ng-template>





          share|improve this answer
















          • 1




            As simple as that! Thank you
            – Mark Bell
            Nov 10 at 11:00












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          You can use ngIf and else syntax as below:



          <button *ngIf="column == 'options';else other" mat-raised-button color="accent">Edit</button>
          <ng-template #other>element[column]</ng-template>





          share|improve this answer












          You can use ngIf and else syntax as below:



          <button *ngIf="column == 'options';else other" mat-raised-button color="accent">Edit</button>
          <ng-template #other>element[column]</ng-template>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 3:35









          User3250

          1,5433825




          1,5433825







          • 1




            As simple as that! Thank you
            – Mark Bell
            Nov 10 at 11:00












          • 1




            As simple as that! Thank you
            – Mark Bell
            Nov 10 at 11:00







          1




          1




          As simple as that! Thank you
          – Mark Bell
          Nov 10 at 11:00




          As simple as that! Thank you
          – Mark Bell
          Nov 10 at 11:00












          up vote
          1
          down vote













          Yeah that's the beauty of structural directive you cannot have two directive in a same tag - but you can wrap a directive into another



          <ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
          <th mat-header-cell *matHeaderCellDef mat-sort-header> <span style="text-transform: capitalize"> column </span> </th>
          <ng-container *ngIf="column != 'options'">
          <td mat-cell *matCellDef="let element">
          element[column] // I need to use an expression here
          </td>
          </ng-container>
          <ng-container *ngIf="column === 'options'">
          <td mat-cell *matCellDef="let element">
          <button mat-raised-button color="accent">Edit</button>
          </td>
          </ng-container>
          </ng-container>


          This is the one way otherwise you can use if and else



          <ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
          <th mat-header-cell *matHeaderCellDef mat-sort-header> <span style="text-transform: capitalize"> column </span> </th>
          <ng-container *ngIf="column != 'options'; else opt">
          <td mat-cell *matCellDef="let element">
          element[column] // I need to use an expression here
          </td>
          </ng-container>
          <ng-template #opt>
          <td mat-cell *matCellDef="let element">
          <button mat-raised-button color="accent">Edit</button>
          </td>
          </ng-template>
          </ng-container>





          share|improve this answer




















          • Thanks, Rahul. I must have been at the keyboard too long yesterday as I'm sure ng-template was the first thing I tried. I'm going to accept @User3250's answer as it is achieved in less code but your answer does have the desired outcome. Thanks again buddy
            – Mark Bell
            Nov 10 at 10:57










          • No problem - happy coding!
            – Rahul Swamynathan
            Nov 10 at 11:59














          up vote
          1
          down vote













          Yeah that's the beauty of structural directive you cannot have two directive in a same tag - but you can wrap a directive into another



          <ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
          <th mat-header-cell *matHeaderCellDef mat-sort-header> <span style="text-transform: capitalize"> column </span> </th>
          <ng-container *ngIf="column != 'options'">
          <td mat-cell *matCellDef="let element">
          element[column] // I need to use an expression here
          </td>
          </ng-container>
          <ng-container *ngIf="column === 'options'">
          <td mat-cell *matCellDef="let element">
          <button mat-raised-button color="accent">Edit</button>
          </td>
          </ng-container>
          </ng-container>


          This is the one way otherwise you can use if and else



          <ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
          <th mat-header-cell *matHeaderCellDef mat-sort-header> <span style="text-transform: capitalize"> column </span> </th>
          <ng-container *ngIf="column != 'options'; else opt">
          <td mat-cell *matCellDef="let element">
          element[column] // I need to use an expression here
          </td>
          </ng-container>
          <ng-template #opt>
          <td mat-cell *matCellDef="let element">
          <button mat-raised-button color="accent">Edit</button>
          </td>
          </ng-template>
          </ng-container>





          share|improve this answer




















          • Thanks, Rahul. I must have been at the keyboard too long yesterday as I'm sure ng-template was the first thing I tried. I'm going to accept @User3250's answer as it is achieved in less code but your answer does have the desired outcome. Thanks again buddy
            – Mark Bell
            Nov 10 at 10:57










          • No problem - happy coding!
            – Rahul Swamynathan
            Nov 10 at 11:59












          up vote
          1
          down vote










          up vote
          1
          down vote









          Yeah that's the beauty of structural directive you cannot have two directive in a same tag - but you can wrap a directive into another



          <ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
          <th mat-header-cell *matHeaderCellDef mat-sort-header> <span style="text-transform: capitalize"> column </span> </th>
          <ng-container *ngIf="column != 'options'">
          <td mat-cell *matCellDef="let element">
          element[column] // I need to use an expression here
          </td>
          </ng-container>
          <ng-container *ngIf="column === 'options'">
          <td mat-cell *matCellDef="let element">
          <button mat-raised-button color="accent">Edit</button>
          </td>
          </ng-container>
          </ng-container>


          This is the one way otherwise you can use if and else



          <ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
          <th mat-header-cell *matHeaderCellDef mat-sort-header> <span style="text-transform: capitalize"> column </span> </th>
          <ng-container *ngIf="column != 'options'; else opt">
          <td mat-cell *matCellDef="let element">
          element[column] // I need to use an expression here
          </td>
          </ng-container>
          <ng-template #opt>
          <td mat-cell *matCellDef="let element">
          <button mat-raised-button color="accent">Edit</button>
          </td>
          </ng-template>
          </ng-container>





          share|improve this answer












          Yeah that's the beauty of structural directive you cannot have two directive in a same tag - but you can wrap a directive into another



          <ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
          <th mat-header-cell *matHeaderCellDef mat-sort-header> <span style="text-transform: capitalize"> column </span> </th>
          <ng-container *ngIf="column != 'options'">
          <td mat-cell *matCellDef="let element">
          element[column] // I need to use an expression here
          </td>
          </ng-container>
          <ng-container *ngIf="column === 'options'">
          <td mat-cell *matCellDef="let element">
          <button mat-raised-button color="accent">Edit</button>
          </td>
          </ng-container>
          </ng-container>


          This is the one way otherwise you can use if and else



          <ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
          <th mat-header-cell *matHeaderCellDef mat-sort-header> <span style="text-transform: capitalize"> column </span> </th>
          <ng-container *ngIf="column != 'options'; else opt">
          <td mat-cell *matCellDef="let element">
          element[column] // I need to use an expression here
          </td>
          </ng-container>
          <ng-template #opt>
          <td mat-cell *matCellDef="let element">
          <button mat-raised-button color="accent">Edit</button>
          </td>
          </ng-template>
          </ng-container>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 3:39









          Rahul Swamynathan

          8861314




          8861314











          • Thanks, Rahul. I must have been at the keyboard too long yesterday as I'm sure ng-template was the first thing I tried. I'm going to accept @User3250's answer as it is achieved in less code but your answer does have the desired outcome. Thanks again buddy
            – Mark Bell
            Nov 10 at 10:57










          • No problem - happy coding!
            – Rahul Swamynathan
            Nov 10 at 11:59
















          • Thanks, Rahul. I must have been at the keyboard too long yesterday as I'm sure ng-template was the first thing I tried. I'm going to accept @User3250's answer as it is achieved in less code but your answer does have the desired outcome. Thanks again buddy
            – Mark Bell
            Nov 10 at 10:57










          • No problem - happy coding!
            – Rahul Swamynathan
            Nov 10 at 11:59















          Thanks, Rahul. I must have been at the keyboard too long yesterday as I'm sure ng-template was the first thing I tried. I'm going to accept @User3250's answer as it is achieved in less code but your answer does have the desired outcome. Thanks again buddy
          – Mark Bell
          Nov 10 at 10:57




          Thanks, Rahul. I must have been at the keyboard too long yesterday as I'm sure ng-template was the first thing I tried. I'm going to accept @User3250's answer as it is achieved in less code but your answer does have the desired outcome. Thanks again buddy
          – Mark Bell
          Nov 10 at 10:57












          No problem - happy coding!
          – Rahul Swamynathan
          Nov 10 at 11:59




          No problem - happy coding!
          – Rahul Swamynathan
          Nov 10 at 11:59

















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53235667%2fhow-to-use-a-ternary-expression-inside-angular-material-datatable-interpolation%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

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo