Skip to content

Commit c3fa35f

Browse files
committed
Add optional parameter to generate() to return data array
1 parent 82a7e55 commit c3fa35f

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ $datatables->addOrderEscapes([
190190

191191
// Don't forget to call generate to get the results
192192
$datatables->generate();
193+
194+
// Optional usage: get the response data as an array instead of sending JSON
195+
$data = $datatables->generate(true);
193196
```
194197

195198
## Complete Example

src/DataTables.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,15 @@ public function asObject()
342342
}
343343

344344
/**
345-
* Generate the datatables results
345+
* Generate DataTables results.
346+
*
347+
* By default, sends JSON response and exits.
348+
* If $return = true, returns the response data as an array instead.
349+
*
350+
* @param bool $return Optional. Return array instead of sending JSON. Default: false.
351+
* @return array|null Response array if $return = true, otherwise null.
346352
*/
347-
public function generate()
353+
public function generate($return = FALSE)
348354
{
349355
$this->setReturnedFieldNames();
350356
$this->filter();
@@ -380,13 +386,19 @@ public function generate()
380386
}
381387
}
382388

383-
$response = new JsonResponse();
384-
$response->setData([
389+
$responseData = [
385390
'draw' => $this->request->get('draw'),
386391
'recordsTotal' => $this->recordsTotal,
387392
'recordsFiltered' => $this->recordsFiltered,
388393
'data' => $output
389-
]);
394+
];
395+
396+
if ($return) {
397+
return $responseData;
398+
}
399+
400+
$response = new JsonResponse();
401+
$response->setData($responseData);
390402
$response->send();
391403
exit;
392404
}

0 commit comments

Comments
 (0)