Skip Navigation

[Cerrado] disabling dates in the datepicker

This support ticket is created hace 3 años, 4 meses. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Este tema contiene 11 respuestas, tiene 3 mensajes.

Última actualización por Luo Yang hace 3 años, 4 meses.

Asistido por: Luo Yang.

Autor
Mensajes
#2115869

I have a lot of different date pickers and I need to be able to disable dates (or more accurately only allow dates based on a list [ from backend ajax])

I have tried :

var array = ["2021-07-14","2021-07-15","2021-07-16"]
$('input.hasDatepicker').datepicker({
beforeShowDay: function(date){
var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
return [ array.indexOf(string) == -1 ]
}
});

which should do the inverse of what I need, but nothing.

I have tried:
$( "input.hasDatepicker" ).datepicker({ minDate: -20, maxDate: "+1M +10D" });
but also does nothing.

I have even tried:
jQuery(document).ready(function(){
jQuery('input.hasDatepicker').datepicker('option','onClose',function(){
console.log('close');
});
jQuery('input.hasDatepicker').datepicker('option','onSelect',function(){
console.log('select');
});
});

from https://toolset.com/forums/topic/datepicker-change-event/ still nothing...

#2116149

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As I understand - you want to disable the selection of specific dates on the date picker?

If yes, can you please share admin access details as well as on what page/post you added the datepicker field?

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2117103

I appreciate you are trying to help in the easiest way possible.
Should the code I've been trying work? Is what I'm trying to do possible?
Please provide code, there is no need for admin access.

#2118547

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Here is the sample code that shows how you can disable specific dates using jQuery date picker:
- https://stackoverflow.com/questions/9742289/jquery-ui-date-picker-disabling-specific-dates

You will have to target your field using CSS ID or CSS class if you do not know how to do it, that is why I will require admin access details so that I can review where you added the date picker field on what page/post and what dates you would like to hide.

You should try it first on your own and let me know if you will require further assistance.

#2120799

Can you please answer some of the questions I have asked.
- Should the code I've been trying work?
- Is what I'm trying to do possible?

One of the code samples was from a Toolset post (as linked) but the jQuery('input.hasDatepicker') selector doesn't seem to work. (is present in the html).

Have you tried the code you are suggesting?

I have previously tried that code and again now:

                  
var unavailableDates = ["20-7-2021", "22-7-2021", "15-7-2021"];

    function unavailable(date) {
        dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
        if (jQuery.inArray(dmy, unavailableDates) == -1) {
            return [true, ""];
        } else {
            return [false, "", "Unavailable"];
        }
    }
    jQuery(function() {
        jQuery('input[name="testing[display-only]"]').datepicker({
            dateFormat: 'dd MM yy',
          	beforeShowDay: unavailable
        });

      jQuery("#cred_form_6903_1_testingdatepicker").attr('type', 'text');

    });

When applied these edits work, but they prevent the hidden field from being populated.

#2120845

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

I cant say anything until I check where exactly you are looking to apply the above code as there are lots of possibilities that code should not run as expected due to conflicts or as you shared even if code applied it could not work.

That is why its important for me to look at where exactly you are trying to apply the code and once I review that I'll be able to guide you in the right direction.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2121775

Please refer this ticket to a different staff member.

#2121985
datepicker2.JPG

Hello,

I assume we are talking about a custom date field of Toolset post form.

If it is, you can try the JS event "cred_form_ready", for example:

var unavailableDates = ["20-7-2021", "22-7-2021", "15-7-2021"];
 
function unavailable(date) {
	dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
	if (jQuery.inArray(dmy, unavailableDates) == -1) {
		return [true, ""];
	} else {
		return [false, "", "Unavailable"];
	}
}
jQuery(document).on('cred_form_ready', function(){
	jQuery('input[name="testing[display-only]"]').datepicker({
		dateFormat: 'dd MM yy',
		beforeShowDay: unavailable
	});
});

I have tried it in my localhost, it works fine, see my screenshot datepicker2.JPG

#2122103

Thank Luo,

Exactly that, for me it displays correctly with or without the

(document).on('cred_form_ready',

But as you will see if you add the

jQuery("#cred_form_6903_1_testingdatepicker").attr('type', 'text');

The hidden field is never populated and so data is never saved to the datebase!

I have just change the field from a generic field to a custom field to prove the point and definitely no data is saved.

                  
var unavailableDates = ["20-7-2021", "22-7-2021", "15-7-2021"];

    function unavailable(date) {
        dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
        if (jQuery.inArray(dmy, unavailableDates) == -1) {
            return [true, ""];
        } else {
            return [false, "", "Unavailable"];
        }
    }
    jQuery(document).on('cred_form_ready', function(){
        jQuery('input[name="wpcf-date-c9c197b4[display-only]"]').datepicker({
            dateFormat: 'dd MM yy',
          	beforeShowDay: unavailable
        });

      jQuery('input[name="wpcf-date-c9c197b4[datepicker]"]').attr('type', 'text');

    });
[cred_field field='date-c9c197b4' force_type='field' class='form-control' output='bootstrap']
#2123041

You need to set the hidden date field "wpcf-date-c9c197b4[datepicker]" as timestamp value, for example, you can modify your JS codes as below:

var unavailableDates = ["20-7-2021", "22-7-2021", "15-7-2021"];
 
function unavailable(date) {
	dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
	if (jQuery.inArray(dmy, unavailableDates) == -1) {
		return [true, ''];
	} else {
		return [false, "", "Unavailable"];
	}
}
jQuery(document).on('cred_form_ready', function(){
	jQuery( 'input[name="wpcf-date-c9c197b4[display-only]"]' ).datepicker({
		dateFormat: 'dd MM yy',
		beforeShowDay: unavailable,
      	onSelect: function(mydate){
          jQuery('input[name="wpcf-date-c9c197b4[datepicker]"]').val(Date.parse(mydate)/1000);
        }
	});
});

More help:
enlace oculto

#2123831

I see so rather than changing or updating the toolset datepicker instance, this is creating a new one and therefore this function needs to be added back in?

#2124847

Above JS codes works fine in my localhost, since it is a custom JS codes problem, if you need more assistance for it, please provide a test site with the same problem, also point out the problem page URL and form URL, I need a live website to test and debug.

El debate ‘[Cerrado] disabling dates in the datepicker’ está cerrado y no admite más respuestas.