How make summary to footer column?
$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);
-->
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.
Most helpful comment
$dataTableQuery - This is passed without filtering and|or searching data. How get a parsed query with all binding params?