如何在我的cakephp 3.4项目中使用bootstrap datetimepicker

时间:2017-06-27 15:16:36

标签: php cakephp cakephp-3.0 bootstrap-datetimepicker

我在huespedes / add.ctp中有这段代码:

<div class="form" >
<?= $this->Form->create($huespede) ?>
<fieldset>
    <legend><?= __('Añadir Huésped') ?></legend>

    <?php

    echo $this->Form->control('nombre',array('type'=>'text','class' => 'form-control'));
        echo $this->Form->control('apellidos',array('type'=>'text','class' => 'form-control'));
        echo $this->Form->control('pasaporte', array ('label'=>'Pasaporte o Carnet de Identidad', 'class' => 'form-control','id' => 'carnet'));
        ?>
        <br>
        <?php
        echo $this->Form->control('fechanac', ['label'=>'Fecha de Nacimiento   .', 'empty' => true]);
        echo $this->Form->control('paise_id', ['label'=>'País', 'options' => $paises, 'empty' => 'Seleccione el País', 'class' => 'form-control']);
        echo $this->Form->control('observaciones', array ('class' => 'form-control'));
        echo $this->Form->hidden('habitacione_id', ['label'=>'Habitación', 'options' => $habitaciones, 'empty' => true, 'class' => 'form-control']);
        echo $this->Form->control('categoria_id', ['label'=>'Categorias', 'options' => $categorias, 'empty' => true, 'class' => 'form-control']);
    ?>
</fieldset>
<br>
<?= $this->Form->button(__('Aceptar')) ?>
<?= $this->Form->end() ?>

我希望fechanac字段显示datetimepicker js和css,如示例页面...

3 个答案:

答案 0 :(得分:1)

首先加入all libreries

<?= $this->Html->css('bootstrap.min.css') ?>
<?= $this->Html->script('bootstrap.min.js') ?>

现在在代码中加入example

echo $this->Form->control('fechanac', ['label'=>'Fecha de Nacimiento   .', 'id' => 'datetimepicker1', 'empty' => true]);
 <script type="text/javascript">
            $(function () {
                $('#datetimepicker1').datetimepicker();
            });
        </script>

这应该有效。

答案 1 :(得分:0)

我假设您正在使用http://tarruda.github.io/bootstrap-datetimepicker/如果这是正确的,那么示例适用,否则您需要进行调整。您很可能还需要进行调整,但关键是利用options数组来传递数据格式等内容。您需要加载bootstrap twitter css和js。

您的目标是获得以下

<div class="well">
  <div id="datetimepicker1" class="input-append date">
    <input data-format="dd/MM/yyyy hh:mm:ss" type="text"></input>
    <span class="add-on">
      <i data-time-icon="icon-time" data-date-icon="icon-calendar">
      </i>
    </span>
  </div>
</div>
<script type="text/javascript">
  $(function() {
    $('#datetimepicker1').datetimepicker({
      language: 'pt-BR'
    });
  });
</script>

要实现此目的,您需要指定输入的类和其他选项。

<div class="well">
  <div id="datetime-picker-div" class="input-append date">
    echo $this->Form->control('fechanac', ['label'=>'Fecha de Nacimiento   .', 'empty' => true, 'data-format' => 'dd/MM/yyyy hh:mm:ss', 'type' => 'text']);
    <span class="add-on">
      <i data-time-icon="icon-time" data-date-icon="icon-calendar">
      </i>
    </span>
  </div>
</div>
<script type="text/javascript">
  $(function() {
    $('#datetimepicker1').datetimepicker({
      language: 'pt-BR'
    });
  });
</script>

答案 2 :(得分:0)

我想你可以使用这个例子:

<?= $this->Form->create($entity) ?>

<?= $this->Form->input('datetime_field' , [
    'type' => 'text',
    'class' => 'picker',
    'value' => $entity->datetime_field ? $entity->datetime_field->i18nFormat('y-MM-dd HH:mm:ss') : ''
]) ?>

<?= $this->Form->end(); ?>

然后初始化datetimepicker:

<script type="text/javascript">
;+function() {
    $('.picker').datetimepicker({
        format : 'YYYY-MM-DD HH:mm:ss'
    })
}();
</script>