|
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
|
|
|
array_only($row, ['class', 'id', 'width', 'style', 'data-class', 'data-hide']), |
|
127
|
|
|
$row['attributes'] |
|
128
|
|
|
)); |
|
129
|
|
|
$th[] = '<th ' . $thAttr . '>' . $row['title'] . '</th>'; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $th; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Compile table search headers. |
|
137
|
|
|
* |
|
138
|
|
|
* @return array |
|
139
|
|
|
*/ |
|
140
|
|
|
protected function compileTableSearchHeaders() |
|
141
|
|
|
{ |
|
142
|
|
|
$search = []; |
|
143
|
|
|
foreach ($this->collection->all() as $key => $row) { |
|
144
|
|
|
$search[] = $row['searchable'] ? '<th>' . (isset($row->search) ? $row->search : '') . '</th>' : '<th></th>'; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return $search; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Compile table footer contents. |
|
152
|
|
|
* |
|
153
|
|
|
* @return array |
|
154
|
|
|
*/ |
|
155
|
|
|
protected function compileTableFooter() |
|
156
|
|
|
{ |
|
157
|
|
|
$footer = []; |
|
158
|
|
|
foreach ($this->collection->all() as $row) { |
|
159
|
|
|
if (is_array($row->footer)) { |
|
160
|
|
|
$footerAttr = $this->html->attributes(array_only($row->footer, |
|
161
|
|
|
['class', 'id', 'width', 'style', 'data-class', 'data-hide'])); |
|
162
|
|
|
$title = isset($row->footer['title']) ? $row->footer['title'] : ''; |
|
163
|
|
|
$footer[] = '<th ' . $footerAttr . '>' . $title . '</th>'; |
|
164
|
|
|
} else { |
|
165
|
|
|
$footer[] = '<th>' . $row->footer . '</th>'; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $footer; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
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: