Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
124
rated 0 times [  126] [ 2]  / answers: 1 / hits: 15546  / 7 Years ago, fri, january 20, 2017, 12:00:00

I am trying to sum the values of the Total Work Assigned and Not Called columns.



Here is my table HTML



<table border=1 id=category>
<tr>
<th>User Name</th>
<th>User Belongs</th>
<th>Total Work Assigned</th>
<th>Not Called</th>
</tr>
<tr>
<td>vidyaranyapura</td>
<td>Category</td>
<td>172</td>
<td>156</td>
</tr>
<tr>
<td>sahasra</td>
<td>Company</td>
<td>500</td>
<td>350</td>
</tr>
<tr>
<td>global</td>
<td>Not Assigned</td>
<td>0</td>
<td>0</td>
</tr>
</table>


here is snippet





$('#category tr td').text(function(i,el){
console.log(parseInt(el,10));
});

<script src=https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js></script>

<table border=1 id=category>
<tr>
<th>User Name</th>
<th>User Belongs</th>
<th>Total Work Assigned</th>
<th>Not Called</th>
</tr>
<tr>
<td>vidyaranyapura</td>
<td>Category</td>
<td>172</td>
<td>156</td>
</tr>
<tr>
<td>sahasra</td>
<td>Company</td>
<td>500</td>
<td>350</td>
</tr>
<tr>
<td>global</td>
<td>Not Assigned</td>
<td>0</td>
<td>0</td>
</tr>
</table>
<h3>MY EXPECTED OUTPUT</h3>
<table border=1 id=same_id_wont_work_changed_for_demo_only>
<tr>
<th>User Name</th>
<th>User Belongs</th>
<th>Total Work Assigned</th>
<th>Not Called</th>
</tr>
<tr>
<td>vidyaranyapura</td>
<td>Category</td>
<td>172</td>
<td>156</td>
</tr>
<tr>
<td>sahasra</td>
<td>Company</td>
<td>500</td>
<td>350</td>
</tr>
<tr>
<td>global</td>
<td>Not Assigned</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td></td>
<td></td>
<td>672</td>
<td>506</td>
</tr>
</table>





jsfiddle for my expected output: https://jsfiddle.net/2g29c8uv/16/


More From » php

 Answers
1

I guess what you want to do is something like this:



DEMO: https://jsfiddle.net/zxooa1j4/1/



var sum1 = 0;
var sum2 = 0;
$(#category tr).not(':first').not(':last').each(function() {
sum1 += getnum($(this).find(td:eq(2)).text());
sum2 += getnum($(this).find(td:eq(3)).text());
function getnum(t){
if(isNumeric(t)){
return parseInt(t,10);
}
return 0;
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
}
});
$(#sum1).text(sum1);
$(#sum2).text(sum2);


isNumeric() function is quoted from this answer:



Is there any function like IsNumeric in javascript to validate numbers



HTML:



<table border=1 id=category>
<tr>
<th>User Name</th>
<th>User Belongs</th>
<th>Total Work Assigned</th>
<th>Not Called</th>
</tr>
<tr>
<td>vidyaranyapura</td>
<td>Category</td>
<td>172</td>
<td>156</td>
</tr>
<tr>
<td>sahasra</td>
<td>Company</td>
<td>500</td>
<td>350</td>
</tr>
<tr>
<td>global</td>
<td>Not Assigned</td>
<td>0</td>
<td>0</td>
</tr>
<tr>
<td></td>
<td></td>
<td id=sum1></td>
<td id=sum2></td>
</tr>
</table>


Hope this helps.


[#59277] Wednesday, January 18, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
adriannemyiag

Total Points: 504
Total Questions: 105
Total Answers: 99

Location: Ecuador
Member since Thu, Apr 22, 2021
3 Years ago
;