|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ElfSundae\Laravel\Datatables\Html; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Yajra\Datatables\Html\Builder as BaseBuilder; |
|
8
|
|
|
|
|
9
|
|
|
class Builder extends BaseBuilder |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Table attributes. |
|
13
|
|
|
* |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $tableAttributes = [ |
|
17
|
|
|
'id' => 'dataTable', |
|
18
|
|
|
'class' => 'table table-bordered table-hover dt-responsive', |
|
19
|
|
|
'width' => '100%', |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Set table "id" attribute. |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $id |
|
26
|
|
|
* @return $this |
|
27
|
|
|
*/ |
|
28
|
|
|
public function setTableId($id) |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->setTableAttribute('id', $id); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Add classes to table "class" attribute. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string ...$classes |
|
37
|
|
|
* @return $this |
|
38
|
|
|
*/ |
|
39
|
|
|
public function addTableClass(...$classes) |
|
40
|
|
|
{ |
|
41
|
|
|
$value = Arr::get($this->tableAttributes, 'class', ''); |
|
42
|
|
|
|
|
43
|
|
|
foreach ($classes as $class) { |
|
44
|
|
|
if (! Str::contains($value, $class)) { |
|
45
|
|
|
$value .= ' '.$class; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $this->setTableAttribute('class', trim($value)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Remove classes to table "class" attribute. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string ...$classes |
|
56
|
|
|
* @return $this |
|
57
|
|
|
*/ |
|
58
|
|
|
public function removeTableClass(...$classes) |
|
59
|
|
|
{ |
|
60
|
|
|
if ($value = Arr::get($this->tableAttributes, 'class')) { |
|
61
|
|
|
foreach ($classes as $class) { |
|
62
|
|
|
if (Str::contains($value, $class)) { |
|
63
|
|
|
$value = str_replace($class, '', $value); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$this->setTableAttribute('class', trim($value)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Change table style to striped. |
|
75
|
|
|
* |
|
76
|
|
|
* @return $this |
|
77
|
|
|
*/ |
|
78
|
|
|
public function stripedTable() |
|
79
|
|
|
{ |
|
80
|
|
|
$this->removeTableClass('table-hover'); |
|
81
|
|
|
|
|
82
|
|
|
return $this->addTableClass('table-striped'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Change table style to hovered. |
|
87
|
|
|
* |
|
88
|
|
|
* @return $this |
|
89
|
|
|
*/ |
|
90
|
|
|
public function hoveredTable() |
|
91
|
|
|
{ |
|
92
|
|
|
$this->removeTableClass('table-striped'); |
|
93
|
|
|
|
|
94
|
|
|
return $this->addTableClass('table-hover'); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|