amsul / pickadate.js

The mobile-friendly, responsive, and lightweight jQuery date & time input picker.
http://amsul.ca/pickadate.js
MIT License
7.7k stars 1.01k forks source link

Disable dates by variable #1058

Closed edgrod01 closed 6 years ago

edgrod01 commented 6 years ago

hi! im trying to send the value for disable the dates by a variable, but no excecute nothing, i take the value and paste, this excecute fine. how can i do for send the variable. ej


while($datos=$rpta->fetch_object())
        {
            // $fecha.= '[2,3],[5,6],';
            $fecha.= '{from: ['.$datos->fecInicio.'], to: ['.$datos->fecFinal.']},';
        }

        $fecha.= '';
var $fecha = data.replace(/[-]/gi,',');

$('#fecha').pickadate({
                today: '',
                clear: '',
                close: '',
                format: 'yyyy-mm-dd',
                formatSubmit: 'yyyy-mm-dd',
                weekdaysShort: ['Dom', 'Lun', 'Mar', 'Mie' ,'Jue', 'Vie', 'Sab'],
                monthsShort: ['Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun', 'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dic'],
                showMonthsShort: true,
                selectMonths: true,
                selectYears: true,
                disable: [$fecha],
            });
  ----------- no print -----------------------------

$('#fecha').pickadate({
                today: '',
                clear: '',
                close: '',
                format: 'yyyy-mm-dd',
                formatSubmit: 'yyyy-mm-dd',
                weekdaysShort: ['Dom', 'Lun', 'Mar', 'Mie' ,'Jue', 'Vie', 'Sab'],
                monthsShort: ['Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun', 'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dic'],
                showMonthsShort: true,
                selectMonths: true,
                selectYears: true,
                disable: [{from: [2018,02,17], to: [2018,02,17]},{from: [2018,02,22], to: [2018,02,22]},{from: [2018,03,01], to: [2018,03,31]},{from: [2018,04,01], to: [2018,04,30]},{from: [2018,06,01], to: [2018,06,30]},{from: [2018,07,01], to: [2018,07,31]},],
            });
-------------- print ok ------------------------------

Thanks for your help

edgrod01 commented 6 years ago

hi, please help

DanielRuf commented 6 years ago

Did you try json_encode? A string is not an object. You can check this in JavaScript with typeof.

edgrod01 commented 6 years ago

hello, thank for your help, excuseme for my english im colombian. Also i new programming :)

these are the dates in my bd

CREATE TABLE `citprogagenda` (
  `ideCitProgAgenda` int(11) NOT NULL COMMENT 'LLAVE PRIMARIA',
  `ideGenHisEspecialidad` int(11) NOT NULL COMMENT 'IDE ESPECIALIDAD',
  `ideGenPerEmpleado` int(11) NOT NULL COMMENT 'ID MÉDICO',
  `ideGenUbicacion` int(11) NOT NULL COMMENT 'ID CONSULTORIO',
  `fecInicio` date NOT NULL COMMENT 'FECHA INICIO DE AGENDA',
  `fecFinal` date NOT NULL COMMENT 'FECHA FIN DE AGENDA',
  `fecInactiva` text NOT NULL COMMENT 'FECHAS INACTIVAS',
  `ideGenIntervalo` int(11) NOT NULL COMMENT 'IDE INTERVALO',
  `indActivo` int(1) NOT NULL DEFAULT '1' COMMENT 'INDICADOR DE ACTIVO'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
----------------------
INSERT INTO `citprogagenda` (`ideCitProgAgenda`, `ideGenHisEspecialidad`, `ideGenPerEmpleado`, `ideGenUbicacion`, `fecInicio`, `fecFinal`, `fecInactiva`, `ideGenIntervalo`, `indActivo`) VALUES
(7, 1, 3, 1, '2018-02-22', '2018-02-28', '', 48, 1),
(8, 1, 3, 1, '2018-03-22', '2018-03-28', '', 48, 1);

im trayin send the values from php to JS, this is te code in php

case 'agenda':
        $rpta= $citAgenda->agenda($ideGenPerEmpleado, $ideGenUbicacion);
        $fecha= array();
        while($datos=$rpta->fetch_object())
        {
            array_push($fecha,'{from: ['.$datos->fecInicio.'], to: ['.$datos->fecFinal.']}');
        }
        echo json_encode($fecha);
break;

-------------- and here recieve in JS to send to pickdate

$("#ideGenUbicacion").on("change", function(e)
    {
        $.post("../ajax/citAgenda.php?op=agenda",{ideGenPerEmpleado: $("#ideGenPerEmpleado").val(), ideGenUbicacion:  $("#ideGenUbicacion").val()   }, function(data, status)
        {
            console.log(data);
            console.log(data.length);
            data= data.replace(/[-]/gi,',');

            data = JSON.parse(data);
            console.log(data);

            var inputpick = $('#fecha').pickadate();
                var pick = inputpick.pickadate('picker');
                console.log (pick.set('disable', [data] ));
        });
    }); 

-------------in the console show this in the constructor

item:
clear:null
disable:Array(1)
0:(2) ["{from: [2018,02,22], to: [2018,02,28]}", "{from: [2018,03,22], to: [2018,03,28]}"]
length:1
edgrod01 commented 6 years ago

hi Daniel, could you help me, thanks

edgrod01 commented 6 years ago

hia Daniel, after i trying, i get it, thanks for you help, look:

$.post("../ajax/citAgenda.php?op=agenda",{ideGenPerEmpleado: $("#ideGenPerEmpleado").val(), ideGenUbicacion: $("#ideGenUbicacion").val() }, function(data, status) {

        // console.log(data);
        // console.log(data.length);
        data= data.replace(/[-]/gi,',');
        data= data.replace(/[""]/gi,'');
        // console.log(eval(data));

        // var inputpick = $('#fecha').pickadate();
        // var pick = inputpick.pickadate('picker');
        // console.log(pick.set('disable', eval(data)));

        console.log( $('#fecha').pickadate({
            today: '',
            clear: '',
            close: '',
            format: 'yyyy-mm-dd',
            formatSubmit: 'yyyy-mm-dd',
            weekdaysShort: ['Dom', 'Lun', 'Mar', 'Mie' ,'Jue', 'Vie', 'Sab'],
            monthsShort: ['Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun', 'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dic'],
            showMonthsShort: true,
            selectMonths: true,
            selectYears: true,
            disable: eval(data)
        }));
    }); 
DanielRuf commented 6 years ago

eval should be normally avoided. So this is solved for you?

edgrod01 commented 6 years ago

hmmm, thanks for answer, really i new programming, don't know why avoid eval, could you explain me... thank

and its rigth, with just use it, solved my problem, its looking disabled the dates.

But now i get other problem, how can i do for january by the number 1 no 0

thanks

DanielRuf commented 6 years ago

0 is January in JS. Just substract 1 from your month string.

eval is a security problem.

edgrod01 commented 6 years ago

thanks for your help, i solved the problem, in php i rest 1 to the month

thanks

DanielRuf commented 6 years ago

Okay, thanks for the feedback.