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, 12:54 AM EDT
Is this code can use for multiple record
Gowtham KTOP 100Posted Oct 30, 2015, 8:37 AM EDT
Good One, Thanks for sharing
Nilesh JadavPosted Oct 30, 2015, 7:55 AM EDT
Good one sir!
Dinesh BeniwalTOP 10Posted Oct 30, 2015, 12:51 AM EDT
Welcome to C# Corner Ethan.
Humayun Kabir MamunPosted Oct 29, 2015, 10:25 PM EDT
Nice...
Mukesh KumarPosted Oct 29, 2015, 10:13 PM EDT
Good Job
Harshad PansuriyaPosted Oct 29, 2015, 9:37 PM EDT
Nice one
Rohit TarparaPosted Oct 29, 2015, 9:12 PM EDT
Excellent and minimum code lines for calculation result using jQuery, Very Nice...
Sibeesh VenuTOP 100Posted Oct 29, 2015, 8:41 PM EDT
Nice Share