1: <?php
2:
3: declare(strict_types=1);
4:
5: namespace Atk4\Ui\Table\Column;
6:
7: use Atk4\Data\Field;
8: use Atk4\Ui\Exception;
9: use Atk4\Ui\Js\Jquery;
10: use Atk4\Ui\Js\JsExpression;
11: use Atk4\Ui\Js\JsExpressionable;
12: use Atk4\Ui\Table;
13:
14: /**
15: * Implements Checkbox column for selecting rows.
16: */
17: class Checkbox extends Table\Column
18: {
19: /** @var string */
20: public $class;
21:
22: /**
23: * Return action which will calculate and return array of all Checkbox IDs, e.g.
24: *
25: * [3, 5, 20]
26: */
27: public function jsChecked(): JsExpressionable
28: {
29: return (new Jquery($this->table))->find('.checked.' . $this->class)->closest('tr')
30: ->map(new \Atk4\Ui\Js\JsFunction([], [new JsExpression('return $(this).data(\'id\')')]))
31: ->get()->join(',');
32: }
33:
34: #[\Override]
35: protected function init(): void
36: {
37: parent::init();
38:
39: if (!$this->class) {
40: $this->class = 'cb_' . $this->shortName;
41: }
42: }
43:
44: #[\Override]
45: public function getHeaderCellHtml(Field $field = null, $value = null): string
46: {
47: if ($field !== null) {
48: throw (new Exception('Checkbox must be placed in an empty column, don\'t specify any field'))
49: ->addMoreInfo('field', $field);
50: }
51: $this->table->js(true)->find('.' . $this->class)->checkbox();
52:
53: return parent::getHeaderCellHtml($field);
54: }
55:
56: #[\Override]
57: public function getDataCellTemplate(Field $field = null): string
58: {
59: return $this->getApp()->getTag('div', ['class' => 'ui checkbox ' . $this->class], [['input/', ['type' => 'checkbox']]]);
60: }
61: }
62: