In my HTML file, I've duplicated the "elseBlock2". How do I implement directive (ng-template) to handle it?
<div class="col-2">
<ng-container *ngIf="item.isSuccess; else elseBlock2">
{{getSimplifiedSize(item.file.size)}}
</ng-container>
<ng-container *ngIf="item.progress; else elseBlock2">
{{item.progress}}%
<mat-progress-bar mode="determinate" color="primary" value={{item.progress}}></mat-progress-bar>
</ng-container>
</div>
Can work something like this?
<div class="col-2">
<ng-container *ngIf="(item.isSuccess || item.progress); else elseBlock2">
<ng-container *ngIf="item.isSuccess">
{{getSimplifiedSize(item.file.size)}}
</ng-container>
<ng-container *ngIf="item.progress">
{{item.progress}}%
<mat-progress-bar mode="determinate" color="primary" value={{item.progress}}></mat-progress-bar>
</ng-container>
</ng-container>
<ng-template #elseBlock2>
Your content
</ng-template>
</div>
Update:
If you want to display the elseBlock only when it's both not in progress and success you can do something like this:
<div class="col-2">
<ng-container *ngIf="item.isSuccess">
{{getSimplifiedSize(item.file.size)}}
</ng-container>
<ng-container *ngIf="item.progress">
{{item.progress}}%
<mat-progress-bar mode="determinate" color="primary" value={{item.progress}}></mat-progress-bar>
</ng-container>
<ng-container *ngIf="!item.progress && !item.isSuccess">
Your content
</ng-container>
</div>
You can use if then else with ternary operator to achieve else-if in Angular
<ng-container
*ngIf="item.isSuccess then successBlock; else (item.progress) ? progressBlock : elseBlock"
></ng-container>
<ng-template #successBlock>
{{ getSimplifiedSize(item.file.size) }}
</ng-template>
<ng-template #progressBlock>
{{ item.progress }}%
</ng-template>
<ng-template #elseBlock>
Error
</ng-template>
In else statement, instead of passing a template, do another conditional check with ternary operator and pass other two templates as values
Related
I am using Angular 12 with Material... I have a main page with a mat-table.
In the HTML I have an Edit() button inside the table and a create() button outside the table.
This is the call .ts in the to the popup Windows. In both cases I call the same function.
callDialog(id:any)
{
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
dialogConfig.width = "60%";
dialogConfig.data=id;
dialogConfig.backdropClass= 'bdrop';
const dialogRef = this.dialog.open(NotificationDetailComponent,dialogConfig);
dialogRef.afterClosed().subscribe(result => {
this.NotificationService();
});
}
create() {
this.callDialog(null);
}
edit(id:string){
this.callDialog(id);
}
When I open the dialog window, the parent window has a shadow.
When I close the popup windows, OnEdit (the button is inside the table), the shadow disappear and all come back to normal. But when I click onCreate() button and then close the popup window, the shadows does not disappear.
In other words.. the shadows in the parent window does not disappear if the button is outside the table..
I tested adding the Create() button inside the table and it works fine...
this is my HTML
<connector-card [title]="cardTitle">
</connector-card>
<div class="search-div">
<button mat-raised-button (click)="create()">
<mat-icon>add</mat-icon>Create
</button>
<mat-form-field class="search-form-field" floatLabel="never" >
<input matInput [(ngModel)]="searchKey" placeholder="Search" autocomplete="off" (keydown.enter)="search()">
<button mat-button matSuffix mat-icon-button aria-label="Clear" *ngIf="searchKey" (click)="delete()">
<mat-icon>close</mat-icon>
</button>
</mat-form-field>
</div>
<div class="mat-elevation-z8">
<mat-table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="Active">
<mat-header-cell *matHeaderCellDef>Active</mat-header-cell>
<mat-cell *matCellDef="let not">{{not.Active}}</mat-cell>
</ng-container>
<ng-container matColumnDef="NotificationName" >
<mat-header-cell *matHeaderCellDef mat-sort-header>Notification Name</mat-header-cell>
<mat-cell *matCellDef="let not">{{not.NotificationName}}</mat-cell>
</ng-container>
<ng-container matColumnDef="ProcessorName" >
<mat-header-cell *matHeaderCellDef mat-sort-header>Processor Name</mat-header-cell>
<mat-cell *matCellDef="let not">{{not.ProcessorName}}</mat-cell>
</ng-container>
<ng-container matColumnDef="ChannelAndLevel">
<mat-header-cell *matHeaderCellDef mat-sort-header>Channel & Level</mat-header-cell>
<mat-cell *matCellDef="let not">{{not.ChannelAndLevel}}</mat-cell>
</ng-container>
<ng-container matColumnDef="edit">
<mat-header-cell *matHeaderCellDef></mat-header-cell>
<mat-cell *matCellDef="let not">
<button mat-icon-button matTooltip="Click to Edit" color="primary" (click)="edit(not.Id)">
<mat-icon>edit</mat-icon>
</button> </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row;columns:displayedColumns"></mat-row>
</mat-table>
<mat-paginator #paginator [length]="tablePaging.totalRows" [pageIndex]="tablePaging.pageNum" [pageSize]="tablePaging.pageSize" showFirstLastButtons
[pageSizeOptions]="tablePaging.pageSizeOptions" (page)="pageChanged($event)" aria-label="Select page">
</mat-paginator>
</div>
Here is the windows
Another thing I notice if I change the style using
dialogConfig.backdropClass= 'bdrop';
.bdrop {
background-color: #bbbbbbf2;
backdrop-filter: blur(4px);
}
th shadows appears before I open the popup windows, and once opened, the shadow disappears.. But OnCreate() the shadows appears again when I close the opoup windows.
What I am missing?
I had defined my Create button like this
<button mat-raised-button (click)="create()">
<mat-icon>add</mat-icon>Create
</button>
I changed the definition like this
<button (click)="create()">
<mat-icon>add</mat-icon>Create
</button>
I removed
mat-raised-button
and now it works fine... Do not know why...
Say I had an ngFor loop surrounding an ngIf statement
<ng-container *ngFor="let item of items">
<ng-container *ngIf="condition1">
//display table1
</ng-container>
</ng-container>
Now, say I had a second table (call it table2) that I want to display if table1 is not displayed.
<ng-container *ngIf="condition1 was never met and table1 is not displayed">
//display table2
<ng-container>
What is the best way to do this, and is there a way to do this using Angular's data binding features? Or, could you point me in the right direction?
You can use standard Angular ngIf, Else like this 👇
<ng-container
*ngIf="condition1; then table1; else table2">
</ng-container>
<ng-template #table1>
<div>
table1
</div>
</ng-template>
<ng-template #table2>
<div>
table2
</div>
</ng-template>
You can read more about it on this post
You can use the ng-template and ngIf-else
<ng-container *ngFor="let item of items">
<ng-container *ngIf="condition1;else table2">
//display table1
</ng-container>
</ng-container>
<ng-template #table2> //display table2 </ng-template>
or
<ng-container *ngFor="let item of items">
<ng-container *ngIf="condition1;then table1 else table2"> </ng-container>
</ng-container>
<ng-template #table1> //display table1 </ng-template>
<ng-template #table2> //display table2 </ng-template>
I have a angular material table with some header column. But under one header column I have some checkboxes for the name of the project.
I have it like this:
<ng-container matColumnDef="projects">
<th mat-header-cell *matHeaderCellDef (mouseover)="show = true" (mouseout)="show = false" mat-sort-header i18n>
<div class="col">
<div class="row" *ngFor="let item of returnProjectCodes; let i = index">
<mat-checkbox
(click)="$event.stopPropagation()"
(change)="filterProjects($event, i, item)"
[checked]="selected === i"
[disabled]="isDisabled"
>{{ item.name }}
</mat-checkbox>
</div>
</div>
Project
</th>
<td mat-cell *matCellDef="let row">{{ row.projects }}</td>
</ng-container>
But now it looks like this:
But of course the header column project has to bee positioned above the checkboxes in line with the other column headers.
So what I have change?
Thank you
I'm building up a Frontend with JSON Data.
I have to go trough a lot of arrays to check if a value exists inside. If the Value doesn't exist, then my complete HTML shouldn't render.
<ng-container *ngIf="document161['_source']['161_Events']">
<div class="row">
<span class="col-md-auto meta-object text-muted"><span>Begräbnistag:</span></span>
<span class="col">
<span *ngFor="let event of document161['_source']['161_Events']">
<span *ngFor="let sub of event.peventsub">
<ng-container *ngIf="sub.content === 'Begräbnis'">
<span *ngFor="let geburtsort of event.pstdatetypesub">
{{geburtsort.content}}
</span>
</ng-container>
</span>
</span>
</span>
</div>
<mat-divider class="my-2"></mat-divider>
</ng-container>
What i have already tried is:
<ng-template #thenBlock>
<ng-container *ngIf="document161['_source']['161_Events']">
<div class="row">
<span class="col-md-auto meta-object text-muted"><span>Begräbnistag:</span></span>
<span class="col">
<span *ngFor="let event of document161['_source']['161_Events']">
<span *ngFor="let sub of event.peventsub">
<ng-container *ngIf="sub.content === 'Begräbnis'">
<span *ngFor="let geburtsort of event.pstdatetypesub">
<ng-container ngIf="*ngIf="geburtsort.content; then thenBlock else elseBlock">
{{geburtsort.content}}
</ng-container>
</span>
</ng-container>
</span>
</span>
</span>
</div>
<mat-divider class="my-2"></mat-divider>
</ng-container>
</ng-template>
<ng-template #elseBlock>
</ng-template>
But as you can see there are multiple problems. Maybe Ii can't reach the ngIf condition if the value doesn't exist. And the then else logic doesn't work inside a ng-container template.
What should be the best solution?
try this
*ngIf="document161?._source?.161_Events?.length > 0"
If array having values it will show that element else will hide same.
I'm working on a small attendance project and have some table component within some mat-tab components. When there is overflow from the table, it scrolls the full component, I only want it to scroll the table on the inner component.
I have tried adding "overflow: auto" in these sections:
on the (app-attendance-table) selector
inside table component
::ng-deep { .mat-tab-body { overflow: auto } }
on the (mat-tab-group) selector
wrapped (app-attendance-table) in an (ng-container) or (div) and adding overflow: auto
This is the outer component with tabs:
<ng-container>
<mat-form-field class="date">
<input
matInput
[matDatepicker]="picker"
placeholder="Select a Date"
(dateInput)="addEvent($event)"
[(ngModel)]="currentDate"
/>
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
</mat-form-field>
</ng-container>
<mat-tab-group>
<ng-container *ngFor="let grade of result; let i = index">
<mat-tab *ngIf="grade.length > 0" [label]="grades[i]">
<app-attendance-table [dataSource]="grade"></app-attendance-table>
</mat-tab>
</ng-container>
</mat-tab-group>
<div *ngIf="this.result.length < 1 && !this.loading">
No Records Found for The Date: {{ currentDate.toDateString() }}
</div>
<mat-spinner *ngIf="this.loading"> </mat-spinner>
</div>
This is the actual table component itself:
<mat-table #table [dataSource]="dataSource">
<ng-container matColumnDef="name">
<mat-header-cell *matHeaderCellDef> Student Name </mat-header-cell>
<mat-cell *matCellDef="let student"
><span
[ngClass]="{
absent: student.isAbsent() && !student.Reason,
finished: student.isAbsent() && student.Reason && !student.editing
}"
>
{{ student.Name }}
</span>
</mat-cell>
</ng-container>
<ng-container matColumnDef="grade">
<mat-header-cell *matHeaderCellDef> Student Grade </mat-header-cell>
<mat-cell
[ngClass]="{
absent: student.isAbsent() && !student.Reason,
finished: student.isAbsent() && student.Reason && !student.editing
}"
*matCellDef="let student"
>
{{ student.Grade }}
</mat-cell>
</ng-container>
<ng-container matColumnDef="status">
<mat-header-cell *matHeaderCellDef> Status </mat-header-cell>
<mat-cell
[ngClass]="{
absent: student.isAbsent() && !student.Reason,
finished: student.isAbsent() && student.Reason && !student.editing
}"
*matCellDef="let student"
>
{{ student.Status }}
</mat-cell>
</ng-container>
<ng-container matColumnDef="reason">
<mat-header-cell *matHeaderCellDef> Reason </mat-header-cell>
<mat-cell *matCellDef="let student">
<mat-form-field
class="reasons"
*ngIf="!student.isPresent()"
appearance="outline"
>
<mat-select
[(ngModel)]="student.Reason"
[disabled]="!student.editing"
placeholder="Select Reason"
(selectionChange)="makeChange(student)"
>
<mat-option
*ngFor="let reason of reasons; let i = index"
[value]="reason"
[disabled]="student.Reason === reason"
>
{{ reason }}
</mat-option>
</mat-select>
</mat-form-field>
</mat-cell>
</ng-container>
<ng-container matColumnDef="comments">
<mat-header-cell *matHeaderCellDef> Comments </mat-header-cell>
<mat-cell *matCellDef="let student">
<mat-form-field *ngIf="!student.isPresent()">
<input
matInput
[(ngModel)]="student.Comments"
[disabled]="!student.editing"
(input)="makeChange(student)"
/>
</mat-form-field>
</mat-cell>
</ng-container>
<ng-container matColumnDef="edit">
<mat-header-cell *matHeaderCellDef> </mat-header-cell>
<mat-cell *matCellDef="let student">
<button
*ngIf="!student.isPresent() && !student.editing"
mat-raised-button
color="primary"
(click)="startEditing(student)"
>
Edit
</button>
<button
*ngIf="!student.isPresent() && student.editing"
mat-raised-button
color="warn"
(click)="saveEdits(student)"
>
Finish
</button>
</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns"></mat-row>
</mat-table>
</div>
To make sure the table is scrollable, wrap the table inside div with a fixed height and make its overflow: auto.
You can also check below links that has both page as well as table as scrollable.
https://angular-qfqpwr.stackblitz.io (Working Stackblitz)
https://material.angular.io/components/table/examples (Table with sticky header)
To extend on #Pankaj Prakash's answer, you should set the overflow property on your outer container to overflow: hidden.
Then wrap a div around the mat cells and set the property to overflow: scroll. If you only want a vertical scroll you could also set the following property instead: overflow-y: scroll