|
| 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