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
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]
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
add a comment |
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
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]
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
add a comment |
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
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]
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
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
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]
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
angular typescript angular-material-7
edited Nov 10 at 3:13
asked Nov 10 at 3:03
Mark Bell
757
757
add a comment |
add a comment |
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>
1
As simple as that! Thank you
– Mark Bell
Nov 10 at 11:00
add a comment |
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>
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
add a comment |
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>
1
As simple as that! Thank you
– Mark Bell
Nov 10 at 11:00
add a comment |
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>
1
As simple as that! Thank you
– Mark Bell
Nov 10 at 11:00
add a comment |
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>
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>
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
add a comment |
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
add a comment |
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>
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
add a comment |
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>
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
add a comment |
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>
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>
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
add a comment |
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
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.
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.
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%2f53235667%2fhow-to-use-a-ternary-expression-inside-angular-material-datatable-interpolation%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