sum of a column of all the pages of a filter Ajax

sum of a column of all the pages of a filter Ajax

VConsultingVConsulting Posts: 1Questions: 1Answers: 0
edited June 30 in Free community support

I am looking for the sum of a column of all the pages of a filter, I managed to obtain the sum of the current page:

I manage to pass the sum of the total of the records of the table in JSON parameter without worries, but if I add a filter, for example by date, I cannot find how to recover the sum of the lines concerned by the filter ...
Datatable load page by page in AJAX therefore impossible to calculate in JS and on the server side it is the datatable which takes care of the filter ...
I can't figure out how to tell him to give me that filter sum ... has anyone ever done that?

I followed :
https://datatables.net/examples/advanced_init/footer_callback.html

But with :

// Total over all pages
total = api
.column (2, {filter: 'applied'})
.data ()
.reduce (function (a, b) {
return intVal (a) + intVal (b);
}, 0);

I don't have the sum of the total with the filter.

Server side:

-> with ('total', $ datas-> sum ('amount')) returns the total of all the rows in the table but not the current filter

return Datatables::of($datas)
            ->editColumn('created_at', function ($datas) {
                return $datas->created_at->format('d-m-Y');
            })
            ->filterColumn('created_at', function ($query, $keyword) {
                $query->whereRaw("DATE_FORMAT(created_at,'%d-%m-%Y') like ?", ["%$keyword%"]);
            })
            ->with('total', $datas->sum('montant'))
            ->make(true);

An idea ?

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • colincolin Posts: 8,995Questions: 0Answers: 1,501

    Does total contain the correct sum across all pages? If so, in footerCallback you could do something like:

    var api = this.api();
    var json = api.ajax.json();
    
    $( api.column( 4 ).footer() ).html(json.total);
    

    Colin

Sign In or Register to comment.