1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Http\Controllers\CrudFeatures; |
4
|
|
|
|
5
|
|
|
trait AjaxTable |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Respond with the JSON of one or more rows, depending on the POST parameters. |
9
|
|
|
* @return JSON Array of cells in HTML form. |
|
|
|
|
10
|
|
|
*/ |
11
|
|
|
public function search() |
12
|
|
|
{ |
13
|
|
|
$this->crud->hasAccessOrFail('list'); |
|
|
|
|
14
|
|
|
|
15
|
|
|
// create an array with the names of the searchable columns |
16
|
|
|
$columns = $this->searchableColumns(); |
17
|
|
|
|
18
|
|
|
// structure the response in a DataTable-friendly way |
19
|
|
|
$dataTable = new \LiveControl\EloquentDataTable\DataTable($this->crud->query, $columns); |
20
|
|
|
|
21
|
|
|
// make the datatable use the column types instead of just echoing the text |
22
|
|
|
$dataTable->setFormatRowFunction(function ($entry) { |
23
|
|
|
// get the actual HTML for each row's cell |
24
|
|
|
$row_items = $this->crud->getRowViews($entry, $this->crud); |
25
|
|
|
|
26
|
|
|
// add the buttons as the last column |
27
|
|
|
if ($this->crud->buttons->where('stack', 'line')->count()) { |
28
|
|
|
$row_items[] = \View::make('crud::inc.button_stack', ['stack' => 'line']) |
29
|
|
|
->with('crud', $this->crud) |
30
|
|
|
->with('entry', $entry) |
31
|
|
|
->render(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
// add the details_row buttons as the first column |
35
|
|
|
if ($this->crud->details_row) { |
36
|
|
|
array_unshift($row_items, \View::make('crud::columns.details_row_button') |
37
|
|
|
->with('crud', $this->crud) |
38
|
|
|
->with('entry', $entry) |
39
|
|
|
->render()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $row_items; |
43
|
|
|
}); |
44
|
|
|
|
45
|
|
|
return $dataTable->make(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Gets the searchable columns |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
protected function searchableColumns() |
54
|
|
|
{ |
55
|
|
|
// create an array with the names of the searchable columns |
56
|
|
|
$columns = collect($this->crud->columns) |
57
|
|
|
// Excludes fields that are not searchable |
58
|
|
|
->reject(function ($column, $key) { |
|
|
|
|
59
|
|
|
return array_get($column, 'searchable', true) === false; |
60
|
|
|
}) |
61
|
|
|
// Excludes fields on a relation |
62
|
|
|
->reject(function ($column, $key) { |
|
|
|
|
63
|
|
|
return array_key_exists('entity', $column); |
64
|
|
|
}) |
65
|
|
|
// Excludes field types with no column |
66
|
|
|
->reject(function ($column, $key) { |
|
|
|
|
67
|
|
|
return in_array(array_get($column, 'type'), ['select_multiple', 'model_function', 'model_function_attribute']); |
|
|
|
|
68
|
|
|
}) |
69
|
|
|
->pluck('name') |
70
|
|
|
// add the primary key, otherwise the buttons won't work |
71
|
|
|
->merge($this->crud->model->getKeyName()) |
72
|
|
|
->toArray(); |
73
|
|
|
|
74
|
|
|
return $columns; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.If the return type contains the type array, this check recommends the use of a more specific type like
String[]
orarray<String>
.