Laravel-datatables: Total over all pages for serverside datatable show currentpage only

Created on 16 Jul 2016  路  6Comments  路  Source: yajra/laravel-datatables

i want to show sum of current page and total pages for my serverside datatable
i use sum plugin like this

$('#mytable').DataTable({
        processing: true,
        serverSide: true,
        stateSave: true,
        ajax: 'stock/data-json',
        "fnDrawCallback": function() {
        var api = this.api();

        // Total over all pages
        var total = api.column(4).data().sum();

        // Total over this page
        var pageTotal = api.column(4, {page:'current'}).data().sum();
        $(api.column(4).footer()).html(pageTotal + ' ( ' + total + ' total)');
    }
});

the sum total shows the current page
i know this is bacause it loads only the current page and it cannot see the other records
now how i can show the correct sum of all pages in the footer

"yajra/laravel-datatables-oracle": "v6.11.3"
DataTables 1.10.12
PHP 5.6.15
windows 8.1

Most helpful comment

You can use something like below. Note that this was not tested but the concept should guide you to make it work:

Datatables::of($stock)
    ->with('total', $stock->getTotal())
    ->make();
$('#mytable').DataTable({
    processing: true,
    serverSide: true,
    stateSave: true,
    ajax: 'stock/data-json',
    "fnDrawCallback": function() {
        var api = this.api()
        var json = api.ajax.json();
        $(api.column(4).footer()).html(json.total);
    }
});

All 6 comments

You can use something like below. Note that this was not tested but the concept should guide you to make it work:

Datatables::of($stock)
    ->with('total', $stock->getTotal())
    ->make();
$('#mytable').DataTable({
    processing: true,
    serverSide: true,
    stateSave: true,
    ajax: 'stock/data-json',
    "fnDrawCallback": function() {
        var api = this.api()
        var json = api.ajax.json();
        $(api.column(4).footer()).html(json.total);
    }
});

works well
thanks

As always thanks for your help dear yajra.

Can u explain how you do that,i am new to datatables.

Working perfectly in normal condition but its not working when query have "HAVING" clause, see below -

Annotation 2019-04-12 163412

Please give me some solution...

`I did this :
const Table = $('#foo').DataTable({
. . . . . .,
. . . . . .,
drawCallback: function(){
Table.columns(5, {
page: 'current'
}).every(function() {
var sum = this
.data()
.reduce(function(a, b) {
var x = parseFloat(a) || 0;
var y = parseFloat(b) || 0;
return x + y;
}, 0);
// console.log(sum); alert(sum);
$(this.footer()).html(sum);
});
}
});

in this case the column was column number 5`

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sangnguyenplus picture sangnguyenplus  路  3Comments

nasirkhan picture nasirkhan  路  3Comments

vipin733 picture vipin733  路  3Comments

t0n1zz picture t0n1zz  路  3Comments

hohuuhau picture hohuuhau  路  3Comments