2017-01-18 1 views
0

У меня есть jquery datatable, и я хочу рассчитать сумму двух столбцов. инициализация таблицывычислить сумму двух столбцов внутри jquery datatables

$('#myTable').DataTable({ 
     autoWidth: false, 
     searching: false, 
    ... 
    initComplete: function (settings, json) { 
     // calculate sum and inject into second and third row in the footer 
    }); 
    }); 

<table id="myTable"> 
    <thead> 
     <tr> 
     <th>Id</th> 
     <th>Time one</th> 
     <th>Time two</th> 
     </tr> 
    </thead> 
    <tfoot> 
     <tr> 
     <th></th> 
     <th></th> 
     <th></th> 
     </tr> 
    </thead> 
</table> 

Update:

Vales внутри этих колонн, как:

===================== 
| 1 | 01:15 | 10:00 | 
===================== 
| 9 | 11:44 | 0:120 

|

+2

Что именно ваш вопрос? С какой проблемой вы столкнулись? –

+0

Позвольте мне перефразировать вопрос: я хочу, чтобы сумма была в нижнем колонтитуле третьего столбца. – user1765862

ответ

0

Вы можете сделать это с помощью DataTable Plugin API:

// Simply get the sum of a column 
var table = $('#example').DataTable(); 
table.column(3).data().sum(); 

// Insert the sum of a column into the columns footer, for the visible 
    // data on each draw 
    $('#example').DataTable({ 
    drawCallback: function() { 
     var api = this.api(); 
     $(api.table().footer()).html(
     api.column(4, {page:'current'}).data().sum() 
    ); 
    } 
    }); 

Подробнее: https://datatables.net/plug-ins/api/sum()

+0

Не уверен, что OP хочет общий или, скорее, новый столбец, содержащий строку за строкой, сумму –

+0

всего в нижнем колонтитуле. – user1765862

+0

Итак, вы можете использовать этот api. –

 Смежные вопросы

  • Нет связанных вопросов^_^