php - How to modify a DataTable in order to get parts of DB results from a controller via ajax? -
i have controller returns large amount of data. pass data twig template, make <table>
it, , call .datatable() on table element.
this not efficient, because downloads data @ once , passes page source...
i want modify in way enable datatable use ajax download , display first 500 records, when user clicks on "more" button, download , display next 500 records , on. there way achieve using datatables?
this method retrieving records (it later called in controller , results passed directly twig):
public function getloginshistory(){ $document_manager = $this ->container ->get('doctrine_mongodb') ->getmanager(); $query_builder = $document_manager ->createquerybuilder('appbundle:loginevent') ->sort('timestamp', 'desc') ->find(); $query = $query_builder->getquery(); $entries = $query->execute(); return $entries; }
this happens in twig:
<div class="table-scrollable"> <table id="login_history_table" class="table table-striped table-hover"> <thead> <tr> <th>user id</th> <th>action</th> <th>time</th> </tr> </thead> {% entry in entries %} <tr> <td> {{ entry.userid}} </td> <td> {{ entry.event==1 ? '<span class="label label-sm label-success"> logged in </span>' : '<span class="label label-sm label-danger"> logged out </span>' }} </td> <td> {{ entry.timestamp|date }} </td> </tr> {% endfor %} </table> </div> </div>
and in js file call
$("#login_history_table").datatable();
so there way achieve have described?
create new action answer on ajax calls (two arguments must provided - currentoffset , maxrecords (if need more/less 500)) , call query login history arguments request, query method smth this:
public function getloginshistory($offset = 0, $maxrecords = 500){ $document_manager = $this ->container ->get('doctrine_mongodb') ->getmanager(); $query_builder = $document_manager ->createquerybuilder('appbundle:loginevent') ->sort('timestamp', 'desc') ->limit($maxrecords) ->skip($offset) ->find(); $query = $query_builder->getquery(); $entries = $query->execute(); return $entries; }
see documentation results limiting;
Comments
Post a Comment