Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoUIslider - Update range on demand

I have two sliders and I would like to update the range of one slider based on the movement of the other.

For instance; slider_1 and slider_2 both have a range of 1-10. When I move slider_1 from position 1 to 2, slider_2's range changes from 1-10 to 1-20. If I move slider_1 from position 2 to 3, slider_3 now has a range from 1-30, and so on.

I initialize the slider like so:

  function slider() {
        $(".slider").noUiSlider({
            orientation: "horizontal",
            start: [0],
            range: {
                min: 0,
                max: 10,
            },
            connect: 'lower',
            direction: "ltr",
            step: 1,
        });
    };

The best way I can come up with implementing this so far is deconstruct the whole slider, and re-initialize it with the new range each time. However I am unsure of how to properly deconstruct the slider.

Any ideas on how this should be done?

like image 843
DGDD Avatar asked Sep 10 '14 18:09

DGDD


People also ask

How do I set the value of a noUiSlider?

To set a single slider handle, the setHandle method can be used. This method accepts a zero-indexed handle number, a value and optionally a 'fire set event' boolean. Passing null as the value to setHandle will leave the handle unchanged.

How do you use a noUiSlider?

noUiSlider can provide a basic tooltip using the tooltips option. This option can also accept formatting options to format the tooltips content. In that case, pass an array with a formatter for each handle, true to use the default or false to display no tooltip. To merge overlapping tooltips, refer to this example.


2 Answers

noUiSlider offers an updateOptions method, which will keep all settings, save for any new ones you pass it. The documentation on updating is here.

Starting from noUiSlider 8, you can do:

slider.noUiSlider.updateOptions({
    range: {
        'min': 20,
        'max': 30
    }
});

Disclosure: I'm the plugin author.

like image 50
Lg102 Avatar answered Sep 23 '22 20:09

Lg102


You need to call destroy() and then create() to dynamically change the range. There is no way to alter the range after the control has been rendered.

slider.noUiSlider.destroy()
slider.noUiSlider.create({
    range: {
        'min': 20,
        'max': 30
    }
});
like image 37
SliverNinja - MSFT Avatar answered Sep 20 '22 20:09

SliverNinja - MSFT