Search code examples
jquerydatetimepicker

timepicker disableTimeRanges


I have a timepicker that works just fine except when I am trying to disableTimeRanges. I have some code that assembles this string:

var disableTimeRanges = '[';
var count = jsonObject.length;
var i = 0;
$.each(jsonObject, function (i, obj) {
  disableTimeRanges += "['" + obj.appointmentStart.split(" ").slice(1, 3).join('') + "','" + obj.appointmentEnd.split(" ").slice(1, 3).join('') + "']";
  i++;
  if (i < count)
      disableTimeRanges += ',';
});// end $.each
disableTimeRanges += ']';

at this point, disableTimeRanges = [['11:30:00AM','12:00:00PM'],['12:30:00PM','1:30:00PM']]

Then,

$('#appointmentTimeTextbox').timepicker('option', {
    'disableTimeRanges': disableTimeRanges
 });

I know I am doing something wrong because the chrome debugger is giving me:

Uncaught TypeError: b.disableTimeRanges.sort is not a function

Is there a problem with the array? Is there a better way to assemble the array?

Edit: fixed as an array:

var disableTimeRanges = new Array();
var appointmentTime = [obj.appointmentStart.split(" ").slice(1, 3).join(''), 
obj.appointmentEnd.split(" ").slice(1, 3).join('')];
disableTimeRanges.push(appointmentTime);

Solution

  • disableTimeRanges is not an array in your code snippet, it's a string. Something like this should do:

    var disableTimeRanges = [];
    var count = jsonObject.length;
    
    $.each(jsonObject, function (i, obj) {
       inRange = []
      inRange.push(obj.appointmentStart.split(" ").slice(1, 3).join(''));
      inRange.push(obj.appointmentEnd.split(" ").slice(1, 3).join(''));
      disableTimeRanges.push(inRange);
    });