|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace WdevRs\LaravelDatagrid\DataGrid; |
|
5
|
|
|
|
|
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
7
|
|
|
|
|
8
|
|
|
class DataGrid |
|
9
|
|
|
{ |
|
10
|
|
|
protected Builder $query; |
|
|
|
|
|
|
11
|
|
|
protected array $columns; |
|
12
|
|
|
protected array $formatters; |
|
13
|
|
|
|
|
14
|
|
|
public function query(Builder $query): self |
|
15
|
|
|
{ |
|
16
|
|
|
$this->query = $query; |
|
17
|
|
|
|
|
18
|
|
|
return $this; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function column(string $id, string $name, $formatter = null, ?string $width = null): self |
|
22
|
|
|
{ |
|
23
|
|
|
$this->columns[] = [ |
|
24
|
|
|
'id' => $id, |
|
25
|
|
|
'name' => $name, |
|
26
|
|
|
'width' => $width |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
$this->formatters[$id] = $formatter; |
|
30
|
|
|
|
|
31
|
|
|
return $this; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function render(string $view = 'laravel-datagrid::datagrid') |
|
35
|
|
|
{ |
|
36
|
|
|
$request = request(); |
|
37
|
|
|
|
|
38
|
|
|
if ($request->ajax()) { |
|
39
|
|
|
$paginator = $this->search($request->search) |
|
40
|
|
|
->sort($request->order, $request->dir) |
|
41
|
|
|
->paginate($request->limit); |
|
42
|
|
|
|
|
43
|
|
|
return [ |
|
44
|
|
|
'data' => $this->format($paginator->items()), |
|
45
|
|
|
'total' => $paginator->total() |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return view($view, [ |
|
51
|
|
|
'baseUrl' => $request->url(), |
|
52
|
|
|
'columns' => $this->columns |
|
53
|
|
|
]); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected function format($data) |
|
57
|
|
|
{ |
|
58
|
|
|
return collect($data)->map(function($item){ |
|
59
|
|
|
$formatted = $item->toArray(); |
|
60
|
|
|
foreach($this->formatters as $field => $formatter){ |
|
61
|
|
|
if (is_callable($formatter)) { |
|
62
|
|
|
$formatted[$field] = $formatter($item); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $formatted; |
|
67
|
|
|
}); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected function search(?string $search): self |
|
71
|
|
|
{ |
|
72
|
|
|
if (!$search) { |
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$this->query->where(function(Builder $query) use($search){ |
|
77
|
|
|
foreach ($this->columns as $column){ |
|
78
|
|
|
$this->query->orWhere($column['id'], 'like', '%' . $search . '%'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
}); |
|
82
|
|
|
|
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function sort(?array $orders, ?array $dirs): self |
|
87
|
|
|
{ |
|
88
|
|
|
if (!$orders || !$dirs) { |
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
collect($orders)->each(fn($field, $index) => $this->query->orderBy($field, $dirs[$index] ?? 'asc')); |
|
93
|
|
|
|
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function paginate($limit) |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->query->paginate($limit); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|