conditional check while iterating the loop - html

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>

Related

ng-repeat highlight all rows but also deselect, select

I have a basic html table where i need to have all the rows initially highlighted when the table is created. Also, if the user clicks the row it un highlights and clicked again highlights.
I have the click on a row, and it highlights. If you click again it un highlights.
I just need to initially highlight all rows possibly by ng-repeat. It also needs to release the highlighting when the row is clicked again and then highlight back. userData is just a line of text for each row
HTML
<table class="superusertable" cellpadding="5" cellspacing="0">
<tbody class="table-font">
<tr ng-init="" ng-repeat="source in userData"
ng-model="source.fromSourceID"
ng-class="{'sourcesSelected': source.sourcesSelected}"
ng-click="select(source)">
<td width="290px">
<div class="action-checkbox"; width="290px">{{source.fromSourceID}}
</div>
</td>
</tr>
</tbody>
</table>
angular
$scope.select = function(item) {
item.sourcesSelected ? item.sourcesSelected = false : item.sourcesSelected = true;
};
You can just add a function to the ng-init attribute on your tr. Just pass in your item and set it to true. Then like Aluan said in a comment, you can just make your ng-click function simpler by doing item.sourcesSelected = !item.sourcesSelected.
html
<table class="superusertable" cellpadding="5" cellspacing="0">
<tbody class="table-font">
<tr ng-init="init(source)"
ng-repeat="source in userData"
ng-model="source.fromSourceID"
ng-class="{'sourcesSelected': source.sourcesSelected}"
ng-click="select(source)">
<td width="290px">
<div class="action-checkbox"; width="290px">{{source.fromSourceID}}</div>
</td>
</tr>
</tbody>
</table>
angular
$scope.select = function(item) {
item.sourcesSelected = !item.sourcesSelected;
};
$scope.init = function(item) {
item.sourcesSelected = true;
}
On a side note, you can completely eliminate the ng-init and init function by setting item.sourcesSelected = true when you are retrieving your data.
There are too many errors i can observe.
ng-init="" not required
ternary operator is wrong you should do something following:
item.sourcesSelected = item.sourcesSelected ? false : true;

Change table background dynamically angularjs

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

Changing CSS of a table cell based on the value in another cell of same row in HTML

I am working on an HTML web page where I have a table with some data in it and I am trying to control CSS for an entire column in the table based on the values in another column and same row
For example, In the following screenshot I have my data
In the above picture, I have Volume, Price and Type. Now, I want to control the color of Price column based on the corresponding value in Type Column. Like for Price=10 I have Type as Sell, so I want to make the value 10 to red color and similarly if type is Buy then Price value should be in Yellow.
I am trying to do that using following script
<td data-bind="text: Volume"></td>
<td data-bind="text: (typeof Price() === 'number') ? Price().toFixed(2) : '',css:{cclientType:Type=='Sell'}"></td>
<td data-bind="text: Type"></td>
But, that doesn't seem to be working.
Provided, the data is coming from a Knockout View model which in turn is pulling from SQL Server.
Is there a better way I could achieve this?
You could add a ko.computed function to each of your data items to help you determine the css:
self.volume = ko.observable(volume);
self.price = ko.observable(price);
self.type = ko.observable(type);
self.priceCss = ko.computed(function() {
if (self.type() == 'buy') {
return 'buy-class';
} else if (self.type() == 'sell') {
return 'sell-class';
} else return '';
});
This can then be added to your markup:
<tr>
<td data-bind="text:volume"></td>
<td data-bind="text:price, css: priceCss"></td>
<td data-bind="text:type"></td>
</tr>
A plunker demonstrating this can be seen here
Dev.
Please, try this! Works.
<td data-bind="text: volume"></td>
<td data-bind="text: (typeof price() === 'number') ? price().toFixed(2) : '',css:{ cclientType: type() == 'sell'}"></td>
<td data-bind="text: type"></td>
I would set styles on the <tr> based on the contents of type column (or any other column) and handle everything you need in CSS.
Eg.
<tr class="sell">
<td>100</td>
<td>10.00</td>
<td>Sell</td>
</tr>
tr.sell td:nth-of-type(2) {
color: red;
}
If you don't like using the nth-of-type selector, you can set a class on the <td> and then your CSS selector would be:
tr.sell td.price {
color: red;
}

HTML Sum of Column Needed

I have the following html code for a table I am generating information from but I also need it to calculate the sum of the "TranAmt" column. Can someone help me with this?
<p>Hi Marly,</p>
<p>The following customer invoices were posted today:</p>
<table style="width: 1300px;" border="1" cellspacing="1.5" cellpadding="1.5" align="left">
<thead>
<tr style="background-color: #81BEF7;" align="center" valign="middle">
<td style="text-align: center;"><strong>Customer ID</strong></td>
<td style="text-align: center;"><strong>Customer Name</strong></td>
<td style="text-align: center;"><strong>Customer PO Number</strong></td>
<td style="text-align: center;"><strong>Invoice Number</strong></td>
<td style="text-align: center;"><strong>Invoice Date</strong></td>
<td style="text-align: center;"><strong>Post Date</strong></td>
<td style="text-align: center;"><strong>Invoice Amount</strong></td>
<td style="text-align: center;"><strong>Invoice Sales Tax Amount</strong></td>
<td style="text-align: center;"><strong>Create User</strong></td>
</tr>
</thead>
{BEGIN*REPEAT}
<tbody>
<tr>
<td>{CustID}</td>
<td>{CustName}</td>
<td>{CustPONo}</td>
<td>{TranID}</td>
<td>{TranDate}</td>
<td>{PostDate}</td>
<td>{TranAmt}</td>
<td>{STaxAmt}</td>
<td>{CreateUserID}</td>
</tr>
{END*REPEAT}
</tbody>
</table>
If you are using JQuery you can use the n-th child selector to add all the items of a column within a table.
http://api.jquery.com/nth-child-selector/
Here's an example
var rows = $("#table_id tr:gt(0)");
rows.children("td:nth-child(7)").each(function() {
the_sum += parseInt($(this).text());
});
This function will help you for getting the total of any column whose column header name you supplied to it. But I want to suggest one thing instead of tag names thead and tbody plz use some id, as it will create problem if you have multiple tables in the same page.
function someFunction() {
var invoiceTotalAmount = getTotal(("invoice amount").toUpperCase());
}
function getTotal(myString) {
var CellListing = $("thead > tr > td");
var selIndex=-1;
var sum = 0;
CellListing.each(function(index){
if($(this).children("strong").html().toUpperCase()===myString)
selndex= index+1;
});
var rows = $("tbody >tr");
rows.children("td:nth-child("+selIndex+")").each(function() {
sum += parseInt($(this).text());
});
return sum;
}
running example html sum of column needed
I don't know how to define variable in {FinalTranAmt} so i'm explain you
Tack a variable with initially 0 as value
For every repeat add {TranAmt} in {FinalTranAmt} & finally print it.
I don't know my structure is right or not.
{FinalTranAmt} = 0;
{BEGIN*REPEAT}
<tbody>
<tr>
<td>{CustID}</td>
<td>{CustName}</td>
<td>{CustPONo}</td>
<td>{TranID}</td>
<td>{TranDate}</td>
<td>{PostDate}</td>
<td>{TranAmt}</td>
<td>{STaxAmt}</td>
<td>{CreateUserID}</td>
</tr>
{FinalTranAmt} += {TranAmt} ;
{END*REPEAT}

trouble adding value of <td> to text box onclick

I am trying to create a javascript function within my html document that essentially takes the value of each <td> and places it in the textbox. Any help is very appreciated.
<html>
<head>
<script type="text/javascript">
function typeThis(){
document.getElementById('box_1').value = document.getElementById('typewriter');
}
</script>
<style type="text/css">
td{
border:1px solid black;
padding:10px 10px 10px 10px;
font-family:"Helvetica Neue";
font-size:20px;
}
table{
margin-top:50px;
}
</style>
</head>
<body>
<table id = "typewriter">
<td value="k" onclick="typeThis();">k</td>
<td value="c" onclick="typeThis();">c</td>
<td value="y" onclick="typeThis();">y</td>
<td value="s" onclick="typeThis();">s</td>
<td value="p" onclick="typeThis();">p</td>
<input type="text" id="box_1">
</table>
</body>
</html>
value is a custom property for a td,
so you can access it using this method
function typeThis(){
document.getElementById('box_1').value = this.getAttribute("value");
}
Side Note:
this is how your table should look like
<table id = "typewriter">
<tr>
<td value="k" onclick="typeThis();">k</td>
<td value="c" onclick="typeThis();">c</td>
<td value="y" onclick="typeThis();">y</td>
<td value="s" onclick="typeThis();">s</td>
<td value="p" onclick="typeThis();">p</td>
</tr>
</table>
<input type="text" id="box_1">
Example 2:
function typeThis(letter){
document.getElementById('box_1').value = letter;
}
<table id = "typewriter">
<tr>
<td value="k" onclick="typeThis('k');">k</td>
<td value="c" onclick="typeThis('c');">c</td>
<td value="y" onclick="typeThis('y');">y</td>
<td value="s" onclick="typeThis('s');">s</td>
<td value="p" onclick="typeThis('p');">p</td>
</tr>
</table>
How about
var box = document.getElementById("box_1");
var tds = document.getElementsByTagName("td");
for (var i = 0; i < tds.length; i++) {
var valToAdd = tds[i].textContent ? tds[i].textContent :
(tds[i].innerText ? tds[i].innerText : tds[i].innerHTML);
box.value = box.value + valToAdd;
}
to avoid using innerHTML it checks for the newer textContent and uses it if present. If not, it falls back to innerText and, as a last resort, innerHTML.
Also, if you want to add custom attributes to your td tags, you may want to opt for the more standard data-value="k" format. And check your code for a closing table tag
The main problem is on this line:
document.getElementById('box_1').value = document.getElementById('typewriter');
You are assigning the value of the 'box_1' input equal to the table element itself, not to the value from the particular td that was clicked.
If you change your function to accept a parameter that is the clicked td you can then access the value property:
function typeThis(el){
document.getElementById('box_1').value = el.getAttribute('value');
}
// then change each TD to look like this:
<td value="k" onclick="typeThis(this);">k</td>
However, you can simplify your code somewhat if you use a single click handler on the table instead of putting one on every individual td. When a td is clicked that event "bubbles up" to the containing tr and then to the table, so you handle it there and check the event object to see which td was the actual target:
function typeThis(e) {
// allow for the way IE handles the event object
// compared to other browsers
e = e || window.event;
var el = e.srcElement || e.target;
if (el.tagName.toLowerCase() === "td")
document.getElementById('box_1').value = el.getAttribute('value');
}
document.getElementById('typewriter').onclick = typeThis;
Regarding your table html, some browsers may guess what you meant and display it OK, but you should have a closing </table> tag and your tds should be in a tr. Note that I've removed all of the onclick assignments because with the code above that assigns one for the table you don't need them:
<table id="typewriter">
<tr>
<td value="k">k</td>
<td value="c">c</td>
<td value="y">y</td>
<td value="s">s</td>
<td value="p">p</td>
</tr>
<table>
Note that at the moment each td's value is exactly the same as its innerHTML, so you could just remove all of the value properties from the markup and user .innerHTML in your function instead of getting the value of value:
document.getElementById('box_1').value = el.innerHTML;