简体   繁体   English

如何使用JavaScript获取GridView的列总和

[英]How to get a sum of column of GridView using JavaScript

I'm trying to access to the column named Time of a gridview and calculate Sum of whole column using JavaScript. 我正在尝试访问名为gridview的时间的列,并使用JavaScript计算整个列的总和。

I have something like this: 我有这样的事情:

在此处输入图片说明

So in Report total I want to get number 5, since it is only entry right now. 因此,在“报告总数”中,我想获取数字5,因为它现在才是条目。

This is a definition of a gridview: 这是gridview的定义:

<div class="total">

    <asp:GridView ID="ReportGrid" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDS" Height="68px" Width="813px" AllowPaging="True">
        <Columns>
            <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" DataFormatString="{0:MM/dd/yy}" />
            <asp:BoundField DataField="Name" HeaderText="Team Member Name" SortExpression="Name" />
            <asp:BoundField DataField="ProjectName" HeaderText="Projects" SortExpression="ProjectName" />
            <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
            <asp:BoundField DataField="Time" HeaderText="Time" SortExpression="Time" />
        </Columns>
    </asp:GridView>
    <br />
    <br />
    <span>Report total: <em>
        <asp:Label runat="server" ID="totalHours" Text="0"></asp:Label></em></span>
</div>

Can anyone help me with javaScript code for this problem or share with me some related links, cause all I've found on the internet wasn't similar to this, and I guess this is simple thing to do when you know how to use JS. 谁能为我提供有关此问题的javaScript代码或与我分享一些相关链接,导致我在互联网上发现的所有内容都不与此相似,并且我想这是当您知道如何使用JS时要做的简单事情。

NaN value for sum during debugging: 调试期间的总和的NaN值:

在此处输入图片说明

Add a css class to your fields 在字段中添加CSS类

    <asp:BoundField DataField="Time" HeaderText="Time"> 
      <ItemStyle CssClass="yourclass"></ItemStyle> 
     </asp:BoundField> 

and use the following code 并使用以下代码

 var fields= document.getElementsByClassName('yourclass');
    var sum = 0;
    for (var i = 0; i < fields.length; ++i) {
        var item = fields[i];  
         sum += parseInt(item.innerHTML);
    }

and then assign that sum to your total label 然后将该总和分配给您的总标签

$("#<%= totalHours.ClientID %>").text(sum);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM