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