/** * @throws \yii\base\InvalidConfigException */ public function init() { parent::init(); $value = $this->hasModel() ? Html::getAttributeValue($this->model, $this->attribute) : $this->value; $this->momentDatetimeFormat = $this->momentDatetimeFormat ?: ArrayHelper::getValue($this->getPhpMomentMappings(), $this->phpDatetimeFormat); if (!$this->momentDatetimeFormat) { throw new InvalidConfigException('Please set momentjs datetime format'); } // Init default clientOptions $this->clientOptions = ArrayHelper::merge(['useCurrent' => true, 'locale' => $this->locale ?: substr(Yii::$app->language, 0, 2), 'format' => $this->momentDatetimeFormat], $this->clientOptions); // Init default options $this->options = ArrayHelper::merge(['class' => 'form-control'], $this->options); if ($value !== null) { $this->options['value'] = array_key_exists('value', $this->options) ? $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})"); }
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)); } }