From a55c2e17fb1bc0993d8515abbd2939ebaf5525fc Mon Sep 17 00:00:00 2001 From: Alexander Lukyanchikov Date: Thu, 11 Feb 2016 17:11:17 +0400 Subject: [PATCH 1/3] #7 Added support js events --- src/DateTimeWidget.php | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/DateTimeWidget.php b/src/DateTimeWidget.php index 1a16f39..874799d 100755 --- a/src/DateTimeWidget.php +++ b/src/DateTimeWidget.php @@ -21,6 +21,10 @@ class DateTimeWidget extends InputWidget * @link http://eonasdan.github.io/bootstrap-datetimepicker/#options */ public $clientOptions = []; + /** + * @var array the event handlers for the underlying bootstrap-datetimepicker plugin. + */ + public $clientEvents = []; /** * @var array */ @@ -102,12 +106,28 @@ public function init() ? $this->options['value'] : Yii::$app->formatter->asDatetime($value, $this->phpDatetimeFormat); } - DateTimeAsset::register($this->getView()); - $clientOptions = Json::encode($this->clientOptions); + if (!isset($this->containerOptions['id'])) { $this->containerOptions['id'] = $this->getId(); } - $this->view->registerJs("$('#{$this->containerOptions['id']}').datetimepicker({$clientOptions})"); + + $this->registerJs(); + } + + protected function registerJs() + { + DateTimeAsset::register($this->getView()); + $clientOptions = Json::encode($this->clientOptions); + $this->getView()->registerJs("$('#{$this->containerOptions['id']}').datetimepicker({$clientOptions})"); + + if (!empty($this->clientEvents)) { + $id = $this->getId(); + $js = []; + foreach ($this->clientEvents as $event => $handler) { + $js[] = "jQuery('#$id').on('$event', $handler);"; + } + $this->getView()->registerJs(implode("\n", $js)); + } } /** From 32b1f638bf4c568e02ccf243026d977ba79f8b1e Mon Sep 17 00:00:00 2001 From: Alexander Lukyanchikov Date: Thu, 11 Feb 2016 17:19:17 +0400 Subject: [PATCH 2/3] #7 Added support js events DOC --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 7c48d74..6614a3e 100644 --- a/README.md +++ b/README.md @@ -103,3 +103,13 @@ Standalone widget for date only: 'phpDatetimeFormat' => 'yyyy-MM-dd', ]) ; ?> ``` +Add custom JS events: +```php + [ + 'dp.change' => 'function(e){ + console.log('dp.change'); + }', + ], +]) ; ?> +``` From 9ad5cbd62856cfbdc5dd6b9f55fd3985510d93bd Mon Sep 17 00:00:00 2001 From: Alexander Lukyanchikov Date: Thu, 11 Feb 2016 18:06:47 +0400 Subject: [PATCH 3/3] #7 Fix id --- src/DateTimeWidget.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/DateTimeWidget.php b/src/DateTimeWidget.php index 874799d..82990d8 100755 --- a/src/DateTimeWidget.php +++ b/src/DateTimeWidget.php @@ -121,10 +121,9 @@ protected function registerJs() $this->getView()->registerJs("$('#{$this->containerOptions['id']}').datetimepicker({$clientOptions})"); if (!empty($this->clientEvents)) { - $id = $this->getId(); $js = []; foreach ($this->clientEvents as $event => $handler) { - $js[] = "jQuery('#$id').on('$event', $handler);"; + $js[] = "jQuery('#{$this->containerOptions['id']}').on('$event', $handler);"; } $this->getView()->registerJs(implode("\n", $js)); }