Skip to content

Commit 93b8ba9

Browse files
authored
Merge pull request #244 from yajra/column-control
feat: column control builder
2 parents 1f495cf + 7dfce84 commit 93b8ba9

File tree

6 files changed

+486
-3
lines changed

6 files changed

+486
-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\ColumnControl;
39+
use Plugins\SearchPanes;
3940

4041
/**
4142
* @param array $attributes

src/Html/Editor/Fields/Options.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ public static function yesNo(): static
2828
return new static($data);
2929
}
3030

31+
/**
32+
* Create options from an array.
33+
*
34+
* @param array<array-key, string|int|float|array{label: string, value: mixed}> $data
35+
*/
36+
public static function from(array $data): static
37+
{
38+
$items = [];
39+
foreach ($data as $key => $value) {
40+
$items[] = is_array($value) ? $value : ['label' => $value, 'value' => $key];
41+
}
42+
43+
return new static($items);
44+
}
45+
3146
/**
3247
* Get options from a model.
3348
*

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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,12 @@ public function lengthChange(bool $value = true): static
6868
/**
6969
* Set ordering option value.
7070
*
71+
* @param bool|array{indicators: bool, handler: bool} $value
7172
* @return $this
7273
*
7374
* @see https://datatables.net/reference/option/ordering
7475
*/
75-
public function ordering(bool $value = true): static
76+
public function ordering(bool|array $value = true): static
7677
{
7778
$this->attributes['ordering'] = $value;
7879

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
$existingContent = $control['content'] ?? [];
35+
$mergedContent = array_merge($existingContent, $content);
36+
37+
// Remove duplicates properly for mixed array types (strings and arrays)
38+
$uniqueContent = [];
39+
foreach ($mergedContent as $item) {
40+
$serialized = is_array($item) ? serialize($item) : $item;
41+
if (! in_array($serialized, array_map(fn ($i) => is_array($i) ? serialize($i) : $i, $uniqueContent))) {
42+
$uniqueContent[] = $item;
43+
}
44+
}
45+
46+
$control['content'] = $uniqueContent;
47+
48+
return $this;
49+
}
50+
}
51+
52+
$this->attributes['columnControl'][] = ['target' => $target, 'content' => $content];
53+
54+
return $this;
55+
}
56+
57+
public function columnControlHeader(array $content, int $target = 0): static
58+
{
59+
$this->addColumnControl('thead:'.$target, $content);
60+
61+
return $this;
62+
}
63+
64+
public function columnControlFooter(array $content, int $target = 0): static
65+
{
66+
$this->addColumnControl('tfoot:'.$target, $content);
67+
68+
return $this;
69+
}
70+
71+
public function columnControlFooterSearch(array $content = []): static
72+
{
73+
$this->addColumnControl('tfoot', [empty($content) ? ['search'] : $content]);
74+
75+
return $this;
76+
}
77+
78+
public function columnControlSearchDropdown(int|string $target = 0): static
79+
{
80+
$this->addColumnControl($target, ['order', 'searchDropdown'])
81+
->ordering(['indicators' => false, 'handler' => false]);
82+
83+
return $this;
84+
}
85+
86+
public function columnControlSearch(?array $content = null, int|string $target = 1): static
87+
{
88+
$this->addColumnControl($target, $content ?? ['search']);
89+
90+
return $this;
91+
}
92+
93+
public function columnControlSpacer(int|string $target = 1): static
94+
{
95+
$this->addColumnControl($target, [['extend' => 'spacer']]);
96+
97+
return $this;
98+
}
99+
100+
public function columnControlTitle(null|string|array $title = null, int|string $target = 1): static
101+
{
102+
if (is_array($title)) {
103+
$this->addColumnControl($target, [['extend' => 'title', ...$title]]);
104+
} else {
105+
$this->addColumnControl($target, [['extend' => 'title', 'text' => $title]]);
106+
}
107+
108+
return $this;
109+
}
110+
}

0 commit comments

Comments
 (0)