Auto Calculate Cell Value In HTML Table With ASP.NET MVC

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:

  1. @  
  2. {  
  3.     ViewBag.Title = "Index";  
  4. } < script src = "~/Scripts/Helper.js" > < /script>  @  
  5. section featured { < section class = "featured" > @using(Html.BeginForm()) { < div class = "content-wrapper" > < table class = "table table-striped table-bordered table-condensed"  
  6.         id = "tblStudent"  
  7.         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"  
  8.         style = "vertical-align:middle;" > < b > ALAX < /b></td > < td rowspan = "2"  
  9.         style = "vertical-align:middle;" > < input type = "text"  
  10.         disabled = "disabled"  
  11.         id = "txtTotalMark"  
  12.         class = "tdTotalMark"  
  13.         style = "width:80px; height:50px;"  
  14.         value = "600" / > < /td>   < td > < input type = "text"  
  15.         class = "jsMark"  
  16.         id = "txtMaths"  
  17.         style = "width:70px; height:15px;" / > < /td>   < td > < input type = "text"  
  18.         class = "jsMark"  
  19.         id = "txtScience"  
  20.         style = "width:70px; height:15px;" / > < /td>   < td > < input type = "text"  
  21.         class = "jsMark"  
  22.         id = "txtChemistry"  
  23.         style = "width:70px; height:15px;" / > < /td>   < td > < input type = "text"  
  24.         class = "jsMark"  
  25.         id = "txtMicrobiology"  
  26.         style = "width:70px; height:15px;" / > < /td>   < td > < input type = "text"  
  27.         class = "jsMark"  
  28.         id = "txtPhysics"  
  29.         style = "width:70px; height:15px;" / > < /td>    < td > < input type = "text"  
  30.         class = "jsMark"  
  31.         id = "txtEnglish"  
  32.         style = "width:70px; height:15px;" / > < /td>   < td > < input type = "text"  
  33.         class = "jsMarkTot"  
  34.         id = "txtTotal"  
  35.         style = "width:70px; height:15px;"  
  36.         disabled = "disabled" / > < /td>   < /tr>   < tr class = "trRow2" > < td > < input type = "text"  
  37.         class = "jsCal"  
  38.         id = "txtMathsValue"  
  39.         style = "width:70px; height:15px;"  
  40.         disabled = "disabled" / > < /td>   < td > < input type = "text"  
  41.         class = "jsCal"  
  42.         id = "txtScienceValue"  
  43.         style = "width:70px; height:15px;"  
  44.         disabled = "disabled" / > < /td>   < td > < input type = "text"  
  45.         class = "jsCal"  
  46.         id = "txtChemistryValue"  
  47.         style = "width:70px; height:15px;"  
  48.         disabled = "disabled" / > < /td>   < td > < input type = "text"  
  49.         class = "jsCal"  
  50.         id = "txtMicrobiologyValue"  
  51.         style = "width:70px; height:15px;"  
  52.         disabled = "disabled" / > < /td>   < td > < input type = "text"  
  53.         class = "jsCal"  
  54.         id = "txtPhysicsValue"  
  55.         style = "width:70px; height:15px;"  
  56.         disabled = "disabled" / > < /td>   < td > < input type = "text"  
  57.         class = "jsCal"  
  58.         id = "txtEnglishValue"  
  59.         style = "width:70px; height:15px;"  
  60.         disabled = "disabled" / > < /td>   < td > < input type = "text"  
  61.         class = "jsCalTot"  
  62.         id = "txtTotalValue"  
  63.         style = "width:70px; height:15px;"  
  64.         disabled = "disabled" / > < /td>   < /tr>   < /table>   < /div>    
  65.     } < /section>    
  66. }  
Now, you can see that I have added reference of helper.js file on the top of the page. This js file is used just to validate if the value in textbox is valid number or not. You have to reference jquery.min.js to your code if not available.

Now, the following is the jQuery and JavaScript code for cell calculation.
  1. <script type="text/javascript">    
  2.     $(function () {    
  3.         $(".jsMark").keyup(calculateResult);    
  4.         $(".jsCal").keyup(calculateResult);    
  5.         $(".jsMark").change(calculateResult);    
  6.         $(".jsCal").change(calculateResult);    
  7.         $(".jsMark,.jsCal").keydown(Helper.isNumberKey);    
  8.     });    
  9.     
  10.     function calculateResult() {    
  11.         var ta;    
  12.         var tr = $(this).closest('tr');    
  13.         $(tr).find('td').each(function () {    
  14.             $(this).find('.tdTotalMark').each(function () {    
  15.                 ta = $(this).val();    
  16.             });    
  17.         });    
  18.     
  19.         var cost = this.value;    
  20.         var tot = (parseFloat(cost) / parseFloat(ta)) * 100;    
  21.     
  22.         if (cost != "") {    
  23.             $('#' + this.id + 'Value').val(tot.toFixed(2));    
  24.         }    
  25.     
  26.         $('#tblStudent tr.trRow1').each(function () {    
  27.             var sum = 0;    
  28.             $(this).find('td').each(function () {    
  29.                 $(this).find('.jsMark').each(function () {    
  30.                     var combat = $(this).val();    
  31.                     if (!isNaN(combat) && combat.length !== 0) {    
  32.                         sum = parseFloat(sum) + parseFloat(combat);    
  33.                     }    
  34.                 });    
  35.             });    
  36.             $(this).find('td:last').find('input.jsMarkTot').val(sum.toFixed(2));    
  37.         });    
  38.     
  39.         $('#tblStudent tr.trRow2').each(function () {    
  40.             var sum2 = 0;    
  41.             $(this).find('td').each(function () {    
  42.                 $(this).find('.jsCal').each(function () {    
  43.                     var combat2 = $(this).val();    
  44.                     if (!isNaN(combat2) && combat2.length !== 0) {    
  45.                         sum2 = parseFloat(sum2) + parseFloat(combat2);    
  46.                     }    
  47.                 });    
  48.             });    
  49.             $(this).find('td:last').find('input.jsCalTot').val(sum2.toFixed(2));    
  50.         });    
  51.     }    
  52. </script>  
Finally, run the application and enter random values in text boxes, you can see that the total of cell values and calculation automatically set on last cell simultaneously when you change any value. The above code fires on change and keyup event of textboxes.

The output of index.cshtml page looks like the following:

output

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

 

Up Next
    Ebook Download
    View all
    Learn
    View all