Sum column in table

Stacey Fontenot
Stacey Fontenot used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

Commented:
do right click on the table.grid, choose view source and post it here.

Author

Commented:
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));
Zakaria AcharkiAnalyst Developer
Distinguished Expert 2018

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial