Skip to content

Commit 389bbdf

Browse files
committed
1 parent fc8844e commit 389bbdf

File tree

4 files changed

+94
-3
lines changed

4 files changed

+94
-3
lines changed

src/Html/Column.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Illuminate\Support\Arr;
66
use Illuminate\Support\Str;
7-
use Yajra\DataTables\Html\Options\Plugins\SearchPanes;
7+
use Yajra\DataTables\Html\Options\Plugins;
88

99
/**
1010
* @property array|string $data
@@ -35,7 +35,8 @@
3535
class Column extends Fluent
3636
{
3737
use HasAuthorizations;
38-
use SearchPanes;
38+
use Plugins\SearchPanes;
39+
use Plugins\ColumnControl;
3940

4041
/**
4142
* @param array $attributes

src/Html/HasOptions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ trait HasOptions
1717
use Options\Plugins\AutoFill;
1818
use Options\Plugins\Buttons;
1919
use Options\Plugins\ColReorder;
20+
use Options\Plugins\ColumnControl;
2021
use Options\Plugins\FixedColumns;
2122
use Options\Plugins\FixedHeader;
2223
use Options\Plugins\KeyTable;

src/Html/Options/HasFeatures.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function lengthChange(bool $value = true): static
6868
/**
6969
* Set ordering option value.
7070
*
71-
* @param bool|array{indicators: bool, handler: bool} $value
71+
* @param bool|array{indicators: bool, handler: bool} $value
7272
* @return $this
7373
*
7474
* @see https://datatables.net/reference/option/ordering
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Html\Options\Plugins;
4+
5+
/**
6+
* DataTables - Column control plugin option builder.
7+
*
8+
* @see https://datatables.net/extensions/columncontrol/
9+
* @see https://datatables.net/extensions/columncontrol/config
10+
*/
11+
trait ColumnControl
12+
{
13+
/**
14+
* Set column control options.
15+
*
16+
* @see https://www.datatables.net/extensions/columncontrol/
17+
*/
18+
public function columnControl(array $options): static
19+
{
20+
$this->attributes['columnControl'] = $options;
21+
22+
return $this;
23+
}
24+
25+
protected function addColumnControl(int|string $target, array $content): static
26+
{
27+
if (! isset($this->attributes['columnControl']) || ! is_array($this->attributes['columnControl'])) {
28+
$this->attributes['columnControl'] = [];
29+
}
30+
31+
// get existing target and merge content if exists
32+
foreach ($this->attributes['columnControl'] as &$control) {
33+
if (isset($control['target']) && $control['target'] === $target) {
34+
$control['content'] = array_unique(
35+
array_merge($control['content'] ?? [], $content)
36+
);
37+
38+
return $this;
39+
}
40+
}
41+
42+
$this->attributes['columnControl'][] = ['target' => $target, 'content' => $content];
43+
44+
return $this;
45+
}
46+
47+
public function columnControlHeader(array $content, int $target = 0): static
48+
{
49+
$this->addColumnControl('thead:'.$target, $content);
50+
51+
return $this;
52+
}
53+
54+
public function columnControlFooter(array $content, int $target = 0): static
55+
{
56+
$this->addColumnControl('tfoot:'.$target, $content);
57+
58+
return $this;
59+
}
60+
61+
public function columnControlFooterSearch(array $content = []): static
62+
{
63+
$this->addColumnControl('tfoot', [$content] ?? ['search']);
64+
65+
return $this;
66+
}
67+
68+
public function columnControlSearchDropdown(int|string $target = 0): static
69+
{
70+
$this->addColumnControl($target, ['order', 'searchDropdown'])
71+
->ordering(['indicators' => false, 'handler' => false]);
72+
73+
return $this;
74+
}
75+
76+
public function columnControlSearch(?array $content = null, int|string $target = 1): static
77+
{
78+
$this->addColumnControl($target, $content ?? ['search']);
79+
80+
return $this;
81+
}
82+
83+
public function columnControlSpacer(int|string $target = 1): static
84+
{
85+
$this->addColumnControl($target, [['extend' => 'spacer']]);
86+
87+
return $this;
88+
}
89+
}

0 commit comments

Comments
 (0)