Skip to content

Commit

Permalink
Merge pull request #8 from icron/7-add-support-js-events
Browse files Browse the repository at this point in the history
7 add support js events
  • Loading branch information
Eugene Terentev committed Feb 12, 2016
2 parents e70f9ee + 9ad5cbd commit d064d31
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,13 @@ Standalone widget for date only:
'phpDatetimeFormat' => 'yyyy-MM-dd',
]) ; ?>
```
Add custom JS events:
```php
<?php echo trntv\yii\datetime\DateTimeWidget::widget([
'clientEvents' => [
'dp.change' => 'function(e){
console.log('dp.change');
}',
],
]) ; ?>
```
25 changes: 22 additions & 3 deletions src/DateTimeWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -102,12 +106,27 @@ 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)) {
$js = [];
foreach ($this->clientEvents as $event => $handler) {
$js[] = "jQuery('#{$this->containerOptions['id']}').on('$event', $handler);";
}
$this->getView()->registerJs(implode("\n", $js));
}
}

/**
Expand Down

0 comments on commit d064d31

Please sign in to comment.