Link to home
Start Free TrialLog in
Avatar of Stacey Fontenot
Stacey Fontenot

asked on

Sum column in table

I have a jquery bootgrid. I am trying to display the sum of the "Time" in the footer: ID = #totalTime. I can't get this code to work.

----------Javascript
            // dynamically find columns positions
            var indexTime = -1;
            $(bootGrid).find('th').each(function (e) {
                if ($(this).attr('data-column-id') == 'time') {
                    indexTime = e;
                }
            });
            var totalTime = 0.0;
            $(bootGrid).find('tbody tr').each(function () {
                var time = 0.0;
                // loop through rows
                $(this).find('td').each(function (i) {
                    if (i == indexTime) {
                        time = parseFloat($(this).text());
                        alert(time);
                    }
                });
                totalTime += time;
            });
            $('#totalTime').text(totalTime.toFixed(2));

Open in new window


---BootGrid
            <table class="table table-condensed table-hover table-striped" id="grid-data">
                <thead>
                    <tr class="grid-header-nav">
                        <th data-column-id="time_id" data-visible="false">Time ID</th>
                        <th data-column-id="time">Time</th>
                    </tr>
                </thead>
                <tbody>
                    @{
                        foreach (TimeInfo timeinfo in Model.TimeInfos)
                        {
                            <tr>
                                <td>@timeinfo.Time_ID</td>
                                <td>@timeinfo.Time</td>
                            </tr>
                        }
                    }
                </tbody>
                <tfoot>
                    <tr>
                        <td>Total Hours</td>
                        <td id="#totalTime"></td>
                    </tr>

                </tfoot>
            </table>

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

do right click on the table.grid, choose view source and post it here.
Avatar of Stacey Fontenot
Stacey Fontenot

ASKER

I was just able to update the code and was able to get the summation working. I just can't get it to display the sum to id:total. totalTime has a value.

  $('#total').text(totalTime.toFixed(2));
I guess the problem comes from the id of the element :

<td id="#totalTime"></td>

Open in new window


Should be :

<td id="totalTime"></td>

Open in new window


Note the # sign must be removed.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.