leongersen / noUiSlider

noUiSlider is a lightweight, ARIA-accessible JavaScript range slider with multi-touch and keyboard support. It is fully GPU animated: no reflows, so it is fast; even on older devices. It also fits wonderfully in responsive designs and has no dependencies.
https://refreshless.com/nouislider/
MIT License
5.69k stars 656 forks source link

nouislider 15.7.1 ignoring start values #1253

Closed berberon closed 1 year ago

berberon commented 1 year ago

I'm using this code to initialize noUiSlider Everything works perfect but both handles are starting on the left side ignoring the start values and ignoring

slider.noUiSlider.set([parseFloat(minmax["Min"]), parseFloat(minmax["Max"])]);

// Initialize range slider
        var slider = document.getElementById('slider');
        var valuesForSlider = VFS.Values.split(','); // 16 values
        var format = {
            to: function(value) {
                return valuesForSlider[Math.round(value)];
            },
            from: function (value) {
                return valuesForSlider.indexOf(Number(value));
            }
        };
        if (!slider.classList.contains('noUi-target')) {
            noUiSlider.create(slider, {
                start: [parseFloat(minmax["Min"]), parseFloat(minmax["Max"])],
                connect: true,
                step: 1,
                tooltips: true,
                format: format,
                pips: { mode: 'steps', format: format },
                range: { min: 0, max: valuesForSlider.length - 1 }
            });
        }else{
            slider.noUiSlider.updateOptions({
                start: [parseFloat(minmax["Min"]), parseFloat(minmax["Max"])],
                connect: true,
                step: 1,
                tooltips: true,
                format: format,
                pips: { mode: 'steps', format: format },
                range: { min: 0, max: valuesForSlider.length - 1 }
            });
        }
        slider.noUiSlider.set([parseFloat(minmax["Min"]), parseFloat(minmax["Max"])]);
leongersen commented 1 year ago

Since I can't see what valuesForSlider or minmax are, I can't help based on this information.

berberon commented 1 year ago

After a few hours of debug i traced the issue to the var valuesForSlider.

var valuesForSlider = VFS.Values.split(','); // 16 values

VFS.Values is a string of numbers like in the example on the noUiSlider site so basically i got an array of strings instead of an array of numbers.

Changing the line to

var valuesForSlider = VFS.Values.split(',').map(Number);

solved the problem.

So if anyone is using a range based slider with pips and everything is working perfectly apart for the fact that both handles are stuck to the left .... Make sure you are passing an array of numbers to the format function and not an array of strings.

Thanks!

github-actions[bot] commented 1 year ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.