Min Max values not setting correctly with JQuery Slider

Viewed 135

I have created the following fiddle which demonstrates my use of the JQuery slider.

My problem is the min and max values of the slider do not appear to be getting set correctly and the range markers are thus incorrectly positioned.

I am currently trying to set the min value as the first (earliest) date in the date array and the max value as the last (latest) date in the array.

I have debugged and can see that the variables are getting set correctly however on running the range markers are both at the left of the range.

Also running the do it button twice causes it to reload and again set them incorrectly. Where have I gone wrong?

Loading the range

function loadRange (dates) {

        dates = $.parseJSON(dates);

    'use strict';

        var convertedArray = [];

        for (var index = 0; index < dates.length; ++index) {
            var converted;
            if (index === 0) {
                converted = (new Date(dates[index]).getTime() - 86400000) / 1000;
            } else {
                converted = (new Date(dates[index]).getTime() + 86400000) / 1000;
            }
            convertedArray.push(converted);
        }

        var min = new Date(convertedArray[0] * 1000).toISOString().slice(0, 10);
        var max = new Date(convertedArray[convertedArray.length - 1] * 1000).toISOString().slice(0, 10);

        var minDate = min;
        var maxDate = max;

        $("#slider-container").slider({
            range: true,
            min: convertedArray[0],
            max: convertedArray[convertedArray.length - 1],
            step: 864,
            values: convertedArray,
            slide: function (event, ui) {
                var min = new Date(ui.values[0] * 1000).toISOString().slice(0, 10);
                var max = new Date(ui.values[ui.values.length - 1] * 1000).toISOString().slice(0, 10);
                $("#amount").val(min + " - " + max);
                minDate = min;
                maxDate = max;
            }
        });

        $("#amount").val(min +
            " - " + max);
    }
1 Answers

You're not providing the correct data for the values option. From the docs:

This option can be used to specify multiple handles. If the range option is set to true, the length of values should be 2.

Currently you're passing the entire array of values (37 in your JSFiddle). Change to the following and it should work:

values: [convertedArray[0], convertedArray[convertedArray.length - 1]]

Updated JSFiddle: https://jsfiddle.net/ku519g0f/

Related