Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
I am unable to get the required result using the jQuery and I seem cannot figure out what went wrong which I request anyone to please look into the problem and advise/suggest the same. I am open to other suggestions as well.

HTML
<div class="w3-container w3-padding-4x w3-indigo">
		<h2>
  		Table Testing
		</h2>
	</div>
	
	<div class="w3-container w3-section w3-pale-green">
		<table class="w3-table-all" id="sum_table" width="300" border="1">
			<tr class="titlerow">
				<th>SN</th>
				<th>DESCRIPTION</th>
				<TH>UNIT</TH>
				<TH>REQD QTY</TH>
				<TH>AVAIL. QTY</TH>
				<TH>RATE</TH>
				<TH>AMOUNT</TH>
				<TH>REMARKS IF ANY</TH>
			</tr>

			<tr>
				<td>1</td>
				<td>Steel</td>
				<td>Ton</td>
				<td>200</td>
				<td><input id="a.qty" type="number" oninput="calculate()" /></td>
				<td><input id="rate" type="number" oninput="calculate()" /></td>
				<td class="rowDataSd"><input id="amt1" type="number" name="amount" /></td>
				<td><input type="text" name=""></td>
			</tr>

			<tr>
				<td>2</td>
				<td>Cement</td>
				<td>Bags</td>
				<td>300</td>
				<td><input id="qty2" type="number" oninput="calculate1()" /></td>
				<td><input id="rate2" type="number" oninput="calculate1()" /></td>
				<td class="rowDataSd"><input id="amt2" type="number" name="amount" /></td>
				<td><input type="text" name=""></td>
			</tr>
			<tr class="totalColumn">
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td></td>
				<td class="totalCol"><input type="button" oninput="Calculate2()" name="Calculate"/> TOTAL:</td>
				<td><input type="text" name=""></td>
			</tr>
			
		</table>


JavaScript
<script>
function calculate(){
				var x1 = document.getElementById('a.qty').value;
				var x2 = document.getElementById('rate').value;
				var x3 = document.getElementById('amt1').value;
				var x4 = x1 * x2;
				amt1.value = x4;
			}

			function calculate1() {
				var y1 = document.getElementById('qty2').value;
				var y2 = document.getElementById('rate2').value;
				var y3 = document.getElementById('amt2').value;
				var y4 = y1 * y2;
				amt2.value = y4;

			}

			    var totals = [0];
                    
				$(document).ready(function() {

  				var $dataRows = $("#sum_table tr:not('.totalColumn, .titlerow')");

 				 $dataRows.each(function() {
    			$(this).find('.rowDataSd input').each(function(i) {
     				totals[i] += parseInt($(this).val()) || 0
    				});
  				});
 				$("#sum_table td.totalCol").each(function(i) {
    			$(this).value("total:" + totals[i]);
 				 });

			});
</script>


The outcome is available through this link for your easy reference- Image - TinyPic - Free Image Hosting, Photo Sharing &amp; Video Hosting[^]

What I have tried:

I was able to derive the multiplication of each row by using the javascript function in the "Amount" column.
However, I am unable to derive the total sum of "Amount" Column using jQuery function as shown above.
Posted
Updated 30-Jun-16 22:42pm

1 solution

try this
JavaScript
<script>
        function calculate() {
            var x1 = document.getElementById('a.qty').value;
            var x2 = document.getElementById('rate').value;
            var x3 = document.getElementById('amt1').value;
            var x4 = x1 * x2;
            amt1.value = x4;
            dototal();
        }

        function calculate1() {
            var y1 = document.getElementById('qty2').value;
            var y2 = document.getElementById('rate2').value;
            var y3 = document.getElementById('amt2').value;
            var y4 = y1 * y2;
            amt2.value = y4;
            dototal();

        }

       

        $(document).ready(function () {

            dototal();
        });

        function dototal()
        {
            var totals = 0;
            var $dataRows = $("#sum_table tr:not('.totalColumn, .titlerow')");

            $dataRows.each(function () {
                $(this).find('.rowDataSd input').each(function (i) {
                    totals += parseInt($(this).val()) || 0
                });
            });
            $("#sum_table td.totalCol").each(function (i) {
                $(this).text("total:" + totals);
            });

        }
    </script>
 
Share this answer
 
Comments
Member 12604078 1-Jul-16 4:50am    
Thanks Karthik. Point noted. Results came out as expected.
Karthik_Mahalingam 1-Jul-16 4:52am    
cool.
Member 12604078 1-Jul-16 6:06am    
@Karthik Bangalore.
Hi again karthik, I have few more doubts which I hope and request to a little (valuable) time on developing the code (in jQuery) which will be much appreciated. Will this be fine for you?
Karthik_Mahalingam 1-Jul-16 6:09am    
yes proceed.
Member 12604078 1-Jul-16 7:28am    
hi karthik, I had just posted a new question. Kindly please do look into it if possible please. Thanks.
http://www.codeproject.com/Questions/1110027/Jquery-javascript-table-multiplication-and-additio

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900