I'm using a tutorial that takes data from the component.
Here I have to insert every column by myself, instead I want to generate them like the example on the bottom of the page.
<div>
<table mat-table [dataSource]="dataSource" matSort class="mat-elevation-z8">
<ng-container matColumnDef="id">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Id </th>
<td mat-cell *matCellDef="let user"> {{user.id}} </td>
</ng-container>
<ng-container matColumnDef="firstName">
<th mat-header-cell *matHeaderCellDef mat-sort-header> Name </th>
<td mat-cell *matCellDef="let user"> {{user.firstName}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
</div>
Here I wasn't using Material, I generated every column from a string[]:
getColumns(): string[] {
return ['id', 'firstName', 'lastName', 'age'];
}//this is in my service
So if I delete or modify a column in my service, it will be removed in the table.
<table>
<tr>
<th *ngFor="let col of columns" >{{col}}
</th>
</tr>
<tr *ngFor="let user of users">
<td *ngFor="let col of columns">{{user[col]}}</td>
</tr>
</table>
<div>
</div>
How can I apply the same method in the ng-container, th and td's?
You Can Use Like This.
<table class="table table-bordered table-info">
<thead class="bg-primary text-white">
<tr>
<th>Product</th>
<th>Qty</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr *ngFor='let kotitems of cartitems'>
<td>{{kotitems.Product}}</td>
<td>{{kotitems.Qty}}</td>
<td>{{kotitems.Amt}}</td>
</tr>
</tbody>
<tfoot class="bg-primary text-white">
<tr>
<td>Total</td>
<td colspan="2" style="text-align: center;">{{getSum()}}</td>
</tr>
</tfoot>
</table>
Related
I am trying to use this angular material table, like this:
<div class="flex flex-col pb-4 bg-card rounded-2xl shadow overflow-hidden">
<table mat-table class="" [dataSource]="$candidates">
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef> Name</th>
<td mat-cell *matCellDef="let candidate"> {{candidate.name}} </td>
</ng-container>
<ng-container matColumnDef="email">
<th mat-header-cell *matHeaderCellDef> Email</th>
<td mat-cell *matCellDef="let candidate"> {{candidate.email}} </td>
</ng-container>
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef>Position</th>
<td mat-cell *matCellDef="let candidate"> {{candidate.position_id.name}} </td>
</ng-container>
<mat-header-row *matHeaderRowDef="columnsToDisplay"></mat-header-row>
<mat-row *matRowDef="let row; columns: columnsToDisplay"></mat-row>
</table>
</div>
Which should display something like this:
https://material.angular.io/components/table/overview#table-basic
But instead I get this:
Update mat-header-row and mat-row in the template to:
<tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
<tr mat-row *matRowDef="let row; columns: columnsToDisplay"></tr>
Here is my code for my datatable. The columns too packed together, and it's hard to tell them apart. I tried to change the spacing, width of the columns in CSS, add a custom ngClass but nothing had worked. What should I do to fix this?
<table id="order-information" mat-table [dataSource]="dataSource" matSort class="order-table w-100-p mt-24">
<ng-container matColumnDef="DeliveryNumber" >
<th mat-header-cell *matHeaderCellDef mat-sort-header [ngClass]="'w-75'">Tesellüm No</th>
<td mat-cell *matCellDef="let row" [ngClass]="'w-75'">
<div>{{row?.DeliveryNumber}}</div>
</td>
</ng-container>
<ng-container matColumnDef="StockIntegrationCode" >
<th mat-header-cell *matHeaderCellDef mat-sort-header [ngClass]="'w-75'">Stok Kodu</th>
<td mat-cell *matCellDef="let row" [ngClass]="'w-75'"> {{row?.Product?.StockIntegrationCode}} </td>
</ng-container>
<ng-container matColumnDef="ProductName" >
<th mat-header-cell *matHeaderCellDef mat-sort-header [ngClass]="'w-100'">Stok Adı</th>
<td mat-cell *matCellDef="let row" [ngClass]="'w-100'"> {{row?.Product?.ProductName}} </td>
</ng-container>
<ng-container matColumnDef="QuantityIn">
<th mat-header-cell *matHeaderCellDef> Miktar </th>
<td mat-cell *matCellDef="let row"> {{row.QuantityIn}} </td>
</ng-container>
<ng-container matColumnDef="UnitTypeId">
<th mat-header-cell *matHeaderCellDef> Birim </th>
<td mat-cell *matCellDef="let row"> {{row?.UnitTypeId}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns; let i = index"></tr>
</table>
try to remove all width classes (w-100, w-75,...), and see how it looks.
i am trying to use mat table in angular so i can have a table with data the problem is that the headers of the table fields are not in the right side but i want them to be i used RTl but i did not work what should i do?
this is my table right now
this is my html code
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8" class="table" >
<!--- Note that these columns can be defined in any order.
The actual rendered columns are set as a property on the row definition" -->
<!-- Position Column -->
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef > NO </th>
<td mat-cell *matCellDef="let element"> {{element.position}} </td>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef> name </th>
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>
<!-- Weight Column -->
<ng-container matColumnDef="weight">
<th mat-header-cell *matHeaderCellDef> weight </th>
<td mat-cell *matCellDef="let element"> {{element.weight}} </td>
</ng-container>
<!-- Symbol Column -->
<ng-container matColumnDef="symbol">
<th mat-header-cell *matHeaderCellDef> symbol </th>
<td mat-cell *matCellDef="let element"> {{element.symbol}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
this is css
table {
direction: rtl;
width: 1300px;
}
Here a small Stackblitz: https://stackblitz.com/edit/angular-cunndn?file=src/app/table-basic-example.html
You just need to add dir="rtl" to your html tag like...
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8" dir="rtl">
I have to show some data in the HTML table by grouping using rowspan.
Below is the expected GUI
I have the JSON Data like below. JSON Data here
Angular Code
<table class="table table-fixed" border="1">
<thead>
<tr>
<th>Country</th>
<th>State</th>
<th>City</th>
<th>Street</th>
<th>Male</th>
<th>Female</th>
<th>Others</th>
</tr>
</thead>
<tbody>
<ng-container *ngFor="let country of Countries">
<tr *ngFor="let item of [].constructor(country.NoOfStreets); let streetIdx = index">
<ng-container *ngFor="let state of country.States; let stateIdx = index">
<td [attr.rowspan]="state.NoOfStreets" style="width: 15%">
{{state.StateName}}
</td>
</ng-container>
<ng-container *ngFor="let state of country.States; let stateIdx = index">
<ng-container *ngFor="let city of state.Cities; let cityIdx = index">
<td [attr.rowspan]="city.NoOfStreets" style="width: 15%">{{city.CityName}}</td>
<ng-container *ngFor="let street of city.Streets; let streetIdx = index">
<td style="width: 15%">{{street.StreetName}}</td>
<td style="width: 15%">{{street.Male}}</td>
<td style="width: 15%">{{street.Female}}</td>
<td style="width: 15%">{{street.Others}}</td>
</ng-container>
</ng-container>
</ng-container>
</tr>
</ng-container>
</tbody>
</table>
I could not able to generate the expected UI. I get the different UI and not getting rendered properly. I tried this one for almost a week and nothing worked out.
The PLUNK version is https://next.plnkr.co/edit/5nYNZ86BiWDke3GE?open=lib%2Fapp.ts&deferRun=1
What you want is to flatten all streats into array, so that you cal loop over it. The flat code will be:
const concat = (x,y) => x.concat(y)
const flatMap = (f,xs) => xs.map(f).reduce(concat, [])
let states = flatMap(c => c.States.map(s => ({Country:c, State: s})), this.Countries);
let cities = flatMap(c => c.State.Cities.map(s => ({Country:c.Country, State:c.State, City: s})), states);
this.streets = flatMap(c => c.City.Streets.map(str => ({Country:c.Country, State:c.State, City: c.City, Street: str})), cities);
And then easily check if each Country, State and City is first in group like:
<tbody>
<tr *ngFor="let str in streets">
<td *ngIf="firstCountryInGroup(str)" [rowspan]="numberOfCountry(str)">
{{str.Country.CountryName}}
</td>
<td *ngIf="firstStateInGroup(str)" [rowspan]="numberOfStatse(str)">
{{str.State.CityName}}
</td>
<td *ngIf="firstCityInGroup(str)" [rowspan]="numberOfCities(str)">
{{str.City.CityName}}
</td>
<td>{{str.Street.Name}}<td>
<td>{{str.Street.Male}}<td>
<td>{{str.Street.Female}}<td>
<td>{{str.StreetOthers}}<td>
</tr>
</tbody>
We need to have separate columns where we run a loop based on child or sibling - you will get the idea from the comments in the code below also
relevant TS:
<table class="table table-fixed" border="1">
<thead>
<tr>
<th>Country</th>
<th>State</th>
<th>City</th>
<th>Street</th>
<th>Male</th>
<th>Female</th>
<th>Others</th>
</tr>
</thead>
<tbody>
<ng-container *ngFor="let country of Countries; let i = index">
<tr>
<!-- column 1 -->
<td>{{country.CountryName}}</td>
<!-- column 2 -->
<td>
<ng-container *ngFor="let state of country.States">
<tr>
<td> {{state.StateName}} </td>
</tr>
</ng-container>
</td>
<!-- column 3 -->
<td>
<ng-container *ngFor="let state of country.States">
<tr>
<td>
<ng-container *ngFor="let city of state.Cities">
<tr>
<td> {{city.CityName}} </td>
</tr>
</ng-container>
</td>
</tr>
</ng-container>
</td>
<!-- column 4 -->
<td>
<ng-container *ngFor="let state of country.States">
<tr>
<td>
<ng-container *ngFor="let city of state.Cities">
<tr>
<td>
<ng-container *ngFor="let street of city.Streets">
<tr>
<td>
{{street.StreetName}}
</td>
</tr>
</ng-container>
</td>
</tr>
</ng-container>
</td>
</tr>
</ng-container>
</td>
<!-- column 5 -->
<td>
<ng-container *ngFor="let state of country.States">
<tr>
<td>
<ng-container *ngFor="let city of state.Cities">
<tr>
<td>
<ng-container *ngFor="let street of city.Streets">
<tr>
<td>
{{street.Male}}
</td>
</tr>
</ng-container>
</td>
</tr>
</ng-container>
</td>
</tr>
</ng-container>
</td>
<!-- column 6 -->
<td>
<ng-container *ngFor="let state of country.States">
<tr>
<td>
<ng-container *ngFor="let city of state.Cities">
<tr>
<td>
<ng-container *ngFor="let street of city.Streets">
<tr>
<td>
{{street.Female}}
</td>
</tr>
</ng-container>
</td>
</tr>
</ng-container>
</td>
</tr>
</ng-container>
</td>
<!-- column 7 -->
<td>
<ng-container *ngFor="let state of country.States">
<tr>
<td>
<ng-container *ngFor="let city of state.Cities">
<tr>
<td>
<ng-container *ngFor="let street of city.Streets">
<tr>
<td>
{{street.Others}}
</td>
</tr>
</ng-container>
</td>
</tr>
</ng-container>
</td>
</tr>
</ng-container>
</td>
</tr>
</ng-container>
</tbody>
</table>
working stackblitz here
With this solution
https://stackblitz.com/edit/angular-gbhcun?file=src/app/app.component.html
Add below style to meet exact output
table, th, td {
border: 1px solid black;
}
I am working on an Angular Material web-page.
I added a <mat-icon> next to the header text in <mat-table>. After I added the mat-icon, the icon and text are not perfectly aligned. The icon is not vertically centered and looks a little too high. The header text is not aligned with the other column text and looks a little low.
The closest I got was when I moved the mat-icon inside the tag with the text.
Here is my code
<table mat-table [dataSource]="dataSource">
<ng-container matColumnDef="column1">
<th mat-header-cell *matHeaderCellDef style="text-align: center !important" [attr.colspan]="2"><h2>Column 1</h2></th>
</ng-container>
<ng-container matColumnDef="column2">
<th *matHeaderCellDef style="text-align: center !important" [attr.colspan]="2">
<h2><mat-icon class="pointer" style="padding-bottom: 0px; padding-right: 1px; cursor: pointer;" >arrow_left</mat-icon>Column 2</h2>
</th>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedGroupColumns; sticky: true"></tr>
<tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>
</table >
Your attention to detail is reminiscent of a customer I have at work;
To make this happen, we use position:absolute, but enclose it inside a div with position:relative so that we don't fall off the grid
relevant css:
.myCustomHeading{position:relative;padding-top:4px;}
.myIcon{position:relative;}
.mySpan{position:absolute;padding-top:2px;}
relevant HTML:
<table mat-table [dataSource]="dataSource" class="mat-elevation-z8">
<!-- Position Column -->
<ng-container matColumnDef="position">
<th mat-header-cell *matHeaderCellDef> <h2>No.</h2> </th>
<td mat-cell *matCellDef="let element"> {{element.position}} </td>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef>
<div class='myCustomHeading'>
<h2><mat-icon class="pointer myIcon" >arrow_left</mat-icon>
<span class="mySpan">Column 2</span></h2>
</div>
</th>
<td mat-cell *matCellDef="let element"> {{element.name}} </td>
</ng-container>
<tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
<tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
</table>
complete working stackblitz available here