How can I change the background of a table cell dynamically in HTML using AngularJS ?
In the following code I want to show a table with a bunch of names and the including status of an object. The status can weather be VALID (green background) or INVALID (red background).
Can this problem be solved in my <td> HTML tag or do I have to move on to CSS ?
<table border="1">
<tr ng-repeat="object in Cmd.items">
<td>{{object.objectName}}</td>
<td>{{object.objectStatus}}</td> //this background should be colored
</tr>
</table>
You can do this using ng-class:
<tr ng-repeat="object in Cmd.items">
<td>{{object.objectName}}</td>
<td ng-class="{'red': object.objectStatus === 'invalid', 'green': object.objectStatus === 'valid'}">{{object.objectStatus}}</td>
</tr>
Fiddle
you can use ng-class
e.x. :
<div ng-class={'green-bg':isValid, 'red-bg':!isValid}> </div>
green-bg and red-bg
are css classes and isValid is property, where you know expression isValid or no
CSS :
.invalid{
background-color:red;
}
.valid{
background-color:green;
}
HTML
<table border="1">
<tr ng-repeat="object in Cmd.items">
<td>{{object.objectName}}</td>
<td ng-class="object.objectStatus">{{object.objectStatus}}</td> //this background should be colored
</tr>
</table>
It can be solved both in the td tag or/and in your external css.
In Html
<td ng-style="{'background': (object.objectStatus=='VALID') ? 'green' : 'red'}">
{{object.objectStatus}}
</td>
With external css You can use #oto lolua implementation
Related
I want display cricket team details. First row of a table is the captains, so for every first row color, I want to make the font size to be green and bold
This is My HTML:
<div >
<table id="sports" border=1 align=center >
<thead >
<tr>
<th>Team1</th>
<th>Team2</th>
</tr>
</thead>
<tbody *ngFor="let T of Teams" >
<td> {{T.Indian_players}}</td>
<td>{{T.Australia_players}}</td>
</tr>
</tbody>
</table>
</div>
This is my model, where I have taken static data:
export const Players=[
{Indian_players:'Kohili(c)',Australia_players:'David Warner(c)',Pakistan_players:'shaheen Afridi(c)',Southafrica_players:'dale steyn(c)',England_players:'Harry Kane(c)'},
{Indian_players:' Dhoni',Australia_players:'Steve Smith',Pakistan_players:'sarfraz Ahmed',Southafrica_players:'du plessis',England_players:'Joe Root'},
{Indian_players:'Rohit Sharma',Australia_players:'Glen Maxwell',Pakistan_players:'Babar Azam',Southafrica_players:'Imran Tahir',England_players:'Alex Hales'},
{Indian_players:'Jadeja',Australia_players:'Aron Finch',Pakistan_players:'Mohamad Hafeez',Southafrica_players:'David Miller',England_players:'James Anderson'},
{Indian_players:'K.L.Rahul',Australia_players:'Mitchel Starc',Pakistan_players:'Imad Wasim',Southafrica_players:'Jp duminy',England_players:'Moeen Ali'},
{Indian_players:'Bhuvaneswar Kumar',Australia_players:'Travis Head',Pakistan_players:'Shadab khan',Southafrica_players:'de kock',England_players:'Jos Buttler'},
{Indian_players:'Shikar Dhawan',Australia_players:'Pat cummins',Pakistan_players:'yasir shah',Southafrica_players:'Hashim Amla',England_players:'Ben Strokes'},
{Indian_players:'RishabPanth',Australia_players:'Mitchel Marsh',Pakistan_players:'Imam-ul-haq',Southafrica_players:'chris morris',England_players:'Sam Billings'},
{Indian_players:'Ashwin',Australia_players:'Peter siddle',Pakistan_players:'Faheem Ashraf',Southafrica_players:'Aiden markram',England_players:'Eoin Morgan'},
{Indian_players:'Dinesh Karthik',Australia_players:'Tim Paine',Pakistan_players:'Shoib Malik',Southafrica_players:'Dean Elgar',England_players:'chris Woakes'},
You can do the following.
<tbody>
<tr *ngFor="let T of Teams; let i = index" [class.your-captain-css-class]="index===0">
<td> {{T.Indian_players}}</td>
<td>{{T.Australia_players}}</td>
</tr>
</tbody>
I've done the following.
getting the index along with object.
Adding class if index is 0.
This is cover your requirement. There is another way of doing it only by CSS.
You can do it with CSS by doing this
custom-class:first-child {
color: yellow;
font-size: 15px;
}
Also, you have an error in your sample an ending tr tag but no beginning tr tag inside the body
<tbody *ngFor="let T of Teams" >
<td> {{T.Indian_players}}</td>
<td>{{T.Australia_players}}</td>
</tr>
</tbody>
I believe after reading the comments you could/should do this instead and use the CSS at the top and it should all work the reason I suggest using the class instead of tr is because if you are using tr you have used tr more then once and it will affect all tr tags instead of just the one you want in the body.
<tbody>
<tr *ngFor="let T of Teams" class="custom-class">
<td> {{T.Indian_players}}</td>
<td>{{T.Australia_players}}</td>
</tr>
</tbody>
I am working on HTML table with AngularJS. Below is my HTML code.
html code:
<table>
<tr ng-repeat="data in myResults">
<td style="text-align: center;">{{data.name}}</td>
<td style="text-align: center;">{{data.callerID}}</td>
<td style="text-align: center;">{{data.dataPlan}}</td>
</tr>
</table>
I am returning the value in myResults object and iterating and showing the values in the table.
I want to fill the particular <td> with color if the value is blank or null while iterating, Example) While iterating if i have the values of name and callerID but the dataPlan is blank, i need to fill the dataPlan <td> with yellow color and show the value of name and callerID in its individual cells..
I'm not sure how to include conditional check while iterating the loop and fill the cell with color if the value is blank/null. Any suggestions?
PS: Fill the cell with yellow color if the value in the cell is blank/null.
A quick CSS rule might solve your issue.
Demo: http://jsfiddle.net/U3pVM/34058/
CSS:
td:empty {
background: yellow;
}/*Make it more specific*/
Try this ng-style conditional statements. This will work
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$scope.myResults=[
{ name:'aravind',callerId:'1234',dataPlan:'new'},
{ name:'John',callerId:'2345',dataPlan:'another'},
{ name:'ravi',callerId:'3456',dataPlan:''}
];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<table ng-app="myApp" ng-controller="customersCtrl">
<tr ng-repeat="data in myResults">
<td style="text-align: center;">{{data.name}}</td>
<td style="text-align: center;">{{data.callerId}}</td>
<td ng-style="{'background-color': data.dataPlan ==='' ? 'yellow' : ''}">{{data.dataPlan}}</td>
</tr>
</table>
I have a HTML file that should display image for an item. The source of the image is stored as a link in a database and is marked by the placeholder <%IMAGE%> in the html file. There are some items that doesn't have any images. So, I want to hide the row instead of display a large empty cell.
//<%IMAGE%> = placeholder for image link
<tr style = "empty-cells:hide;">
<td style="padding-bottom:10px;">
<img src="<%IMAGE%>" style="width:100%"></img>
</td>
</tr>
Example of the placeholder <%IMAGE%>
<%IMAGE%> = "http://mywebsite.com/image/icon-jobs.png"
I have tried empty-cells:hide but it doesn't work.
JSFiddle.
Key Code:
function imgError(image) {
$(image).parent('div.column').css('display','none');
}
As you said that source image is stored in Database all you have to do is to check whether image source is fetched from database.
<?php if($image):?>
<tr style = "empty-cells:hide;">
<td style="padding-bottom:10px;">
<img src="<%IMAGE%>" style="width:100%"></img>
</td>
</tr>
<?php else:?>
<div>No images found</div>
<?php endif;?>
I have a table and I want certain tr to have the same css class as the tr before it.
in the example below the tr with class="?" should have the same class as the ones above.
is this possible?
<table>
<tr class="red">
<td></td>
</tr>
<tr class="?">
<td></td>
</tr>
<tr class="blue">
<td></td>
</tr>
<tr class="?">
<td></td>
</tr>
</table>
based on Karl idea.
Use Jquery
http://jsfiddle.net/forX/KBfzD/
$(function(){
$(".unknown").each(function()
{
$(this).removeClass("unknown");
$(this).addClass(
$(this)
.prev()
.attr("class")
);
});
});
I change the ? for unknown. I dont know how to use ? in css/css selector.
Try this:
HTML:
<table border="1" style="border-collapse:collapse;">
<tr><td>Data</td><td>Data</td></tr>
<tr><td>Data</td><td>Data</td></tr>
<tr><td>Data</td><td>Data</td></tr>
<tr><td>Data</td><td>Data</td></tr>
<tr><td>Data</td><td>Data</td></tr>
<tr><td>Data</td><td>Data</td></tr>
<tr><td>Data</td><td>Data</td></tr>
<tr><td>Data</td><td>Data</td></tr>
<tr><td>Data</td><td>Data</td></tr>
<tr><td>Data</td><td>Data</td></tr>
<tr><td>Data</td><td>Data</td></tr>
<tr><td>Data</td><td>Data</td></tr>
</table>
jQuery:
$(function(){
$("tr").addClass("red");
$("tr:nth-child(4n)").removeClass("red").addClass("blue")
.prev().removeClass("red").addClass("blue");
});
This will select every 4th child, remove the red CSS class, add the blue CSS class and the move to the previous element and do the same.
Note: Here is a jsFiddle that you can play around with.
Every html element is addressable with css, so in the stylesheet of your css,if you decalre like element{
set the desired property ;
}
In this case the element would be tr all the table rows will have the properties you have set in above general declaration ,and if you want to override the specific values to some of your table rows you can address them like using the class
I am not pretty sure about understanding this problem but:
in css: tr.classname + tr gives you the next tr from tr which has a classname. See this Fiddle.
You can also checkout css nth-child,even & odd selectors.
Based on what you have mentioned, this is the fiddle.
It just checks the previous element of class: ?
without javascript, you can play with element:li:nth-child(x) values
can be anything like n, n+1, 2n+1, etc... and you can give them different values.
I need to repeat an html snippet several times on a page but the problem is that the contained html elements have id defined that should be unique on page. So what is the proper way I could repeat this snippet without removing the id attribute from the elements ?
Could I use iframe as container for this snippet & let the duplicate ids exist on page?
You can use JavaScript to clone the snippet and at the same time change the IDs.
Example if your snippet is:
<div id="snippet">
<table id="list">
<tr id="0">
<td id="firstName0">John</td>
<td id="lastName0">Smith</td>
</tr>
<tr id="1">
<td id="firstName2">John</td>
<td id="lastName2">Doe</td>
</tr>
</table>
</div>
Using
$("#snippet").clone(false).find("*[id]").andSelf().each(function() { $(this).attr("id", $(this).attr("id") + "_1"); });
Will Produce
<div id="snippet_1">
<table id="list_1">
<tr id="0_1">
<td id="firstName0_1">John</td>
<td id="lastName0_1">Smith</td>
</tr>
<tr id="1_1">
<td id="firstName2_1">John</td>
<td id="lastName2_1">Doe</td>
</tr>
</table>
</div>
Which will solve the duplicate ID problem.