locked
Array conversion error in jquery RRS feed

  • Question

  • User1839056048 posted

    hi following is a working example

    var totals = [0, 0, 0, 0, 0, 0];
                var $dataRows = $("#GridViewProfit tr:not('.totalColumn, .titlerow')");
                $dataRows.each(function () {
                    $(this).find('.rowDataSd').each(function (i) {
                        var k = parseInt($(this).html());
                        if ($(this).html() != '') {
    
                            totals[i] += parseInt($(this).html());
                            console.log(totals[i]);
                            // $("#GridViewProfit tr:last td").eq(i + 1).html(totals[i]).css('background-color', 'white');
                        }
                    });
                });
                $("#GridViewProfit td.totalCol").each(function (i) {
                    $(this).html("total:" + totals[i]);
                });

    this is for calculating sum of each column in an html table.

    here iam directly declare totals as

    var totals=[0,0,0,0,0,0];

    my requirement is to create totals dynamically

    like following code

     var totals;
                var l = $('#GridViewProfit tr th').length - 1;
                var str = '[';
                for (k = 0; k < l; k++) {
                    str += 0 + ' ,';
    
                }
    
                alert(totals);
                var bb = str;
                bb = bb.slice(0, -1);
                bb += ']';
                totals = bb;

    here iam getting bb=[0,0,0,0,0,0];

    i convert into totals=bb;

    but this is not working

    How to solve this

    Regards

    Baiju

    Monday, August 22, 2016 3:58 PM

Answers

  • User475983607 posted

    Why do you need an array when you're summing a column?  Are you trying to sum multiple columns?

    Anyway, here is a basic example.  I assigned a class (Value) on each column that I want to sum then just used a simple $.each() loop.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridviewDemo4.aspx.cs" Inherits="TestingWeb.GridviewDemo4" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
         <script src="//code.jquery.com/jquery-1.12.4.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $('#Button1').click(function () {
                    var total = 0;
                    $.each($('.Value'), function (index, value) {
                        total += parseInt($(value).html());
                    });
    
                    $('#total').html(total);
                });
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:BoundField AccessibleHeaderText="Id" DataField="Id" HeaderText="Id" />
                    <asp:BoundField AccessibleHeaderText="Value" DataField="Value" HeaderText="Value">
                    <ItemStyle CssClass="Value" />
                    </asp:BoundField>
                </Columns>
            </asp:GridView>
        </div>
            <div id="total"></div>
            <input id="Button1" type="button" value="button" />
        </form>
    </body>
    </html>
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace TestingWeb
    {
        public partial class GridviewDemo4 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                {
                    GridView1.DataSource = GetData();
                    GridView1.DataBind();
                }
            }
    
            protected List<DataItem> GetData()
            {
                List<DataItem> data = new List<DataItem>();
    
                for (int i = 0; i < 5; i++)
                {
                    data.Add(new DataItem() { Id = i, Value = i });
                }
    
                return data;
            }
    
    
            [Serializable]
            public class DataItem
            {
                public int Id { get; set; }
                public int Value { get; set; }
            }
    
        }
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 22, 2016 4:23 PM