|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JeroenNoten\LaravelAdminLte\Components\Tool; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
use Illuminate\View\Component; |
|
7
|
|
|
|
|
8
|
|
|
class Datatable extends Component |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* The table identification (id) attribute. Required in order to manage |
|
12
|
|
|
* the internal or external (JS) initialization. |
|
13
|
|
|
* |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
public $id; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* An array with the set of headers (titles) for the table columns. Each |
|
20
|
|
|
* header can be a string or an array with next properties: label, width |
|
21
|
|
|
* and no-export. |
|
22
|
|
|
* |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
public $heads; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The table theme (light, dark, primary, secondary, info, warning or |
|
29
|
|
|
* danger). |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
public $theme; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The table head theme (light or dark). |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
public $headTheme; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The datatables plugin configuration parameters. Array with key => value |
|
44
|
|
|
* pairs, where the key should be an existing configuration property of |
|
45
|
|
|
* the datatables plugin. |
|
46
|
|
|
* |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
public $config; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Enables a footer with header cells. The footer can be fully customized |
|
53
|
|
|
* using the 'footerCallback' option of the plugin. |
|
54
|
|
|
* |
|
55
|
|
|
* @var mixed |
|
56
|
|
|
*/ |
|
57
|
|
|
public $withFooter; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* The table footer theme (light or dark). |
|
61
|
|
|
* |
|
62
|
|
|
* @var string |
|
63
|
|
|
*/ |
|
64
|
|
|
public $footerTheme; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* When enabled, borders will be displayed around the table. |
|
68
|
|
|
* |
|
69
|
|
|
* @var mixed |
|
70
|
|
|
*/ |
|
71
|
|
|
public $bordered; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* When enabled, a hover effect will be available for the table rows. |
|
75
|
|
|
* |
|
76
|
|
|
* @var mixed |
|
77
|
|
|
*/ |
|
78
|
|
|
public $hoverable; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* When enabled, a striped effect will be available for the table rows. |
|
82
|
|
|
* |
|
83
|
|
|
* @var mixed |
|
84
|
|
|
*/ |
|
85
|
|
|
public $striped; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* When enabled, the table will be compressed using less white space between |
|
89
|
|
|
* cells and rows. |
|
90
|
|
|
* |
|
91
|
|
|
* @var mixed |
|
92
|
|
|
*/ |
|
93
|
|
|
public $compressed; |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* When enabled, the table cells will be vertically and horizontally |
|
97
|
|
|
* centered. |
|
98
|
|
|
* |
|
99
|
|
|
* @var mixed |
|
100
|
|
|
*/ |
|
101
|
|
|
public $beautify; |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* When enabled, a set of tool buttons for export the table will be |
|
105
|
|
|
* displayed. |
|
106
|
|
|
* |
|
107
|
|
|
* @var mixed |
|
108
|
|
|
*/ |
|
109
|
|
|
public $withButtons; |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Create a new component instance. |
|
113
|
|
|
* |
|
114
|
|
|
* @return void |
|
115
|
|
|
*/ |
|
116
|
2 |
|
public function __construct( |
|
117
|
|
|
$id, $heads, $theme = null, $headTheme = null, $bordered = null, |
|
118
|
|
|
$hoverable = null, $striped = null, $compressed = null, |
|
119
|
|
|
$withFooter = null, $footerTheme = null, $beautify = null, |
|
120
|
|
|
$withButtons = null, $config = [] |
|
121
|
|
|
) { |
|
122
|
2 |
|
$this->id = $id; |
|
123
|
2 |
|
$this->heads = $heads; |
|
124
|
2 |
|
$this->theme = $theme; |
|
125
|
2 |
|
$this->headTheme = $headTheme; |
|
126
|
2 |
|
$this->bordered = $bordered; |
|
127
|
2 |
|
$this->hoverable = $hoverable; |
|
128
|
2 |
|
$this->striped = $striped; |
|
129
|
2 |
|
$this->compressed = $compressed; |
|
130
|
2 |
|
$this->withFooter = $withFooter; |
|
131
|
2 |
|
$this->footerTheme = $footerTheme; |
|
132
|
2 |
|
$this->beautify = $beautify; |
|
133
|
2 |
|
$this->withButtons = $withButtons; |
|
134
|
|
|
|
|
135
|
2 |
|
$this->config = is_array($config) ? $config : []; |
|
136
|
|
|
|
|
137
|
|
|
// When buttons are enabled, change the default table layout. |
|
138
|
|
|
|
|
139
|
2 |
|
if (isset($withButtons) && ! isset($this->config['dom'])) { |
|
140
|
1 |
|
$this->config['dom'] = $this->makeDomCfg(); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
// When buttons are enabled, setup the set of visible buttons and they |
|
144
|
|
|
// default style. |
|
145
|
|
|
|
|
146
|
2 |
|
if (isset($withButtons) && ! isset($this->config['buttons'])) { |
|
147
|
1 |
|
$this->config['buttons'] = $this->makeButtonsCfg(); |
|
148
|
|
|
} |
|
149
|
2 |
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Make the table class. |
|
153
|
|
|
* |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
1 |
|
public function makeTableClass() |
|
157
|
|
|
{ |
|
158
|
1 |
|
$classes = ['table']; |
|
159
|
|
|
|
|
160
|
1 |
|
if (isset($this->bordered)) { |
|
161
|
1 |
|
$classes[] = 'table-bordered'; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
1 |
|
if (isset($this->hoverable)) { |
|
165
|
1 |
|
$classes[] = 'table-hover'; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
1 |
|
if (isset($this->striped)) { |
|
169
|
1 |
|
$classes[] = 'table-striped'; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
1 |
|
if (isset($this->compressed)) { |
|
173
|
1 |
|
$classes[] = 'table-sm'; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
1 |
|
if (isset($this->theme)) { |
|
177
|
1 |
|
$classes[] = "table-{$this->theme}"; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
1 |
|
return implode(' ', $classes); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Make the Datatables 'dom' configuration with the buttons extension. |
|
185
|
|
|
* |
|
186
|
|
|
* @return string |
|
187
|
|
|
*/ |
|
188
|
1 |
|
protected function makeDomCfg() |
|
189
|
|
|
{ |
|
190
|
|
|
// Give bootstrap style to table elements. |
|
191
|
|
|
// The built-in table control elements in DataTables are: |
|
192
|
|
|
// l - Length changing input control. |
|
193
|
|
|
// f - Filtering input. |
|
194
|
|
|
// t - The table! |
|
195
|
|
|
// i - Table information summary. |
|
196
|
|
|
// p - Pagination control. |
|
197
|
|
|
// r - Processing display element. |
|
198
|
|
|
// B - buttons extension. |
|
199
|
|
|
|
|
200
|
1 |
|
return '<"row" <"col-md-8" B> <"col-md-4" f> > |
|
201
|
|
|
<"row" <"col-12" tr> > |
|
202
|
|
|
<"row" <"col-md-5" i> <"col-md-7" p> >'; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Make the Datatables 'buttons' configuration object to define the set of |
|
207
|
|
|
* visible buttons and they style. |
|
208
|
|
|
* |
|
209
|
|
|
* @return array |
|
210
|
|
|
*/ |
|
211
|
1 |
|
protected function makeButtonsCfg() |
|
212
|
|
|
{ |
|
213
|
|
|
// Configure the export columns selector. We are not going to export |
|
214
|
|
|
// columns that explicitly have the 'dt-no-export' attribute. |
|
215
|
|
|
|
|
216
|
1 |
|
$colSelector = ':not([dt-no-export])'; |
|
217
|
|
|
|
|
218
|
|
|
// Button to change the page length of tables. |
|
219
|
|
|
|
|
220
|
|
|
$lengthBtn = [ |
|
221
|
1 |
|
'extend' => 'pageLength', |
|
222
|
|
|
'className' => 'btn-default', |
|
223
|
|
|
]; |
|
224
|
|
|
|
|
225
|
|
|
// Button to print the data. |
|
226
|
|
|
|
|
227
|
|
|
$printBtn = [ |
|
228
|
1 |
|
'extend' => 'print', |
|
229
|
1 |
|
'className' => 'btn-default', |
|
230
|
1 |
|
'text' => '<i class="fas fa-fw fa-lg fa-print"></i>', |
|
231
|
1 |
|
'titleAttr' => 'Print', |
|
232
|
1 |
|
'exportOptions' => ['columns' => $colSelector], |
|
233
|
|
|
]; |
|
234
|
|
|
|
|
235
|
|
|
// Button to export data to CSV file. |
|
236
|
|
|
|
|
237
|
|
|
$csvBtn = [ |
|
238
|
1 |
|
'extend' => 'csv', |
|
239
|
1 |
|
'className' => 'btn-default', |
|
240
|
1 |
|
'text' => '<i class="fas fa-fw fa-lg fa-file-csv text-primary"></i>', |
|
241
|
1 |
|
'titleAttr' => 'Export to CSV', |
|
242
|
1 |
|
'exportOptions' => ['columns' => $colSelector], |
|
243
|
|
|
]; |
|
244
|
|
|
|
|
245
|
|
|
// Button to export data to Excel file. |
|
246
|
|
|
|
|
247
|
|
|
$excelBtn = [ |
|
248
|
1 |
|
'extend' => 'excel', |
|
249
|
1 |
|
'className' => 'btn-default', |
|
250
|
1 |
|
'text' => '<i class="fas fa-fw fa-lg fa-file-excel text-success"></i>', |
|
251
|
1 |
|
'titleAttr' => 'Export to Excel', |
|
252
|
1 |
|
'exportOptions' => ['columns' => $colSelector], |
|
253
|
|
|
]; |
|
254
|
|
|
|
|
255
|
|
|
// Button to export data to PDF file. |
|
256
|
|
|
|
|
257
|
|
|
$pdfBtn = [ |
|
258
|
1 |
|
'extend' => 'pdf', |
|
259
|
1 |
|
'className' => 'btn-default', |
|
260
|
1 |
|
'text' => '<i class="fas fa-fw fa-lg fa-file-pdf text-danger"></i>', |
|
261
|
1 |
|
'titleAttr' => 'Export to PDF', |
|
262
|
1 |
|
'exportOptions' => ['columns' => $colSelector], |
|
263
|
|
|
]; |
|
264
|
|
|
|
|
265
|
|
|
// The length change button should not be added if the configuration of |
|
266
|
|
|
// datatables explicitly disable it. |
|
267
|
|
|
|
|
268
|
1 |
|
$buttons = [$printBtn, $csvBtn, $excelBtn, $pdfBtn]; |
|
269
|
|
|
|
|
270
|
1 |
|
if ($this->isLengthButtonEnabled()) { |
|
271
|
1 |
|
$buttons = Arr::prepend($buttons, $lengthBtn); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
// Return the set of configured buttons. |
|
275
|
|
|
|
|
276
|
|
|
return [ |
|
277
|
1 |
|
'dom' => ['button' => ['className' => 'btn']], |
|
278
|
1 |
|
'buttons' => $buttons, |
|
279
|
|
|
]; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Check if length button should be available on the set of tool buttons. |
|
284
|
|
|
* |
|
285
|
|
|
* @return bool |
|
286
|
|
|
*/ |
|
287
|
1 |
|
protected function isLengthButtonEnabled() |
|
288
|
|
|
{ |
|
289
|
1 |
|
if (! isset($this->config['lengthChange'])) { |
|
290
|
1 |
|
return true; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
1 |
|
return (bool) $this->config['lengthChange']; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* Get the view / contents that represent the component. |
|
298
|
|
|
* |
|
299
|
|
|
* @return \Illuminate\View\View|string |
|
300
|
|
|
*/ |
|
301
|
1 |
|
public function render() |
|
302
|
|
|
{ |
|
303
|
1 |
|
return view('adminlte::components.tool.datatable'); |
|
304
|
|
|
} |
|
305
|
|
|
} |
|
306
|
|
|
|