Problem with datetimepicker in a sun

0

Good morning friends I have the following problem with a datetimepicker in a dynamic form that increases, the problem is that the first row is displayed on the datetimepicker per if I give the button more to clone the row above the second datetimepicker not me unfold the calendar

Html                          Start date:                                                                                              

        <div class="col-xs-5">   
        <label for="exampleInputEmail1">Fecha Fin:</label>
        <div class='input-group date' id='datetimepicker2'>
        <input type="text" name="fecha_final[]"  class="form-control" />
        <span class="input-group-addon">
        <span class="glyphicon glyphicon-calendar"></span>
        </span>
            </div>
            </div>
          </td>

this is a function of datetimepicker

    <script type="text/javascript">
       $(function () {
            $('#datetimepicker1').datetimepicker({
                defaultDate: "<?php echo date('Y-m-d-hh') ?>",

               $('#datetimepicker2').datetimepicker({
                defaultDate: "<?php echo date('Y-m-d-hh') ?>",


            });

        });
    </script>

and finally the javascript that increases the fields of the form

<script>

        $(function(){

        // Clona la fila oculta que tiene los campos base, y la agrega al final de la tabla
        $("#adicional").on('click', function(){
          $("#tabla tbody tr:eq(0)").clone().removeClass('fila-fija').appendTo("#tabla");
        });




        // Evento que selecciona la fila y la elimina 
        $(document).on("click",".eliminar",function(){
          var parent = $(this).parents().get(0);
          $(parent).remove();
        });
     });
    </script>
    
asked by Alexis Lozada 22.10.2017 в 16:13
source

1 answer

1

When you clone an element, the datepicker event is not cloned with it so you will have to indicate to the cloned element that it is a datepicker by applying the function datetimepicker() again on the element that has the class .date :

$(function () {
   $('.date').datetimepicker();
   
   
   $("button").click(function(){
   
   // clonamos el elemento que tiene el datepicker
    var clon = $('.form-group:first').clone();
    
    // buscamos el elemento con la clase .date para aplicarle el datetimepicker
    clon.find(".date").datetimepicker();
    
    // agregamos el picker a la lista de los calendarios
    $("#calendars").append(clon);
    
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" media="screen" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
<link href="//cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/e8bddc60e73c1ec2475f827be36e1957af72e2ea/build/css/bootstrap-datetimepicker.css" rel="stylesheet">

<script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
			<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.js"></script>
			<script src="//cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/e8bddc60e73c1ec2475f827be36e1957af72e2ea/src/js/bootstrap-datetimepicker.js"></script>
      
<div class="container">
    <div class="row">
        <div id='calendars' class='col-sm-6'>
            <div class="form-group">
                <div class='input-group date' id='datetimepicker1'>
                    <input type='text' class="form-control" />
                    <span class="input-group-addon">
                        <span class="glyphicon glyphicon-calendar"></span>
                    </span>
                </div>
            </div>
        </div>
        
        <button>Generar nuevo datepicker</button>
        <script type="text/javascript">
            
        </script>
    </div>
</div>
    
answered by 22.10.2017 / 17:20
source