While developing UI in MVC in medium or large scale applications, we often required to make some auto calculations specially while dealing with sales and statistics. So to quickly develop such auto calculation, I will explain how to calculate all table cell value of 1 row and set the total in last cell using jQuery and JavaScript.
I am using MVC platform. So, first create one MVC application and add one controller and their views. We are calculating values using JavaScript and jQuery so there is no coding part mentioned for controller. I am calculating student marks and full percentage and percentage per subject.
Now, design the MVC view index.cshtml page as per the following screenshot:
- @
- {
- ViewBag.Title = "Index";
- } < script src = "~/Scripts/Helper.js" > < /script> @
- section featured { < section class = "featured" > @using(Html.BeginForm()) { < div class = "content-wrapper" > < table class = "table table-striped table-bordered table-condensed"
- id = "tblStudent"
- style = "width:100%;" > < thead > < tr style = "border-bottom: solid;border-top: solid" > < th > Name < /th> < th > Total Marks < /th> < th > Maths < /th> < th > Science < /th> < th > Chemistry < /th> < th > Microbiology < /th> < th > Physics < /th> < th > English < /th> < th > Total < /th> < /tr> < /thead> < tr class = "trRow1" > < td rowspan = "2"
- style = "vertical-align:middle;" > < b > ALAX < /b></td > < td rowspan = "2"
- style = "vertical-align:middle;" > < input type = "text"
- disabled = "disabled"
- id = "txtTotalMark"
- class = "tdTotalMark"
- style = "width:80px; height:50px;"
- value = "600" / > < /td> < td > < input type = "text"
- class = "jsMark"
- id = "txtMaths"
- style = "width:70px; height:15px;" / > < /td> < td > < input type = "text"
- class = "jsMark"
- id = "txtScience"
- style = "width:70px; height:15px;" / > < /td> < td > < input type = "text"
- class = "jsMark"
- id = "txtChemistry"
- style = "width:70px; height:15px;" / > < /td> < td > < input type = "text"
- class = "jsMark"
- id = "txtMicrobiology"
- style = "width:70px; height:15px;" / > < /td> < td > < input type = "text"
- class = "jsMark"
- id = "txtPhysics"
- style = "width:70px; height:15px;" / > < /td> < td > < input type = "text"
- class = "jsMark"
- id = "txtEnglish"
- style = "width:70px; height:15px;" / > < /td> < td > < input type = "text"
- class = "jsMarkTot"
- id = "txtTotal"
- style = "width:70px; height:15px;"
- disabled = "disabled" / > < /td> < /tr> < tr class = "trRow2" > < td > < input type = "text"
- class = "jsCal"
- id = "txtMathsValue"
- style = "width:70px; height:15px;"
- disabled = "disabled" / > < /td> < td > < input type = "text"
- class = "jsCal"
- id = "txtScienceValue"
- style = "width:70px; height:15px;"
- disabled = "disabled" / > < /td> < td > < input type = "text"
- class = "jsCal"
- id = "txtChemistryValue"
- style = "width:70px; height:15px;"
- disabled = "disabled" / > < /td> < td > < input type = "text"
- class = "jsCal"
- id = "txtMicrobiologyValue"
- style = "width:70px; height:15px;"
- disabled = "disabled" / > < /td> < td > < input type = "text"
- class = "jsCal"
- id = "txtPhysicsValue"
- style = "width:70px; height:15px;"
- disabled = "disabled" / > < /td> < td > < input type = "text"
- class = "jsCal"
- id = "txtEnglishValue"
- style = "width:70px; height:15px;"
- disabled = "disabled" / > < /td> < td > < input type = "text"
- class = "jsCalTot"
- id = "txtTotalValue"
- style = "width:70px; height:15px;"
- disabled = "disabled" / > < /td> < /tr> < /table> < /div>
- } < /section>
- }
Now, the following is the jQuery and JavaScript code for cell calculation.
- <script type="text/javascript">
- $(function () {
- $(".jsMark").keyup(calculateResult);
- $(".jsCal").keyup(calculateResult);
- $(".jsMark").change(calculateResult);
- $(".jsCal").change(calculateResult);
- $(".jsMark,.jsCal").keydown(Helper.isNumberKey);
- });
- function calculateResult() {
- var ta;
- var tr = $(this).closest('tr');
- $(tr).find('td').each(function () {
- $(this).find('.tdTotalMark').each(function () {
- ta = $(this).val();
- });
- });
- var cost = this.value;
- var tot = (parseFloat(cost) / parseFloat(ta)) * 100;
- if (cost != "") {
- $('#' + this.id + 'Value').val(tot.toFixed(2));
- }
- $('#tblStudent tr.trRow1').each(function () {
- var sum = 0;
- $(this).find('td').each(function () {
- $(this).find('.jsMark').each(function () {
- var combat = $(this).val();
- if (!isNaN(combat) && combat.length !== 0) {
- sum = parseFloat(sum) + parseFloat(combat);
- }
- });
- });
- $(this).find('td:last').find('input.jsMarkTot').val(sum.toFixed(2));
- });
- $('#tblStudent tr.trRow2').each(function () {
- var sum2 = 0;
- $(this).find('td').each(function () {
- $(this).find('.jsCal').each(function () {
- var combat2 = $(this).val();
- if (!isNaN(combat2) && combat2.length !== 0) {
- sum2 = parseFloat(sum2) + parseFloat(combat2);
- }
- });
- });
- $(this).find('td:last').find('input.jsCalTot').val(sum2.toFixed(2));
- });
- }
- </script>
The output of index.cshtml page looks like the following:

I created this document just for example purpose, but you can use this code as per your requirements.

Narayan BhosalePosted Jun 26, 2019, 4:54 AM
Is this code can use for multiple record
Gowtham KPosted Oct 30, 2015, 12:37 PM
Good One, Thanks for sharing
Nilesh JadavPosted Oct 30, 2015, 11:55 AM
Good one sir!
Dinesh BeniwalPosted Oct 30, 2015, 4:51 AM
Welcome to C# Corner Ethan.
Humayun Kabir MamunPosted Oct 30, 2015, 2:25 AM
Nice...
Mukesh KumarPosted Oct 30, 2015, 2:13 AM
Good Job
Harshad PansuriyaPosted Oct 30, 2015, 1:37 AM
Nice one
Rohit TarparaPosted Oct 30, 2015, 1:12 AM
Excellent and minimum code lines for calculation result using jQuery, Very Nice...
Sibeesh VenuPosted Oct 30, 2015, 12:41 AM
Nice Share