I have a page where several tables displayed with the data from the backend with php foreach functionality.
Each table has few rows with a column " m3 "
<td class="p-0 singleUnitCubic-{{$container->id}}">
{{$item->prettyDecimal('total_cubic')}}
</td>
rendered result is:
_______________________________________________________
<td class="p-0 singleUnitCubic-13">
12.600
</td>
<td class="p-0 singleUnitCubic-13">
4.000
</td>
_______________________________________________________
Total: <span id="totalCubic-13">16.600</span>
_______________________________________________________
<td class="p-0 singleUnitCubic-69">
4.230
</td>
<td class="p-0 singleUnitCubic-69">
3.000
</td>
_______________________________________________________
Total: <span id="totalCubic-69">7.230</span>
Now I can easily get the sum for one of the tables because I marked each class="singleUnitCubic" with corresponding table id number, in this case, it's class="singleUnitCubic-13" and my sum function is:
cubicOutput = document.getElementById('totalCubic-13');
var sumSingleUnits = 0;
$('.singleUnitCubic-13').each(function(){
sumSingleUnits += parseFloat($(this).text());
cubicOutput.innerText = SingleUnits.toFixed(3);
});
but how can I do the same dynamically for other tables where class="singleUnitCubic-69" and also display that sum under each corresponding table where <span id="totalCubic-69">?
Should it be something like finding what is the id number after singleUnitCubic- which I could do by:
<td onload="getTableid(this.class)" class="p-0 singleUnitCubic-{{$container->id}}">
{{$item->prettyDecimal('total_cubic')}}
</td>
function getTableId(Id) {
tableId = Id.split('-')[1];
totalCubic = document.getElementById("totalCubic-".concat(tableId));
and then somehow injecting it to the sum function?
P.S. I can't do the sum in back-end which would be easy.