1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\DataTables\Html\Options; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Yajra\DataTables\Html\Column; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* DataTables - Columns option builder. |
10
|
|
|
* |
11
|
|
|
* @see https://datatables.net/reference/option/ |
12
|
|
|
*/ |
13
|
|
|
trait HasColumns |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Set columnDefs option value. |
17
|
|
|
* |
18
|
|
|
* @param array $value |
19
|
|
|
* @return $this |
20
|
|
|
* @see https://datatables.net/reference/option/columnDefs |
21
|
|
|
*/ |
22
|
|
|
public function columnDefs(array $value) |
23
|
|
|
{ |
24
|
|
|
$this->attributes['columnDefs'] = $value; |
|
|
|
|
25
|
|
|
|
26
|
|
|
return $this; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Set columns option value. |
31
|
|
|
* |
32
|
|
|
* @param array $columns |
33
|
|
|
* @return $this |
34
|
|
|
* @see https://datatables.net/reference/option/columns |
35
|
|
|
*/ |
36
|
|
|
public function columns(array $columns) |
37
|
|
|
{ |
38
|
|
|
$this->collection = new Collection; |
|
|
|
|
39
|
|
|
|
40
|
|
|
foreach ($columns as $key => $value) { |
41
|
|
|
if (! is_a($value, Column::class)) { |
42
|
|
|
if (is_array($value)) { |
43
|
|
|
$attributes = array_merge( |
44
|
|
|
[ |
45
|
|
|
'name' => $value['name'] ?? $value['data'] ?? $key, |
46
|
|
|
'data' => $value['data'] ?? $key, |
47
|
|
|
], |
48
|
|
|
$this->setTitle($key, $value) |
|
|
|
|
49
|
|
|
); |
50
|
|
|
} else { |
51
|
|
|
$attributes = [ |
52
|
|
|
'name' => $value, |
53
|
|
|
'data' => $value, |
54
|
|
|
'title' => $this->getQualifiedTitle($value), |
|
|
|
|
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->collection->push(new Column($attributes)); |
59
|
|
|
} else { |
60
|
|
|
$this->collection->push($value); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Add a column in collection usingsl attributes. |
69
|
|
|
* |
70
|
|
|
* @param array $attributes |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function addColumn(array $attributes) |
74
|
|
|
{ |
75
|
|
|
$this->collection->push(new Column($attributes)); |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Add a Column object at the beginning of collection. |
82
|
|
|
* |
83
|
|
|
* @param \Yajra\DataTables\Html\Column $column |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
|
|
public function addBefore(Column $column) |
87
|
|
|
{ |
88
|
|
|
$this->collection->prepend($column); |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Add a column at the beginning of collection using attributes. |
95
|
|
|
* |
96
|
|
|
* @param array $attributes |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
|
|
public function addColumnBefore(array $attributes) |
100
|
|
|
{ |
101
|
|
|
$this->collection->prepend(new Column($attributes)); |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Add a Column object in collection. |
108
|
|
|
* |
109
|
|
|
* @param \Yajra\DataTables\Html\Column $column |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
|
|
public function add(Column $column) |
113
|
|
|
{ |
114
|
|
|
$this->collection->push($column); |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get collection of columns. |
121
|
|
|
* |
122
|
|
|
* @return \Illuminate\Support\Collection |
123
|
|
|
*/ |
124
|
|
|
public function getColumns() |
125
|
|
|
{ |
126
|
|
|
return $this->collection; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Remove column by name. |
131
|
|
|
* |
132
|
|
|
* @param array $names |
133
|
|
|
* @return $this |
134
|
|
|
*/ |
135
|
|
|
public function removeColumn(...$names) |
136
|
|
|
{ |
137
|
|
|
foreach ($names as $name) { |
138
|
|
|
$this->collection = $this->collection->filter(function (Column $column) use ($name) { |
139
|
|
|
return $column->name !== $name; |
140
|
|
|
})->flatten(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
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: