Ver Mensaje Individual
  #5 (permalink)  
Antiguo 05/08/2016, 05:34
Avatar de IsaBelM
IsaBelM
Colaborador
 
Fecha de Ingreso: junio-2008
Mensajes: 5.032
Antigüedad: 15 años, 10 meses
Puntos: 1012
Respuesta: Crear de calendario dinámico

Cita:
Iniciado por dardosmania Ver Mensaje
.... Un simple DatePiker de Bootstrap que te permite hacer cosas maravillosas con pocas lineas de html y si quieres hacer cosas dinamicas se puede manejar desde JQuery con pocas lineas de codigo y como ese hay muchos
cuanto consideras pocas líneas?? 2000 líneas??

uno que realicé hace años
Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  4. <style type="text/css">
  5. .c_calendario_general {
  6.     width: 400px;
  7.     height: auto;
  8.     border: 1px solid rgb(195, 192, 192);
  9.     z-index: 1;
  10. }
  11.  
  12.  
  13. .calendario{
  14.     width: 100%;
  15.     height: auto;
  16.     border-collapse: collapse;
  17. }
  18.  
  19.  
  20. .diasemana > th {
  21.     border-bottom: 1px solid rgb(195, 192, 192);
  22. }
  23.  
  24.  
  25. tr.controles > th {
  26.     cursor: default;
  27. }
  28.  
  29.  
  30. .Ccasilladia {
  31.     width: 38px;
  32.     height: 19px;
  33.     cursor: default;
  34.     text-align: center;
  35. }
  36. <script type="text/javascript">
  37. var calendario = {
  38.  
  39.     meses : ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'],
  40.     diasemana : ['Lun', 'Mar', 'Mié', 'Jue', 'Vie', 'Sáb', 'Dom'],
  41.     fechaHoy : null,
  42.     cTabla : null,
  43.     tBody : null,
  44.     formatoFecha : 'dd/mm/yyyy',
  45.  
  46.  
  47.     cabCalendario : function() {
  48.  
  49.         var fecha = new Date();
  50.         calendario.fechaHoy = fecha;
  51.  
  52.         var objcalendario = document.querySelectorAll('.c_calendario_general');
  53.  
  54.         calendario.cTabla = document.createElement('table');
  55.         calendario.cTabla.setAttribute('class', 'calendario');
  56.         var tHead = document.createElement('thead');
  57.         var cTHead = calendario.cTabla.appendChild(tHead);
  58.         var trH1 = document.createElement('tr');
  59.         trH1.setAttribute('class', 'controles');
  60.         var cTrHead = cTHead.appendChild(trH1);
  61.         var thH1 = document.createElement('th');
  62.         thH1.setAttribute('class', 'atrasy');
  63.         thH1.setAttribute('colspan', '1');
  64.         thH1.setAttribute('title', 'Retroceder un año');
  65.         thH1.textContent = '<<';
  66.         trH1.appendChild(thH1);
  67.         var thH2 = document.createElement('th');
  68.         thH2.setAttribute('class', 'atrasm');
  69.         thH2.setAttribute('colspan', '1');
  70.         thH2.setAttribute('title', 'Retroceder un mes');
  71.         thH2.textContent = '<';
  72.         trH1.appendChild(thH2);
  73.         var thH3 = document.createElement('th');
  74.         thH3.setAttribute('class', 'myyyy');
  75.         thH3.setAttribute('colspan', '3');
  76.         thH3.textContent = calendario.meses[calendario.fechaHoy.getMonth()] + ', ' + calendario.fechaHoy.getFullYear();
  77.         trH1.appendChild(thH3);
  78.         var thH4 = document.createElement('th');
  79.         thH4.setAttribute('class', 'adelantem');
  80.         thH4.setAttribute('colspan', '1');
  81.         thH4.setAttribute('title', 'Avanzar un mes');
  82.         thH4.textContent = '>';
  83.         trH1.appendChild(thH4);
  84.         var thH5 = document.createElement('th');
  85.         thH5.setAttribute('class', 'adelantey');
  86.         thH5.setAttribute('colspan', '1');
  87.         thH5.setAttribute('title', 'Avanzar un año');
  88.         thH5.textContent = '>>';
  89.         trH1.appendChild(thH5);
  90.         var trH2 = document.createElement('tr');
  91.         trH2.setAttribute('class', 'diasemana');
  92.         var cTrHead = cTHead.appendChild(trH2);
  93.  
  94.         for (var i = 0; i < 7; i++) {
  95.  
  96.             var thH = document.createElement('th');
  97.             thH.textContent = calendario.diasemana[i];
  98.             trH2.appendChild(thH);
  99.         }
  100.  
  101.  
  102.         objcalendario[0].appendChild(calendario.cTabla);
  103.  
  104.         calendario.cuerpoCalendario();
  105.  
  106.         document.querySelector('th.atrasy').addEventListener('click', function(){calendario.retrocede(false)}, false);
  107.         document.querySelector('th.atrasm').addEventListener('click', function(){calendario.retrocede(true)}, false);
  108.         document.querySelector('th.adelantey').addEventListener('click', function(){calendario.avanza(false)}, false);
  109.         document.querySelector('th.adelantem').addEventListener('click', function(){calendario.avanza(true)}, false);
  110.     },
  111.  
  112.  
  113.  
  114.  
  115.     cuerpoCalendario : function() {
  116.  
  117.         calendario.cBody = document.createElement('tbody');
  118.  
  119.     //variable fechaaux a modo de fecha auxiliar, la situo el dia 1 del mes y año actual
  120.         var fechaaux = new Date(calendario.fechaHoy.getFullYear(), calendario.fechaHoy.getMonth(), 1);
  121.     //saco el dia de la semana que es el 1 del mes actual
  122.         diaactual = fechaaux.getDay();
  123.     //y voy retrasando la fecha de dia en dia hasta que llego a un lunes, una vez llegue a este lunes, sera la primera fecha que se pinte en el calendario
  124.         while (diaactual != 1) {
  125.  
  126.             fechaaux.setDate(fechaaux.getDate() - 1);
  127.             diaactual = fechaaux.getDay();
  128.         }
  129.  
  130.     // creamos los dias en el calendario, pintamos el dia que contiene fechaaux y la pasamos al dia siguiente
  131.         for (var i = 0; i < 6; i++) {
  132.  
  133.             var trB = document.createElement('tr');
  134.             var cTrBody = calendario.cBody.appendChild(trB);
  135.  
  136.             for (var j = 0; j < 7; j++) {
  137.  
  138.                 if (calendario.fechaHoy.getMonth() == fechaaux.getMonth()) {
  139.  
  140.                     var tdB = document.createElement('td');
  141.                     tdB.setAttribute('class', 'Ccasilladia');
  142.  
  143.                     if (calendario.fechaHoy.getDate() == fechaaux.getDate()) {
  144.  
  145.                         configcolor = 'color: rgb(255, 255, 255); background-color: rgb(116, 116, 117);';   //CASILLA DE HOY
  146.                         tdB.setAttribute('style', configcolor);
  147.  
  148.                     } else if ((fechaaux.getDay() == 6) || (fechaaux.getDay() == 0)) { //si el dia actual es sabado o domingo
  149.                                
  150.                         configcolor = 'color: rgb(255, 0, 0);';
  151.                         tdB.setAttribute('style', configcolor);
  152.  
  153.                     } else {
  154.  
  155.                         configcolor = '';
  156.                     }
  157.  
  158.                     tdB.textContent = fechaaux.getDate();
  159.                     var cTdBody = cTrBody.appendChild(tdB);
  160.                     tdB.addEventListener('click', function() {calendario.mostrarFecha(this.textContent)});
  161.  
  162.                 } else {
  163.                    
  164.                     var tdB = document.createElement('td');
  165.                     var cTdBody = cTrBody.appendChild(tdB);
  166.  
  167.                 }
  168.  
  169.                 fechaaux.setDate(fechaaux.getDate() + 1);
  170.             }
  171.        
  172.         }
  173.  
  174.         calendario.cTabla.appendChild(calendario.cBody);
  175.  
  176.     },
  177.  
  178.  
  179.  
  180.     avanza: function(aniomes) {
  181.  
  182.         if (!aniomes) {
  183.  
  184.             calendario.fechaHoy.setFullYear(calendario.fechaHoy.getFullYear() + 1);
  185.            
  186.        
  187.         } else {
  188.  
  189.             calendario.fechaHoy.setMonth(calendario.fechaHoy.getMonth() + 1);
  190.  
  191.         }
  192.  
  193.         document.querySelector('.myyyy').innerHTML = calendario.meses[calendario.fechaHoy.getMonth()] + ', ' + calendario.fechaHoy.getFullYear();
  194.  
  195.         calendario.cTabla.removeChild(calendario.cBody);
  196.  
  197.         calendario.cuerpoCalendario();
  198.     },
  199.  
  200.  
  201.  
  202.     retrocede: function(aniomes) {
  203.  
  204.         if (!aniomes) {
  205.  
  206.             calendario.fechaHoy.setFullYear(calendario.fechaHoy.getFullYear() - 1);
  207.  
  208.         } else {
  209.  
  210.             calendario.fechaHoy.setMonth(calendario.fechaHoy.getMonth() - 1);
  211.  
  212.         }
  213.  
  214.         document.querySelector('.myyyy').innerHTML = calendario.meses[calendario.fechaHoy.getMonth()] + ', ' + calendario.fechaHoy.getFullYear();
  215.  
  216.         calendario.cTabla.removeChild(calendario.cBody);
  217.  
  218.         calendario.cuerpoCalendario();
  219.     },
  220.  
  221.  
  222.  
  223.     mostrarFecha : function(dia) {
  224.  
  225.         var fecha = [calendario.fechaHoy.getFullYear(), calendario.fechaHoy.getMonth() + 1, dia];
  226.  
  227.         document.getElementById('fentrada').textContent = calendario.formatFecha(fecha, calendario.formatoFecha);
  228.     },
  229.  
  230.  
  231.  
  232.    formatFecha : function(arr, darformato) {
  233. //https://msdn.microsoft.com/library/ff743760(v=vs.94).aspx
  234.         darformato = darformato.replace(/(d+)/g, function(v) {
  235.  
  236.             return ((arr[2].length < 2 && v.length > 1 ? '0' : '') + arr[2]);
  237.         });
  238.  
  239.  
  240.         darformato = darformato.replace(/(m+)/g, function(v) {
  241.  
  242.             return ((arr[1].toString().length < 2 && v.length > 1 ? '0' : '') + arr[1]);
  243.         });
  244.  
  245.         return darformato.replace(/(y+)/g, function(v) {
  246.  
  247.             return arr[0].toString().slice(-v.length);
  248.         });
  249.  
  250.     }
  251.  
  252.  
  253. }
  254.  
  255.  
  256. document.addEventListener('DOMContentLoaded', calendario.cabCalendario, false);
  257. </head>
  258.  
  259.     <div class="c_calendario_general"></div>
  260.     <div id="fentrada"></div>
  261.  
  262. </body>
  263. </html>
__________________
if(ViolenciaDeGénero) {alert('MUJER ASESINADA');}

Última edición por IsaBelM; 05/08/2016 a las 14:49