Jquery implements statistics on the sum of cell values of specified columns in the table, jquerytable

Source: Internet
Author: User

Jquery implements statistics on the sum of cell values of specified columns in the table, jquerytable

Statistics implemented by jquery t] the sum of the cell values of the specified columns in the able table:
In some applications, table cells store numbers, such as scores of students, which may be used as an addition operation to calculate the total scores, the following code describes how to calculate the sum of the values of cells in a column.
Code example:

<! DOCTYPE html> 

The above Code meets our requirements and can calculate the sum of the values in the cells in the third column. The following describes the implementation process.
I. Code comments:
1. $ (document). ready (function () {}). After the dom structure is fully loaded, run the code in the function.
2. var totalRow = 0. Declare a variable to store the calculated sum. 3. $ ('# mytable tr'). each (function () {}) to traverse every tr row in the table.
4. $ (this ). find ('td: eq (2 )'). each (function () {totalRow + = parseFloat ($ (this ). text ();}), obtain the cell with the index value of 2 in the current row, use the each () function to traverse the cell, and accumulate the number content in the cell. Many friends may have such questions: Why don't you use $ (this) directly ). find ('td: eq (2) ') for calculation. Do you still need to use the each () function to traverse a cell? In fact, it is still necessary, because the last tr does not have a td cell, and the return value of the text () function is null, parseFloat () conversion will return the NaN value, and the final calculation result may be incorrect, however, if you use each () for traversal, because the last tr row does not have a td cell, you do not need to execute the function to avoid the above problem.
5. $ ('# totalrow '). append ('<td> total </td> <td>' + totalRow + '</td> '), add content in tr dynamically.
Ii. Related reading:
1. For details about the each () function, refer to the each () method section of jQuery.
2. For more information about the find () function, see find () method in jQuery.
3. For the parseFloat () function, see section 1 of parseFloat () method in javascript.
4. For details about the append () function, refer to the append () method section of jQuery.

The original address is: http://www.softwhy.com/forum.php? Mod = viewthread & tid = 15141.

For more information, see: http://www.softwhy.com/jquery/

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.