Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Summary total in the footer column #1239

Closed
yurik3zzz opened this issue Jul 3, 2017 · 9 comments
Closed

Summary total in the footer column #1239

yurik3zzz opened this issue Jul 3, 2017 · 9 comments
Labels

Comments

@yurik3zzz
Copy link

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
@yajra
Copy link
Owner

yajra commented Jul 4, 2017

Use the same query passed on DT like:

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

@yajra yajra added the question label Jul 4, 2017
@yajra yajra closed this as completed Jul 6, 2017
@yurik3zzz
Copy link
Author

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

@prakash-pokhrel
Copy link

How to display this in blade ?

@yajra
Copy link
Owner

yajra commented Nov 16, 2018

@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()

@prakash-pokhrel
Copy link

@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

@yajra
Copy link
Owner

yajra commented Nov 17, 2018

Try adding a script on drawCallback.

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

@yajra
Copy link
Owner

yajra commented Nov 17, 2018

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 ) }')
  ...

@prakash-pokhrel
Copy link

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 ?

@yajra
Copy link
Owner

yajra commented Dec 6, 2018

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

@github-actions github-actions bot locked and limited conversation to collaborators Oct 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants