1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\DataTables\Html; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
|
7
|
|
|
trait HasTable |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Retrieves HTML table attribute value. |
11
|
|
|
* |
12
|
|
|
* @param string $attribute |
13
|
|
|
* @return mixed |
14
|
|
|
* @throws \Exception |
15
|
|
|
*/ |
16
|
|
|
public function getTableAttribute($attribute) |
17
|
|
|
{ |
18
|
|
|
if (! array_key_exists($attribute, $this->tableAttributes)) { |
|
|
|
|
19
|
|
|
throw new \Exception("Table attribute '{$attribute}' does not exist."); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
return $this->tableAttributes[$attribute]; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Get table computed table attributes. |
27
|
|
|
* |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
public function getTableAttributes() |
31
|
|
|
{ |
32
|
|
|
return $this->tableAttributes; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Sets HTML table "id" attribute. |
37
|
|
|
* |
38
|
|
|
* @param string $id |
39
|
|
|
* @return $this |
40
|
|
|
*/ |
41
|
|
|
public function setTableId($id) |
42
|
|
|
{ |
43
|
|
|
return $this->setTableAttribute('id', $id); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Sets HTML table attribute(s). |
48
|
|
|
* |
49
|
|
|
* @param string|array $attribute |
50
|
|
|
* @param mixed $value |
51
|
|
|
* @return $this |
52
|
|
|
*/ |
53
|
|
|
public function setTableAttribute($attribute, $value = null) |
54
|
|
|
{ |
55
|
|
|
if (is_array($attribute)) { |
56
|
|
|
return $this->setTableAttributes($attribute); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->tableAttributes[$attribute] = $value; |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Sets multiple HTML table attributes at once. |
66
|
|
|
* |
67
|
|
|
* @param array $attributes |
68
|
|
|
* @return $this |
69
|
|
|
*/ |
70
|
|
|
public function setTableAttributes(array $attributes) |
71
|
|
|
{ |
72
|
|
|
foreach ($attributes as $attribute => $value) { |
73
|
|
|
$this->tableAttributes[$attribute] = $value; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Add class names to the "class" attribute of HTML table. |
81
|
|
|
* |
82
|
|
|
* @param string|array $class |
83
|
|
|
* @return $this |
84
|
|
|
*/ |
85
|
|
|
public function addTableClass($class) |
86
|
|
|
{ |
87
|
|
|
$class = is_array($class) ? implode(' ', $class) : $class; |
88
|
|
|
$currentClass = Arr::get(array_change_key_case($this->tableAttributes), 'class'); |
89
|
|
|
|
90
|
|
|
$classes = preg_split('#\s+#', $currentClass . ' ' . $class, null, PREG_SPLIT_NO_EMPTY); |
91
|
|
|
$class = implode(' ', array_unique($classes)); |
92
|
|
|
|
93
|
|
|
return $this->setTableAttribute('class', $class); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Remove class names from the "class" attribute of HTML table. |
98
|
|
|
* |
99
|
|
|
* @param string|array $class |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function removeTableClass($class) |
103
|
|
|
{ |
104
|
|
|
$class = is_array($class) ? implode(' ', $class) : $class; |
105
|
|
|
$currentClass = Arr::get(array_change_key_case($this->tableAttributes), 'class'); |
106
|
|
|
|
107
|
|
|
$classes = array_diff( |
108
|
|
|
preg_split('#\s+#', $currentClass, null, PREG_SPLIT_NO_EMPTY), |
109
|
|
|
preg_split('#\s+#', $class, null, PREG_SPLIT_NO_EMPTY) |
110
|
|
|
); |
111
|
|
|
$class = implode(' ', array_unique($classes)); |
112
|
|
|
|
113
|
|
|
return $this->setTableAttribute('class', $class); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Compile table headers and to support responsive extension. |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
protected function compileTableHeaders() |
122
|
|
|
{ |
123
|
|
|
$th = []; |
124
|
|
|
foreach ($this->collection->toArray() as $row) { |
|
|
|
|
125
|
|
|
$thAttr = $this->html->attributes(array_merge( |
|
|
|
|
126
|
|
|
Arr::only($row, ['class', 'id', 'title', 'width', 'style', 'data-class', 'data-hide']), |
127
|
|
|
$row['attributes'], |
128
|
|
|
isset($row['titleAttr']) ? ['title' => $row['titleAttr']] : [] |
129
|
|
|
)); |
130
|
|
|
$th[] = '<th ' . $thAttr . '>' . $row['title'] . '</th>'; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $th; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Compile table search headers. |
138
|
|
|
* |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
protected function compileTableSearchHeaders() |
142
|
|
|
{ |
143
|
|
|
$search = []; |
144
|
|
|
foreach ($this->collection->all() as $key => $row) { |
145
|
|
|
$search[] = $row['searchable'] ? '<th>' . (isset($row->search) ? $row->search : '') . '</th>' : '<th></th>'; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $search; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Compile table footer contents. |
153
|
|
|
* |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
protected function compileTableFooter() |
157
|
|
|
{ |
158
|
|
|
$footer = []; |
159
|
|
|
foreach ($this->collection->all() as $row) { |
160
|
|
|
if (is_array($row->footer)) { |
161
|
|
|
$footerAttr = $this->html->attributes(Arr::only($row->footer, |
162
|
|
|
['class', 'id', 'title', 'width', 'style', 'data-class', 'data-hide'])); |
163
|
|
|
$title = isset($row->footer['title']) ? $row->footer['title'] : ''; |
164
|
|
|
$footer[] = '<th ' . $footerAttr . '>' . $title . '</th>'; |
165
|
|
|
} else { |
166
|
|
|
$footer[] = '<th>' . $row->footer . '</th>'; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $footer; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
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: