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.66k stars 659 forks source link

Place for beginners to ask dumb questions about using noUiSlider? #1128

Closed chrisjdixon closed 3 years ago

chrisjdixon commented 3 years ago

I'm new to HTML / CSS / JS and there are many basic things I don't understand. I can't figure out how to replicate basic things shown in the documentation, probably because I'm oblivious to basic things that noUiSlider depends on and assumes knowledge of. I've been unsuccessful in finding somewhere to ask dumb questions so I'm hoping someone here could please point me in the right direction. I'd ask on SO but I'd they'd say that's a stupid question and my post would be removed. I feel like an idiot.

Is there some kind of forum where super dumb questions such as these would not be unwelcome?

Also, I know this is Github and this is a place for people who know what they're doing to discuss real issues but I couldn't find anywhere else to ask. Hope asking this here is alright.

leongersen commented 3 years ago

No problem, feel free to ask.

chrisjdixon commented 3 years ago

OK, cool. You sure it's alright to ask here? I don't want to clog things up with others and these are super dumb questions which are probably more related to basic web development than noUiSlider.

Thanks so much for the help! There isn't a discord channel for these kinds of questions, is there?

leongersen commented 3 years ago

What's the relevance of all these other files in the repository? I get they're necessary but how?

These include automated tests, editor settings, the license file, package manager configurations, etc.

Should all of the files in the zip be loaded somehow?

No, you only need nouislider.js, and nouislider.css, or their minified versions.

Is that what package managers like npm are for?

Package managers can help with this; they know to only fetch the files needed. For noUiSlider, these are configured in package.json. The use of a package manager is optional, especially while you're learning the basics.

How can I refer to the sliders' values?

You can get these in JavaScript using the API:

slider.noUiSlider.get();

If you need the values server side, you could set them as the value to an input element, which would go something like this:

sliderFormat.noUiSlider.on('update', function (values, handle) {
    document.getElementById('input-element').value = values[handle];
});

get request finds the value from the name (I think...),

Yes, for a GET request, form values are submitted as query string parameters (the part in URLs after ?) by input name.

There isn't a discord channel for these kinds of questions, is there?

I'm not aware of one.

chrisjdixon commented 3 years ago

Awesome, thank you so much. Your response has perfectly resolved many things and helped me uncover more things I don't understand! It's so simple once you know it!

Thank you so much for your help. Is there anything I can do to support noUiSlider?

chrisjdixon commented 3 years ago

Partially figured out my first question by finding your answer on a SO thread asking the same, using edit: function ( value ) {return value == MaxValue ? DisplayString: value;}. Is it possible to do this for multiple values at once? I've tried edit: function ( value ) {return value == '5' ? 'max' : value, return value == '0' ? 'min' : value}, and edit: function ( value ) {return value == '5' ? 'max' : value}, edit: function ( value ) {return value == '0' ? 'min' : value} but this made the 1st line inactive and caused a syntax error, respectively.

I don't want to wear out my welcome so if this is entirely a JS issue feel free to tell me to go away!

leongersen commented 3 years ago

is it possible to set the min and max values of the slider to be some string to signify no upper / lower limit, and show numbers like normal in between?

You could use the formatting feature to output strings for certain values, such as the start/end. For a slider with range 0-50, you could do something like:

noUiSlider.create(slider, {
    // ...
    format: {
        to: function (value) {
            var round = Math.round(value);

            if (round === 0) {
                return 'min value';
            }

            if (round === 50) {
                return 'max value';
            }

            return value;
        },
        from: Number
    }
});

does the slider require a number to start with?

Yes, it does. You have several options to initialize the values, such as setting them to the min/max for the slider.

but this made the 1st line inactive and caused a syntax error, respectively.

The syntax used in the example you've linked is a conditional ternary operator. For your use case, you can use if statements like in the example above.

sandstrom commented 3 years ago

@leongersen You could enable discussions on the repo: https://docs.github.com/en/discussions

chrisjdixon commented 3 years ago

Oh, right... that's just a basic if statement and I now see how stupid my question was. I'm tempted to delete it out of shame but I'll leave it up in the hope it'll help someone else someday. It's all working perfectly now thanks to your help! Thank you very much!

I've got one more question: I'm trying to create another slider for dates, but I'm only interested in showing months and years. Looks like I could adapt from this example, adjusting the step, but it looks like steps are defined by multiples of milliseconds step: 7 * 24 * 60 * 60 * 1000, but the number of days vary by calendar month. Suggestions?

leongersen commented 3 years ago

It'd probably be easiest to set the range to the number of months you want to use, and, pull their values from an array (or create them dynamically). Something like this example.

chrisjdixon commented 3 years ago

OK, great. I ended up using your dates example, except not including the date in the value. Works a treat!

One last question: when the slider's values are at min / max values the data is unfiltered, so I want the slider to be greyed out, similar to the sliders on this page, but I don't want the sliders to be disabled. Is there an easy way to set the connect option to false when values are at min / max?

leongersen commented 3 years ago

Is there an easy way to set the connect option to false when values are at min / max?

I saw your question pop up on StackOverflow as well. You could "disable" the connect option by hiding the .noUi-connect element in the logic proposed there.

.noUi-connect {
    display: none;
}
chrisjdixon commented 3 years ago

Oooh, lovely. Even more elegant than changing the color. Thank you!

Amazing how straightforward things are once you know it! You've made such a great package. Thank you for all your help and this great tool you've given the world!

github-actions[bot] commented 2 years 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.