Laravel-datatables: Summary total in the footer column

Created on 3 Jul 2017  ·  9Comments  ·  Source: yajra/laravel-datatables

Summary of problem or feature request

How make summary to footer column?

Code snippet of problem

        $dataTableQuery = Users::query()
            ->leftJoin('user_info', 'users.id', '=', 'user_info.id')
            ->select([
                'users.id',
                'users.username',
                'users.role_id',
                'user_info.first_name',
                'user_info.last_name',
                'user_info.email',
                'users.blocked',
                'users.balance',
                'users.created',
            ]);

$sums = [
            'sum_balance' => '0',
        ];

        $datatables = Datatables::of($dataTableQuery )
            ->escapeColumns(['first_name', 'last_name', 'email'])
            ->editColumn('created', function ($user) {
                return Carbon::createFromFormat('Y-m-d H:i:s', $user->created)->toDateString();
            })
            ->editColumn('blocked', function ($user) {
                return $user->blocked_label;
            })
            ->addColumn('actions', function ($user) {
                return $user->gamers_buttons;
            })
            ->filter(function ($query) use ($request) {
                if ($request->has('username')) {
                    $query->where('username', 'like', "%{$request->get('username')}%");
                }
            })
            ->addSum('sum_balance', DB::raw("sum(balance)")) <-- How do it?
            ->with('sums', $sums);

-->

System details

  • Operating System Windows 8.1
  • PHP Version 5.6.29
  • Laravel Version 5.4
  • Laravel-Datatables Version 7
question

Most helpful comment

$dataTableQuery - This is passed without filtering and|or searching data. How get a parsed query with all binding params?

All 9 comments

Use the same query passed on DT like:

->with('sum_balance', $dataTableQuery->sum('balance'))

$dataTableQuery - This is passed without filtering and|or searching data. How get a parsed query with all binding params?

How to display this in blade ?

@yurik3zzz you can use withQuery api for that on the latest version.

@prakash-pokhrel you would need to handle the display via javascript by accessing the ajax response data. Something like: LaravelDataTables["users-table"].ajax.json()

@yajra I still didn't get it. Should I place this inside html() function of datatable or put directly in blade. Btw, I am using datatable as service

Try adding a script on drawCallback.

```js
$('#users-table').on('draw.dt', function() {
$('#total').val( LaravelDataTables["users-table"].ajax.json().total )
})

If you are using the latest html package v4.0, you can do something like:

public function html()
{
  ...
  ->ajax()
  ->drawCallback('function() { $("#total").val( this.api().ajax.json().total ) }')
  ...

Hi,
What does "#total" stands for in
->drawCallback('function() { $("#total").val( this.api().ajax.json().total ) }')

Is this id inside table or in other html element ?

It's a jquery selector for an element using id.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ruchisheth picture ruchisheth  ·  16Comments

aliworkshop picture aliworkshop  ·  14Comments

jay-shah-rushiinfotech picture jay-shah-rushiinfotech  ·  19Comments

marioene picture marioene  ·  22Comments

MahdiPishguy picture MahdiPishguy  ·  17Comments