style binding dynamic, 4 different color - html

I want to style my tabel like this:
I try this code, but for 2 colors.
<table *ngFor="let item of notif">
<tr class="cell" [style.border-color]="item.alarmnr === 1 ? 'green' : 'red' ">
</tr>
</table>
But I need for 4 or more color.
I have 4 alarmnrumber,
alarmnr = 1 --> red
alarmnr = 2 --> blue
alarmnr = 3 --> yellow
alarmnr = 4 --> green
How to implement? Can you suggest me any idea please?
Thnx

I would advise you to do a function in the component something like this :
getColorByAlarmnr(alarmnr: number) : string {
switch(alarmnr){
case 1 : return 'red';
case 2 : return 'blue';
case 3 : return 'yellow';
case 4 : return 'green'
}
}
and in the html :
<table *ngFor="let item of notif">
<tr class="cell" [style.border-color]="getColorByAlarmnr(item.alarmnr)">
</tr>
</table>
Or you can define an object/ a map with key, value pairs for the right colors :
let colors = {
1:'red'
2:'blue'
3:'yellow',
4:'green'
}
and in the html :
<table *ngFor="let item of notif">
<tr class="cell" [style.border-color]="colors[item.alarmnr]">
</tr>
</table>

yes you can create a function in ts file
and return class or style on your logic to a variable and use that variable
to [style.border-color] ="yourvariable"

Try using ngClass for this
<table *ngFor="let item of notif">
<tr class="cell" [ngClass]="{'red': item.alarmnr === 1,
'blue': item.alarmnr === 2,
'yellow': item.alarmnr === 3,
'green' : item.alarmnr === 4}">
</tr>
</table

Related

Bootstrap tooltip is switching width on hover

I have a specific problem with my angular web-application. I'm viewing a matrix in my application with data in it. When I hover over my data in this matrix it will show some basic information about the object. anyway: everytime when I hover over it with my cursor, it changes the width from right to left.
my HTML:
<table class="table table-bordered" style="margin-top: 22px">
<colgroup *ngFor="let y of yArray">
<col style="width: 5%" *ngFor="let x of xArray"/>
</colgroup>
<tr *ngFor="let y of yArray">
<td *ngFor="let x of xArray" [style.background]="getBackground(y+1,x+1)"
[tooltip]="getTooltipContent(y+1,x+1)" placement="bottom">
{{ setBerechnung(y+1, x+1).messwert}}
</td>
</tr>
</table>
my GetTooltipContent-Function (TS/Angular):
public getTooltipContent(yKoordinate: number, xKoordinate: number)
{
const sensor = this.fullZoneResponse.sensorInSurface
.filter(x => x.xKoordinate == xKoordinate && x.yKoordinate == yKoordinate);
let zoneAssignment: IzoneAssignment[] = [];
this.fullZonenResponse.zoneInSurface.forEach((value) => {
zoneAssignment.push(...value.assignmentOfZone);
});
const sensorIsInSurface = zoneAssignment.filter(x => x.idSensor == sensor[0].id);
if (sensorIsInSurface.length > 0) {
this.zoneAssignment = this.fullZonenResponse.zoneInSurface.filter(x => x.id == sensorAssignment[0].idZone);
return this.tooltipContent = this.zoneAssignment[0].description;
}
else
{
return this.tooltipContent = "";
}
}
here is a screenshot of it's normal behaviour:
and another one of it's bad behaviour:
I would be very very thankful if you could help me out!
I fixed it by my own!
The problem was that my ngx-bootstrap tooltip got added to my DOM on runtime. I just added the "container='body'"-attribute, so the tooltip get's added on the right element.
my HTML now:
<tr *ngFor="let y of yArray">
<td *ngFor="let x of xArray" container="body" [style.background]="getBackground(y+1,x+1)" [tooltip]="getTooltipContent(y+1,x+1)" placement="bottom">
{{ setBerechnung(y+1, x+1).messwert}}
</td>
</tr>

Need to apply class based on the condition in the header of the table

I need to implement a specific class on the td based on if else condition. If you see my code I am looping through th and td elements. I need to either blank the text in the th or set the color to white if the th class name equals Legal Class ID so that header text is not visible. I have created a class called cellbgcolor. I need to apply only if the header text is Legal Class ID for all others it should apply class tableItem bold. How do I do that. I have shared the html and jsfiddle below
html
<style>
.cellbgcolor {
color: white;
}
</style>
<div *ngIf="LegalFundClasses && LegalFundClasses.LegalFundClassDetailsViewModel && ColumnNames">
<table class="fundClassesTable table-striped">
<tr *ngFor="let c of ColumnNames">
<th class="tableItem bold">{{ c }}</th>
<ng-container *ngFor="let f of LegalFundClasses.LegalFundClassDetailsViewModel; let i=index">
<td class="tableItem" *ngIf="c == ColumnNames[0]">{{f.Description}}</td>
<td class="tableItem" *ngIf="c == ColumnNames[1]">{{f.AuditSummary}}</td>
<td class="tableItem" *ngIf="c == ColumnNames[2]">{{f.Id}}</td
</ng-container>
</tr>
</table>
</div>
Component
public ColumnNames: string[] = ['Legal Class Name',
'Last Edited',
'Legal Class ID'
];
Here is the JSFiddle
https://jsfiddle.net/zyk9xhd1/2/
try something like this:
<td [class.tableItem]="booleanCondition" [class.cellbgcolor]="booleanCondition">...</td>
or
<td [class]="booleanCondition ? 'tableItem' : 'cellbgcolor'">...</td>
Use ngClass
`<th [ngClass]="c == ColumnNames[2] ? 'cellbgcolor' : 'tableItem bold'">{{ c }}</th>`
Updated Fiddle
Try this, hope this helps..
<th [ngClass]="{'cellbgcolor':c == ColumnNames[2],'tableItem bold':c != ColumnNames[2]}">{{ c }}</th>
OR
<th [ngClass]="c == ColumnNames[2] ? 'cellbgcolor' : 'tableItem bold'">...</th>

PrimeNG Table [rowHovered] not working on cells where the cell-background is set

I have a scrollable TreeTable with the rowHover-attribute defined like this:
<p-treeTable [value]="items" [columns]="scrollableCols" [resizableColumns]="true" [frozenColumns]="frozenCols" [scrollable]="true" scrollHeight="550px" frozenWidth="600px" [rowHover]="true" *ngIf="submitFinished">
So I set the background of the second and third columns - I do this like this:
<td style="text-align: center; height: 40px" [ngStyle]="{'background-color' : (col.field=='diff') ? ((getColumnValue(rowData, columns, i) > 0) ?
'#e2fee2' : ((getColumnValue(rowData, columns, i) < 0) ? '#ffa695' : 'white') ) : 'white'}"> <!-- this code only applies on each 2nd and 3rd column, not the first one. The second is always white, the third is either #ffa695, #e2fee2 or white - the first still has its default value -->
This works out completely fine, sadly the rowHover is not only hovering the cells where I did not change the background. Is there a chance to avoid this and still hover the complete row? It is fine if you cannot see the colored background while the row hovers.
Edit:
I assume that it does that because applying the [ngStyle] "keeps tracking" the value of the field and then applies the color to it, so even the selection from rowHover should be overridden. Sadly I have no idea how to work around that.
This is what I would like it to look like: (example created by not applying [ngStyle] of the <td> mentioned above)
Still, the code if it helps:
The full scrollable body (where it should apply):
<ng-template pTemplate="body" let-rowData="rowData" let-columns="columns">
<tr *ngIf="!rowData.hours"><td *ngFor="let col of columns" style="height: 60px" class="psp-row-cell"></td></tr>
<tr *ngIf="rowData.hours">
<ng-template ngFor let-i="index" let-col [ngForOf]="columns">
<ng-template [ngIf]="rowDataIsEmpty(rowData, col)" [ngIfElse]="editableColumn">
<td class="blocked-cell" style="height: 40px">
</td>
</ng-template>
<ng-template #editableColumn>
<ng-template [ngIf]="col.field=='plan'" [ngIfElse]="blockedCell">
<td style="text-align: center; height: 40px" [ttEditableColumn]="rowData">
<p-treeTableCellEditor>
<ng-template pTemplate="input">
<input pInputText type="text" style="text-align: center" [ngModel]="getColumnValue(rowData, columns, i)" (ngModelChange)="setColumnValue(rowData, columns, i, $event)" [ngStyle]="{'width':'100%'}">
</ng-template>
<ng-template pTemplate="output">{{getColumnValue(rowData, columns, i)}}</ng-template>
</p-treeTableCellEditor>
</td>
</ng-template>
<ng-template #blockedCell>
<td style="text-align: center; height: 40px" [ngStyle]="{'background-color' : (col.field=='diff') ? ((getColumnValue(rowData, columns, i) > 0) ?
'#e2fee2' : ((getColumnValue(rowData, columns, i) < 0) ? '#ffa695' : 'white') ) : 'white'}">
{{getColumnValue(rowData, columns, i)}}
</td>
</ng-template>
</ng-template>
</ng-template>
</tr>
</ng-template>
The getColumnValue:
getColumnValue(rowData: any[], columns: any, i: number): number {
let col = columns[i];
let val;
if (col.field == 'diff') {
col = columns[i-2];
val = rowData['hours'].find(entry => entry.year === col.year && entry.month === col.month)[col.field];
col = columns[i-1];
val = val - rowData['hours'].find(entry => entry.year === col.year && entry.month === col.month)[col.field];
} else {
val = rowData['hours'].find(entry => entry.year === col.year && entry.month === col.month)[col.field];
}
return val;
}
As assumed, the background-color seems to be overriden by the [ngStyle] when changed by rowHover. One workaround was to simply change the white back to transparent, so:
[ngStyle]="{'background-color' : (col.field=='diff') ? ((getColumnValue(rowData, columns, i) > 0) ?
'#e2fee2' : ((getColumnValue(rowData, columns, i) < 0) ? '#ffa695' : 'transparent') ) : 'transparent'}
which is kind of working for this problem.

*ngFor to print only the first element of an array

i'm using this code:
<div *ngIf="gamedata.notOver" class="columns">
<div class="column has-text-centered" *ngFor="let board of boards; let i = index">
<table class="is-bordered" [style.opacity]="i == player ? 0.5 : 1">
<tr *ngFor="let row of board.tiles; let j = index">
<td *ngFor="let col of row; let k = index" (click)="boardListener($event)" [style.background-color]="col.used ? '' : 'transparent'" [class.win]="col.status == 'win'" [class.fail]="col.status !== 'win'" class="battleship-tile" id="t{{i}}{{j}}{{k}}">
{{ col.value == "1" ? (col.status == "hit" ? "💀" : "X") : (col.status == "miss" ? "⛶" : (col.status == "hit" ? "💀" : "🌊")) }}
</td>
</tr>
</table>
<hr>
</div>
</div>
to print the first element of an array of arrays that contains 2 elements, how can i choose to print the first or the second element without printing both of them? i need to do this since i have to put both of them in separate divs.
I assume the line that refers to the array of arrays is the board of boards one.
Try wrapping each array in an ng-content that has an ngIf directive as follows:
<div class="column has-text-centered" *ngFor="let board of boards; let i = index">
<ng-content *ngIf="i%2 == 0">
INSERT WHAT YOU WANT TO DISPLAY FOR FIRST ARRAY ITEM HERE
</ng-content>
<ng-content *ngIf="i%2 != 0">
INSERT WHAT YOU WANT TO DISPLAY FOR SECOND ARRAY ITEM HERE
</ng-content>
</div>
You may have to repeat some code, but it should work.

how to display only selected columns and ignore empty cells in html table using ngFor

I am trying to display specific columns in table in angular 2. So my table element looks like this:
<table class="ui single line table" id="tablej">
<tbody>
<tr *ngFor="let row of data;let i=index">
<td *ngFor="let val of row;let j=index" >
<div *ngIf="j<=2 || j==9 || j==17||j==20||j==21">
{{val}}
</div>
</td>
</tr>
</tbody>
</table>
I am getting empty spaces for skipped columns and my table became uneven. Is there any way to display specific columns and ignore the rest?
You can wrap your tr within a template tag to display or not display that column:
<template *ngFor="let row of data;let i=index">
<tr *ngIf="condition to display this column">
<td *ngFor="let val of row;let j=index" >
<div *ngIf="j<=2 || j==9 || j==17||j==20||j==21">
{{val}}
</div>
</td>
</tr>
</template>
The conditional row might be:
<template *ngFor="let row of data;let i=index">
<tr *ngIf="displayRow(row)">
<td *ngFor="let val of row;let j=index" >
{{val}}
</td>
</tr>
</template>
public displayRow(row):boolean {
return row.length <= 2 || row.length == 9 || row.length == 17 || row.length == 20 || row.length == 21
}
Hope that helps