javascript - Datatables - Calculate Column Total when using Row Groups - Stack Overflow

I am using datatables. In addition to the regular table set up I am using datatables to addRow Groups-

I am using datatables. In addition to the regular table set up I am using datatables to add

Row Groups - Outlined here

Column Totals - Using Footer Callbacled Outlined here

I have a working Demo here /

Everything is working fine except my totals are being calculated 1 column off to the right (as you can easily see from demo) I am pretty sure the reason is because of the way groups works and is displayed.

I would like to display the totals under the correct column. i.e. shift all totals 1 column left to line up correctly.

The code used uses the first <td> in the row to use as the group name so for example

<td>Joe Hammer</td> is used as a group name.

This is the code I am using to calculate the totals

"fnFooterCallback": function (nRow, aasData, iStart, iEnd, aiDisplay) {

        var columnas = [1, 2, 3, 4, 5, 6, 7]; //the columns you wish to add            
        for (var j in columnas) {
            var columnaActual = columnas[j];
            var total = 0;
            for (var i = iStart; i < iEnd; i++) {
                total = total + parseFloat(aasData[aiDisplay[i]][columnaActual]);
            }
            $($(nRow).children().get(columnaActual)).html(total);

        } // end 

I am using datatables. In addition to the regular table set up I am using datatables to add

Row Groups - Outlined here

Column Totals - Using Footer Callbacled Outlined here

I have a working Demo here http://jsfiddle/c5UXe/

Everything is working fine except my totals are being calculated 1 column off to the right (as you can easily see from demo) I am pretty sure the reason is because of the way groups works and is displayed.

I would like to display the totals under the correct column. i.e. shift all totals 1 column left to line up correctly.

The code used uses the first <td> in the row to use as the group name so for example

<td>Joe Hammer</td> is used as a group name.

This is the code I am using to calculate the totals

"fnFooterCallback": function (nRow, aasData, iStart, iEnd, aiDisplay) {

        var columnas = [1, 2, 3, 4, 5, 6, 7]; //the columns you wish to add            
        for (var j in columnas) {
            var columnaActual = columnas[j];
            var total = 0;
            for (var i = iStart; i < iEnd; i++) {
                total = total + parseFloat(aasData[aiDisplay[i]][columnaActual]);
            }
            $($(nRow).children().get(columnaActual)).html(total);

        } // end 
Share Improve this question edited Sep 2, 2013 at 16:30 madth3 7,34412 gold badges52 silver badges74 bronze badges asked Aug 11, 2013 at 15:11 RedwallRedwall 1,0205 gold badges31 silver badges56 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

I believe you can fix it entirely through tweaking the JS.

Update the columns to add

var columnas = [2, 3, 4, 5, 6, 7, 8];

Then get columnaActual-1 instead of columnaActual

$($(nRow).children().get(columnaActual-1)).html(total);

Feels weird answering this myself, but just incase anyone else has the same issue here you go.

Updated JSFiddle http://jsfiddle/c5UXe/1/

I added an extra <th> to the column total and I removed the 2nd <th> in column total via CSS setting the style to none.

I then Updated the Columns to add to this

var columnas = [2, 3, 4, 5, 6, 7,8]; //the columns you wish to add

Removing the first 1 and adding number 8

It is a pretty easy fix which mostly involved tweaking the HTML rather than the JS.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742355212a4428292.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信