-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathHasColumns.php
182 lines (158 loc) · 4.26 KB
/
HasColumns.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace Yajra\DataTables\Html\Options;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Yajra\DataTables\Html\Column;
/**
* DataTables - Columns option builder.
*
* @see https://datatables.net/reference/option/
*/
trait HasColumns
{
/**
* Set columnDefs option value.
*
* @param array $value
* @return $this
* @see https://datatables.net/reference/option/columnDefs
*/
public function columnDefs(array $value)
{
$this->attributes['columnDefs'] = $value;
return $this;
}
private function camelRelation($column)
{
$parts = explode('.', $column);
$columnName = array_pop($parts);
$relation = Str::camel(implode('.', $parts));
return $relation . ($relation ? '.' : '') . $columnName;
}
/**
* Set columns option value.
*
* @param array $columns
* @return $this
* @see https://datatables.net/reference/option/columns
*/
public function columns(array $columns)
{
$this->collection = new Collection;
foreach ($columns as $key => $value) {
if (! is_a($value, Column::class)) {
if (is_array($value)) {
$attributes = array_merge(
[
'name' => $value['name'] ?? $this->camelRelation($value['data'] ?? $key),
'data' => $value['data'] ?? $key,
],
$this->setTitle($key, $value)
);
} else {
$attributes = [
'name' => $this->camelRelation($value),
'data' => $value,
'title' => $this->getQualifiedTitle($value),
];
}
$this->collection->push(new Column($attributes));
} else {
$this->collection->push($value);
}
}
return $this;
}
/**
* Set title attribute of an array if not set.
*
* @param string $title
* @param array $attributes
* @return array
*/
public function setTitle($title, array $attributes)
{
if (! isset($attributes['title'])) {
$attributes['title'] = $this->getQualifiedTitle($title);
}
return $attributes;
}
/**
* Convert string into a readable title.
*
* @param string $title
* @return string
*/
public function getQualifiedTitle($title)
{
return Str::title(str_replace(['.', '_'], ' ', Str::snake($title)));
}
/**
* Add a column in collection usingsl attributes.
*
* @param array $attributes
* @return $this
*/
public function addColumn(array $attributes)
{
$this->collection->push(new Column($attributes));
return $this;
}
/**
* Add a Column object at the beginning of collection.
*
* @param \Yajra\DataTables\Html\Column $column
* @return $this
*/
public function addBefore(Column $column)
{
$this->collection->prepend($column);
return $this;
}
/**
* Add a column at the beginning of collection using attributes.
*
* @param array $attributes
* @return $this
*/
public function addColumnBefore(array $attributes)
{
$this->collection->prepend(new Column($attributes));
return $this;
}
/**
* Add a Column object in collection.
*
* @param \Yajra\DataTables\Html\Column $column
* @return $this
*/
public function add(Column $column)
{
$this->collection->push($column);
return $this;
}
/**
* Get collection of columns.
*
* @return \Illuminate\Support\Collection
*/
public function getColumns()
{
return $this->collection;
}
/**
* Remove column by name.
*
* @param array $names
* @return $this
*/
public function removeColumn(...$names)
{
foreach ($names as $name) {
$this->collection = $this->collection->filter(function (Column $column) use ($name) {
return $column->name !== $name;
})->flatten();
}
return $this;
}
}