1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Http\Controllers\CrudFeatures; |
4
|
|
|
|
5
|
|
|
trait AjaxTable |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* The search function that is called by the data table. |
9
|
|
|
* |
10
|
|
|
* @return JSON Array of cells in HTML form. |
11
|
|
|
*/ |
12
|
|
|
public function search() |
13
|
|
|
{ |
14
|
|
|
$this->crud->hasAccessOrFail('list'); |
|
|
|
|
15
|
|
|
|
16
|
|
|
$totalRows = $filteredRows = $this->crud->count(); |
17
|
|
|
|
18
|
|
|
// if a search term was present |
19
|
|
|
if ($this->request->input('search') && $this->request->input('search')['value']) { |
20
|
|
|
// filter the results accordingly |
21
|
|
|
$this->crud->applySearchTerm($this->request->input('search')['value']); |
|
|
|
|
22
|
|
|
// recalculate the number of filtered rows |
23
|
|
|
$filteredRows = $this->crud->count(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
// start the results according to the datatables pagination |
27
|
|
|
if ($this->request->input('start')) { |
28
|
|
|
$this->crud->skip($this->request->input('start')); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// limit the number of results according to the datatables pagination |
32
|
|
|
if ($this->request->input('length')) { |
33
|
|
|
$this->crud->take($this->request->input('length')); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// overwrite any order set in the setup() method with the datatables order |
37
|
|
|
if ($this->request->input('order')) { |
38
|
|
|
$column_number = $this->request->input('order')[0]['column']; |
39
|
|
|
if ($this->crud->details_row) { |
40
|
|
|
$column_number = $column_number - 1; |
|
|
|
|
41
|
|
|
} |
42
|
|
|
$column_direction = $this->request->input('order')[0]['dir']; |
43
|
|
|
$column = $this->crud->findColumnById($column_number); |
44
|
|
|
|
45
|
|
|
if ($column['tableColumn']) { |
46
|
|
|
$this->crud->orderBy($column['name'], $column_direction); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$entries = $this->crud->getEntries(); |
51
|
|
|
|
52
|
|
|
return $this->crud->getEntriesAsJsonForDatatables($entries, $totalRows, $filteredRows); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: