Automatic table computing for table computing

Source: Internet
Author: User

Automatic table computing for table computing

Let's take a look at the real-time operation animation demonstration:

Subtotal for each row, quantity X unit price. The sum is to add all rows in the subtotal column in the table.

Html code:

<Table id = "Order"> <tr> <th> billing item </th> <th> quantity </th> <th> unit price </th> <th> Subtotal </th> </tr> <tr class = "trData"> <td> ItemA </td> <input type = "text" class = 'txtquantity '/> </td> <input type = "text" class = 'txtprice'/> </td> <input type = "text" class = 'txtamount '/> </td> </tr> <tr class = "trData"> <td> ItemB </td> <input type = "text" class =' txtQuantity '/> </td> <input type = "text" class = 'txtprice'/> </td> <input type = "text" class = 'txtamount '/> </td> </tr> <tr class = "trData"> <td> ItemC </td> <input type = "text" class = 'txtquantity '/> </td> <input type = "text" class = 'txtprice'/> </td> <input type =" text "class = 'txtamount '/> </td> </tr> <tr class =" trData "> <td colspan =" 3 "style =" text-align: right; "> total </td> <span id =" total "> </span> </td> </tr> </table>Source Code


Total calculation:

 

JQuery code:

CalcSum (); function CalcSum () {var total_sum = 0; $ ("# Order. txtAmount "). each (function () {var val = $ (this ). val (); if ($. isNumeric (val) {total_sum + = parseFloat (val) ;}}); $ ("# total" ).html (total_sum );}Source Code

 

Data Change event in any file box in the quantity column:

 

JQuery code:

$ ("# Order "). on ('input', '.txt Quantity ', function () {var self = $ (this); var tr = self. closest ("tr"); var quantity = self. val (); var Price = tr. find (". txtPrice "). val (); var amount = 0 if ($. isNumeric (quantity) & $. isNumeric (Price) {amount = quantity * Price;} tr. find (". txtAmount "). val (amount); CalcSum ();});Source Code

 

Data value change event in any text box in the unit price column of the table:

 

JQuery code:

$ ("# Order "). on ('input', '.txt price', function () {var self = $ (this); var tr = self. closest ("tr"); var quantity = tr. find (". txtQuantity "). val (); var Price = self. val (); var amount = 0 if ($. isNumeric (quantity) & $. isNumeric (Price) {amount = quantity * Price;} tr. find (". txtAmount "). val (amount); calcSum ();});Source Code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.