Add a summary row for the gridview using JavaScript in Asp.net

Source: Internet
Author: User

The method for adding column summary to the gridview is rich, but there are almost two client-side operations and server-side operations.

I often use client operations. For convenience, I use jquery to write code.

For example, we have such a data grid,

Now we want to add a summary row below to calculate the number of students and the total bonus.

First, calculate the total number of persons as follows:

Function getpersons () <br/>{< br/> var Total = 0.00; <br/> // calculates the total number of data rows, remove the header line <br/> total =$ ('# gridview1 '). find ('tr '). length-1; <br/> return total; <br/>}

Then we calculate the total bonus:

Function gettotal () <br/>{< br/> var Total = 0; <br/> $ ('# gridview1 '). find ('tr '). each (function () {<br/> // calculate the number of cells in the third column (bonus) <br/> $ (this ). find ('td: eq (3 )'). each (function () {<br/> var tempvar = $. trim ($ (this ). ATTR ("innerhtml"); <br/> If (tempvar) {<br/> total + = ~~ Tempvar; <br/>}< br/>}); <br/> return total; <br/>}

If the value is the template column Textbox (or other controls), we can

VaR tempvar = $. Trim ($ (this). ATTR ("innerhtml "));

Change

VaR tempvar = $ (this). Find ('input'). Val ();

In this way, the data in the control can be retrieved.

Now, we can add a summary row for gridview1,

Function createrow () <br/>{< br/> var pers = getpersons (); <br/> var Total = gettotal (); <br/> $ ('# gridview1 '). append ("<tr style =" background-color: # b3d9ff; "mce_style =" background-color: # b3d9ff; "> <TD style =" color: red; "mce_style =" color: red; "> total" + pers + "People </TD> <TD> ¥" + total + "</TD> </tr> "); <br/>}

Of course, you can customize the style of the newly added rows and add multiple rows to meet your needs. For example, you can add another row and calculate the average bonus.

Finally, we call the createrow method when page Dom loading is complete.

$ (Document). Ready (function () {<br/> createrow (); <br/> });

This completes the addition of our summary rows. Here we mainly operate the table. In fact, we want to parse it into a table control or a table. We can also perform operations in a similar way.

 

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.