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
---BootGrid
----------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));
---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>
do right click on the table.grid, choose view source and post it here.
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 ));
$('#total').text(totalTime
I guess the problem comes from the id of the element :
Should be :
Note the # sign must be removed.
<td id="#totalTime"></td>
Should be :
<td id="totalTime"></td>
Note the # sign must be removed.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers 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.