|
19 | 19 | use Omines\DataTablesBundle\Exception\InvalidArgumentException; |
20 | 20 | use Omines\DataTablesBundle\Exception\InvalidConfigurationException; |
21 | 21 | use Omines\DataTablesBundle\Exception\InvalidStateException; |
| 22 | +use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
22 | 23 | use Symfony\Component\HttpFoundation\JsonResponse; |
23 | 24 | use Symfony\Component\HttpFoundation\Request; |
24 | 25 | use Symfony\Component\OptionsResolver\OptionsResolver; |
@@ -65,6 +66,9 @@ class DataTable |
65 | 66 | /** @var array<string, AbstractColumn> */ |
66 | 67 | protected $columnsByName = []; |
67 | 68 |
|
| 69 | + /** @var EventDispatcherInterface */ |
| 70 | + protected $eventDispatcher; |
| 71 | + |
68 | 72 | /** @var string */ |
69 | 73 | protected $method = Request::METHOD_POST; |
70 | 74 |
|
@@ -104,11 +108,14 @@ class DataTable |
104 | 108 | /** |
105 | 109 | * DataTable constructor. |
106 | 110 | * |
| 111 | + * @param EventDispatcherInterface $eventDispatcher |
107 | 112 | * @param array $options |
108 | 113 | * @param Instantiator|null $instantiator |
109 | 114 | */ |
110 | | - public function __construct(array $options = [], Instantiator $instantiator = null) |
| 115 | + public function __construct(EventDispatcherInterface $eventDispatcher, array $options = [], Instantiator $instantiator = null) |
111 | 116 | { |
| 117 | + $this->eventDispatcher = $eventDispatcher; |
| 118 | + |
112 | 119 | $this->instantiator = $instantiator ?? new Instantiator(); |
113 | 120 |
|
114 | 121 | $resolver = new OptionsResolver(); |
@@ -138,6 +145,24 @@ public function add(string $name, string $type, array $options = []) |
138 | 145 | return $this; |
139 | 146 | } |
140 | 147 |
|
| 148 | + /** |
| 149 | + * Adds an event listener to an event on this DataTable. |
| 150 | + * |
| 151 | + * @param string $eventName The name of the event to listen to |
| 152 | + * @param callable $listener The listener to execute |
| 153 | + * @param int $priority The priority of the listener. Listeners |
| 154 | + * with a higher priority are called before |
| 155 | + * listeners with a lower priority. |
| 156 | + * |
| 157 | + * @return $this |
| 158 | + */ |
| 159 | + public function addEventListener(string $eventName, callable $listener, int $priority = 0): self |
| 160 | + { |
| 161 | + $this->eventDispatcher->addListener($eventName, $listener, $priority); |
| 162 | + |
| 163 | + return $this; |
| 164 | + } |
| 165 | + |
141 | 166 | /** |
142 | 167 | * @param int|string|AbstractColumn $column |
143 | 168 | * @param string $direction |
@@ -204,6 +229,14 @@ public function getColumns(): array |
204 | 229 | return $this->columns; |
205 | 230 | } |
206 | 231 |
|
| 232 | + /** |
| 233 | + * @return EventDispatcherInterface |
| 234 | + */ |
| 235 | + public function getEventDispatcher(): EventDispatcherInterface |
| 236 | + { |
| 237 | + return $this->eventDispatcher; |
| 238 | + } |
| 239 | + |
207 | 240 | /** |
208 | 241 | * @return bool |
209 | 242 | */ |
|
0 commit comments