سؤال

I use 'DateTimePicker' (http://xdsoft.net/jqplugins/datetimepicker/) on my date fields.

I would to manage different options on my date fields.

By instance:

  • field "Birthdate" : allowBlank = false, format = 'Y-m-d'
  • field "appointmentdate" : allowBlank = true, format = 'd-m-Y'

my javascript is

jQuery(document).on('ready page:change', function() {

    var datefield = jQuery("[data-behaviour~='my-datepicker']");
    datefield.datetimepicker({
        format: datefield.attr('data-dateformat') || 'Y-m-d',
        timepicker:false,
        allowBlank: datefield.attr('data-allowblank'),
        lang: datefield.attr('data-lang') || 'en'
    });

});

My problem is all 'options' (allowblank, format) are sames for all dates fields !

I can't make my datetimepicker function apply only on the field which has the option (allowblank=true only for 'appointmentdate'...).

هل كانت مفيدة؟

المحلول

You could do it as follows:

jQuery(document).on('ready page:change', function() {

    var datefields = jQuery("[data-behaviour~='my-datepicker']");

    datefields.each(function(){
        $(this).datetimepicker({
            format: $(this).attr('data-dateformat') || ($(this).attr('data-allowblank') ? 'd-m-Y' : 'Y-m-d'),
            timepicker:false,
            allowBlank: $(this).attr('data-allowblank'),
            lang: $(this).attr('data-lang') || 'en'
        });
    });

});

Or simply specify it in the data-dateformat attribute in the HTML code.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top