HTML Sum of Column Needed - html

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}

Related

how to display data for a specific month in a table

is it possible to display the data on which column month of the table?
below is the sample dashboard to achieve
and this is my DB
currently this is my dashboard. I want to align the data base on the period in the DB.
what should be my tboby look like to achieve the desired dashboard
this is my index html
<div class="w3-container w3-padding-64 w3-theme-l5">
<div class="w3-padding-16"><span class="w3-xlarge w3-border-purple w3-bottombar">COUNT PER BUSINESS UNIT</span></div>
<div>
<table id="businessUnit-tbl" class="table table-bordered display nowrap">
<thead>
<tr style="/*color:white;*/ align-content:center">
<td>MONTH</td>
</tr>
<tr #*style="color:white"*#>
<td>Business Unit</td>
<td id="jan">January</td>
<td id="feb">February</td>
<td id="mar">March</td>
<td id="apr">April</td>
<td id="may">May</td>
<td id="june">June</td>
<td id="july">July</td>
<td id="aug">August</td>
<td id="sept">September</td>
<td id="oct">October</td>
<td id="nov">November</td>
<td id="dec">December</td>
<td id="total">Total</td>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
According to my understanding of the topic, I suggest leaving tbody blank and trying to fill it with javascript
// append column to the HTML table
function appendColumn() {
var tbl = document.getElementById('my-table'), // table reference
i;
// open loop for each row and append cell
for (i = 0; i < tbl.rows.length; i++) {
createCell(tbl.rows[i].insertCell(tbl.rows[i].cells.length), i, 'col');
}
}

How to calculate difference of two column into third column at runtime

I have created a web application which fetch data from RTC tool.
So In that web application I have to calculate difference at runtime(for third column) basis on two column values.
First column value will be taken from RTC tool programmatically and for second column user will enter value in text boxes and for third column it will calculate difference automatically.
Let me know if we can calculate difference for this third column automatically and how?
Thanks
Hard to tell without rendered HTML. If your columns are in a table, you can do
const makeNum = str => isNaN(str) || str.trim() === ""? 0:+str;
document.getElementById("table").addEventListener("input", function(e) {
const tgt = e.target;
if (tgt.classList.contains("userInput")) { // <input class="userInput"
const parent = tgt.closest("tr");
const rtc = makeNum(parent.querySelector(".rtc").textContent); // <td class="rtc>value</td>
const val = makeNum(tgt.value);
parent.querySelector(".diff").textContent = rtc - val; // <td class="diff"></td>
}
})
<table>
<thead></thead>
<tbody id="table">
<tr>
<td class="rtc">1000</td>
<td><input class="userInput"></td>
<td class="diff"></td>
</tr>
<tr>
<td class="rtc">2000</td>
<td><input class="userInput"></td>
<td class="diff"></td>
</tr>
<tr>
<td class="rtc">3000</td>
<td><input class="userInput"></td>
<td class="diff"></td>
</tr>
<tr>
<td class="rtc">4000</td>
<td><input class="userInput"></td>
<td class="diff"></td>
</tr>
<tr>
<td class="rtc">5000</td>
<td><input class="userInput"></td>
<td class="diff"></td>
</tr>
</table>
If you are using jQuery in you project they you can also try this:
Check the working here
JS:
$('.user-input').on('input', function() {
var static_val = $(this).parent().prev().text();
var user_val = $(this).val();
var diff = parseInt(static_val) - parseInt(user_val);
if(!isNaN(diff)){
$(this).parent().next().text(diff);
}else{
$(this).parent().next().text('');
}
});

Count <tr> HTML tag grouped by their #class attribute name in XPath

I need a XPath expression that count all the <tr> rows that have a starting class attribute string: room_loop_counter grouped by their attribute name itself.
I have the following sample HTML code to extract data from:
<tbody id="container" >
<tr class="room_loop_counter1 maintr">
<td class="legibility " rowspan="6"></td>
<td colspan="4" style="padding:0;"></td>
</tr>
<tr class="room_loop_counter1">
<td ></td>
<td class=""></td>
</tr>
<tr class="room_loop_counter1"></tr>
<tr class="room_loop_counter2 maintr divider"></tr>
<tr class="room_loop_counter2"></tr>
<tr class="room_loop_counter3 maintr divider"></tr>
<tr class="room_loop_counter3"></tr>
<tr class="room_loop_counter3"></tr>
<tr class="room_loop_counter3"></tr>
<tr class="room_loop_counter3"></tr>
</tbody>
Given the above HTML I would want to get as result : 2,1,4. The count is the number of elements minus one, since I want to discard from the count the first <tr>(the one with the maintr) that is the header...
Between <tr> elements there could be other <tr> elements so their are not strictly one after the other, so we can't rely on following or preceding sibling logic.
I've tried with the following XPath expression :
count(//table[#id="maxotel_rooms"]/tbody/tr[#class=distinct-values(//table[#id="maxotel_rooms"]/tbody/tr[starts-with(#class, "room_loop_counter") and not(contains(#class, "maintr"))]/#class)]/#class])
but it doesn't work on chrome(evaluating it with $x('') on the console window) since it doesn't recognize the distinct-values function.
Could you suggest a possible solution? What is the best approach ?
Check this XPath for unique tr with class starts with some data and not followed by some other class name.
//tbody/tr[starts-with(#class, "room_loop_counter") and not(contains(#class, "maintr"))]/following::tr[not(./#class=following::tr/#class) and not(contains(#class, "maintr"))]
Javascript:
var path = "//body/div";
var uniquePathCount = window.document.evaluate('count(' + path + ')', window.document, null, 0, null);
console.log( uniquePathCount );
console.log( uniquePathCount.numberValue );
Ouput:
<tr class="room_loop_counter1"/>
<tr class="room_loop_counter2"/>
<tr class="room_loop_counter3"/>

how to add class in table in python using Dominate library

I have created Table using Dominate Library but Now I want to change my table class. can someone help me to do that ?
doc1 = dominate.document(title='Dominate your HTML')
with doc1:
with div():
attr(cls='body')
h1('Survey Report : Survey Report')
oc = dominate.document(title="whatever")
with doc1:
tags.style(".calendar_table{width:880px;}")
tags.style("body{font-family:Helvetica}")
tags.style("h1{font-size:x-large}")
tags.style("h2{font-size:large}")
tags.style("table{border-collapse:collapse}")
tags.style("th{font-size:small;border:1px solid gray;padding:4px;background-color:#DDD}")
tags.style("td{font-size:small;text-align:center;border:1px solid gray;padding:4px}")
with tags.table():
with tags.thead():
tags.th("Nominee", style = "color:#ffffff;background-color:#6A75F2")
tags.th("counts", style = "color:#ffffff;background-color:#6A75F2")
with tags.tbody():
for i in range(0,len(nom)):
with tags.tr(): #Row 1
tags.td(nom[i], style = "font-size:small;text-align:center;padding:4px")
if int(count_nom[i]) > 1:
tags.td(count_nom[i], style = "font-size:small;text-align:center;padding:4px;background-color:#F4D8D2")
else:
tags.td(count_nom[i], style = "font-size:small;text-align:center;padding:4px")
with tags.tr(): #Row 1
tags.td(b("Grand Total"), style = "font-size:small;text-align:center;padding:4px")
tags.td(b(sum(count_nom)), style = "font-size:small;text-align:center;padding:4px")
with open('/root/survey/'+'survey'+'.html', 'w') as f:
f.write(doc1.render())
with this I am able to create Table in HTML
<div class="body">
<h1>Survey Report</h1>
</div>
<style>.calendar_table{width:880px;}</style>
<style>body{font-family:Helvetica}</style>
<style>h1{font-size:x-large}</style>
<style>h2{font-size:large}</style>
<style>table{border-collapse:collapse}</style>
<style>th{font-size:small;border:1px solid gray;padding:4px;background-color:#DDD}</style>
<style>td{font-size:small;text-align:center;border:1px solid gray;padding:4px}</style>
<table>
<thead>
<th style="color:#ffffff;background-color:#6A75F2">Nominee</th>
<th style="color:#ffffff;background-color:#6A75F2">counts</th>
</thead>
<tbody>
<tr>
<td style="font-size:small;text-align:center;padding:4px">Deepesh Ahuja</td>
<td style="font-size:small;text-align:center;padding:4px">1</td>
</tr>
<tr>
<td style="font-size:small;text-align:center;padding:4px">Sabyasachi Mallick</td>
<td style="font-size:small;text-align:center;padding:4px">1</td>
</tr>
<tr>
<td style="font-size:small;text-align:center;padding:4px">Raju Singh</td>
<td style="font-size:small;text-align:center;padding:4px">1</td>
</tr>
<tr>
<td style="font-size:small;text-align:center;padding:4px">Abarna Ravi</td>
<td style="font-size:small;text-align:center;padding:4px;background-color:#F4D8D2">2</td>
</tr>
<tr>
<td style="font-size:small;text-align:center;padding:4px">
<b>Grand Total</b>
</td>
<td style="font-size:small;text-align:center;padding:4px">
<b>5</b>
</td>
</tr>
</tbody>
</table><br><br><br>
Now How I will set table class in python code like
<table class='calender_tabe'>
Can someone help me to set class of table and other tag using python dominate library?
Using the example syntax from github's documentation
from dominate.tags import *
testTable = table(border = 1)
print testTable
which will return:
<table border="1"></table>
with the print statement. However since you can't use the word "class" to refer to the html attribute (class being a python-reserved word) you have to go about it indirectly:
testTable.set_attribute('class','my_class_name')
Adding the above to the original instance of testTable will result in:
<table border="1" class="my_class_name"></table>

conditional check while iterating the loop

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>