Passed
Pull Request — master (#832)
by Florian
09:14
created

Datatable   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 273
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 76
dl 0
loc 273
rs 10
c 1
b 0
f 0
wmc 15

5 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 3 1
A makeButtonsCfg() 0 59 1
A __construct() 0 32 6
A makeDomCfg() 0 13 1
A makeTableClass() 0 25 6
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
    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
        $this->id = $id;
122
        $this->heads = $heads;
123
        $this->theme = $theme;
124
        $this->headTheme = $headTheme;
125
        $this->bordered = $bordered;
126
        $this->hoverable = $hoverable;
127
        $this->striped = $striped;
128
        $this->compressed = $compressed;
129
        $this->withFooter = $withFooter;
130
        $this->footerTheme = $footerTheme;
131
        $this->beautify = $beautify;
132
        $this->withButtons = $withButtons;
133
134
        $this->config = is_array($config) ? $config : [];
135
136
        // When buttons are enabled, change the default table layout.
137
138
        if (isset($withButtons) && ! isset($this->config['dom'])) {
139
            $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
        if (isset($withButtons) && ! isset($this->config['buttons'])) {
146
            $this->config['buttons'] = $this->makeButtonsCfg();
147
        }
148
    }
149
150
    /**
151
     * Make the table class.
152
     *
153
     * @return string
154
     */
155
    public function makeTableClass()
156
    {
157
        $classes = ['table'];
158
159
        if (isset($this->bordered)) {
160
            $classes[] = 'table-bordered';
161
        }
162
163
        if (isset($this->hoverable)) {
164
            $classes[] = 'table-hover';
165
        }
166
167
        if (isset($this->striped)) {
168
            $classes[] = 'table-striped';
169
        }
170
171
        if (isset($this->compressed)) {
172
            $classes[] = 'table-sm';
173
        }
174
175
        if (isset($this->theme)) {
176
            $classes[] = "table-{$this->theme}";
177
        }
178
179
        return implode(' ', $classes);
180
    }
181
182
    /**
183
     * Make the Datatables 'dom' configuration with the buttons extension.
184
     *
185
     * @return string
186
     */
187
    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
        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
    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
        $colSelector = ':not([dt-no-export])';
216
217
        // Button to change the page length of tables.
218
219
        $lengthBtn = [
220
            'extend' => 'pageLength',
221
            'className' => 'btn-default',
222
        ];
223
224
        // Button to print the data.
225
226
        $printBtn = [
227
            'extend' => 'print',
228
            'className' => 'btn-default',
229
            'text' => '<i class="fas fa-fw fa-lg fa-print"></i>',
230
            'titleAttr' => 'Print',
231
            'exportOptions' => ['columns' => $colSelector],
232
        ];
233
234
        // Button to export data to CSV file.
235
236
        $csvBtn = [
237
            'extend' => 'csv',
238
            'className' => 'btn-default',
239
            'text' => '<i class="fas fa-fw fa-lg fa-file-csv text-primary"></i>',
240
            'titleAttr' => 'Export to CSV',
241
            'exportOptions' => ['columns' => $colSelector],
242
        ];
243
244
        // Button to export data to Excel file.
245
246
        $excelBtn = [
247
            'extend' => 'excel',
248
            'className' => 'btn-default',
249
            'text' => '<i class="fas fa-fw fa-lg fa-file-excel text-success"></i>',
250
            'titleAttr' => 'Export to Excel',
251
            'exportOptions' => ['columns' => $colSelector],
252
        ];
253
254
        // Button to export data to PDF file.
255
256
        $pdfBtn = [
257
            'extend' => 'pdf',
258
            'className' => 'btn-default',
259
            'text' => '<i class="fas fa-fw fa-lg fa-file-pdf text-danger"></i>',
260
            'titleAttr' => 'Export to PDF',
261
            'exportOptions' => ['columns' => $colSelector],
262
        ];
263
264
        // Return the set of configured buttons.
265
266
        return [
267
            'dom' => ['button' => ['className' => 'btn']],
268
            '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
    public function render()
278
    {
279
        return view('adminlte::components.datatable');
280
    }
281
}
282