本文介绍了如何获得使用JavaScript的GridView列的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图访​​问一个GridView的命名列的时间和使用JavaScript计算整列的总和。

I'm trying to access to the column named Time of a gridview and calculate Sum of whole column using JavaScript.

我有这样的事情:

因此​​,在报告我总想要得到5号,因为它是唯一条目现在。

So in Report total I want to get number 5, since it is only entry right now.

这是一个GridView的定义:

This is a definition of a 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>

谁能帮我用JavaScript code对于这个问题,要么跟我分享一些相关链接,导致所有我发现在互联网上是不是与此类似,我想这是简单的事时,你知道如何使用JS。

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.

调试期间总和NaN的值:

NaN value for sum during debugging:

推荐答案

一个CSS类添加到您的领域

Add a css class to your fields

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

和使用以下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);

这篇关于如何获得使用JavaScript的GridView列的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:56
查看更多