laravel-datatables icon indicating copy to clipboard operation
laravel-datatables copied to clipboard

Total amount causing issue with paging

Open TheGeekyM opened this issue 5 years ago • 9 comments

Summary of problem or feature request

When I get the sum of amount it works great but once I'm using the pagination the total amount gets 0.

Laravel/PHP Code ->withQuery('total', function ($filteredQuery) { return $filteredQuery->sum('Amount'); })```

Js Code table.on('draw.dt', function (e) { $('#total').text(LaravelDataTables["dataTableBuilder"].ajax.json().total) });

System details

  • Operating System unbntu 19.04
  • PHP Version 7.1
  • Laravel Version 5.8
  • Laravel-Datatables Version 9.0

TheGeekyM avatar Oct 26 '19 01:10 TheGeekyM

Any error on the response? Maybe try cloning the filteredQuery cause it's currently mutable.

->withQuery('total', function ($filteredQuery) {
    return (clone $filteredQuery)->sum('Amount');
})

yajra avatar Oct 26 '19 06:10 yajra

I already tried it before but it didn't work, It works correctly only on the page the first page.

the query I get on the first page is: select sum([Amount]) as aggregate from [Transaction]

and in other pages I got a query like this: select * from (select sum([Amount]) as aggregate , row_number() over (order by (select 0)) as row_num from [Transaction]) as temp_table where row_num between 101 and 150 order by row_num

but can I listen to all events that are fired except paging? I think it will solve the problem if there is another one

TheGeekyM avatar Oct 26 '19 10:10 TheGeekyM

I see, so the problem is paging are getting applied when doing the sum? Maybe try removing the paging applied on query before applying the sum? Is it possible to remove the limit applied on query? Will dig further when I got the chance.

yajra avatar Nov 13 '19 01:11 yajra

I have the same issue. Is there any solution for this after 2 years?

fatihmehmetkiris avatar Jan 24 '21 17:01 fatihmehmetkiris

any solve on that?

got same problem.

tcagkansokmen avatar Apr 29 '21 17:04 tcagkansokmen

Any Update?? I got the same issue.

DannyV90 avatar May 21 '21 16:05 DannyV90

@yajra did you got any chance to find solution. i have same problem

mustafaarslan1 avatar May 28 '21 06:05 mustafaarslan1

There is a work around you guys can do. May be helpful for someone. Compare the start if its 0 then return else return static value and in front check that condition. image

"fnDrawCallback": function( oSettings ) { if( oSettings.json.total != 'no' ) { $('#total_donation').text(oSettings.json.total); } },

salmansamkhan avatar Aug 14 '21 10:08 salmansamkhan

There is a work around you guys can do. May be helpful for someone. Compare the start if its 0 then return else return static value and in front check that condition. image

"fnDrawCallback": function( oSettings ) { if( oSettings.json.total != 'no' ) { $('#total_donation').text(oSettings.json.total); } },

This seems a great workaround! It works perfectly. Really thanks man!!!!

If someone is using datatables as service, in method dataTable has to use $startNo = $this->request->input("start") because it's an inherited property.

DannyV90 avatar Aug 15 '21 05:08 DannyV90

This works fine for me: ->withQuery('total', function ($filteredQuery) { return (clone $filteredQuery)->offset(0)->sum('Amount'); })

just add ->offset(0)

Jankec avatar Oct 30 '22 12:10 Jankec

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Mar 19 '23 00:03 github-actions[bot]

This issue was closed because it has been inactive for 7 days since being marked as stale.

github-actions[bot] avatar Mar 27 '23 00:03 github-actions[bot]