0

I have an api which is called using ajax (getJSON). Currently it works perfectly if I want to append to a defined div, namely 'open'.

However, what I really want to do is update the static values in my javascript for the datetime picker, specifically the minTime and maxTime.

My code is as below:

AJAX

$.getJSON("{{ url('api/openingtimes')}}", { option: $(this).val() }, 
    function(data) {
        if (data.success != false ) { 
            $('#open').empty();
            $.each(data, function(key, value) {
                $('#open').append('<div>' + value.weekday_start_time +' ' + value.weekday_end_time +'</div>');
            });
        }
}); 

JAVASCRIPT datetimepicker

var logic = function( currentDateTime ){
    if( currentDateTime.getDay()==6 ){  
        this.setOptions({
            minTime:'10:00',
            maxTime:'21:00'
        });
    } else
        this.setOptions({
            minTime:'08:00',
            maxTime:'22:00'
        });
    };

    jQuery('#start_date_time').datetimepicker({
        onChangeDateTime:logic,
        onShow:logic
    });               
});

API (JSON)

{
    "weekday_start_time": "08:00",
    "weekday_end_time": "22:00",
    "weekend_start_time": "10:00",
    "weekend_end_time": "21:00"
}

I'm struggling to see how this can work via my JSON feed. If anyone can advise, I would be very grateful.

2
  • Your JSON is a little weird as it should probably have quotation marks around the times. Is that just a typing mistake? Commented Nov 12, 2014 at 23:20
  • Perhaps there should be quotations marks around the times. Commented Nov 12, 2014 at 23:30

1 Answer 1

1

Okay, I got it. Just do it like this:

$.getJSON("{{ url('api/openingtimes')}}", { option: $(this).val() }, function(data) {
    if (data.success != false ) { 
        jQuery('#start_date_time').datetimepicker({
            minTime: value.weekday_start_time,
            maxTime: value.weekday_end_time
        });
    }
});

When you call the datetimepicker again using an object as the parameter, it knows you want to set the options.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm assuming that you're using this plugin, as there are several out there with the same name: xdsoft.net/jqplugins/datetimepicker
Thank you for input, however I'm unsure how this appends it to datetimepicker function. I tried your code but nothing happened.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.