IonDen / ion.rangeSlider

jQuery only range slider
http://ionden.com/a/plugins/ion.rangeSlider/en.html
MIT License
2.56k stars 508 forks source link

Slider with % AND dynamic value - can't seem to get prettify to work #741

Closed aximuseng closed 3 years ago

aximuseng commented 3 years ago

I have an existing slider with data attributes like this:

min: 0, max: 100, step: 1, postfix: '%'

I would like to have the slider display like this (I manually edited the HTML to show this):

Screen Shot 2021-02-27 at 1 22 08 PM

The page has multiple sliders in a nested form so each has an associated base multiplier amount i.e. $10,000 x 50% and the slider handle would dynamically update.

My thought here is that I do this:

min: 0, max: 100, step: 1, value: 10000

then I tried this:

$(".myclass").ionRangeSlider()({
  min: 0,
  max: 100,
  step: 1,
  value: $(this).data('value'),
  prettify: function(n) {
    return n + '%' + ' ($' + value * n / 100 + ')';
  }
});

and get:

Screen Shot 2021-02-27 at 2 12 27 PM

I will admit to not being the most proficient at JS so I may be missing something minor / obvious here. After some digging I think I am not accessing the value data attribute. I also tried this (and realized I need to turn my string to a float):

$(".myclass").ionRangeSlider()({
  min: 0,
  max: 100,
  step: 1,
  prettify: function(n) {
    return n + '%' + ' ($' + parseFloat($(this).data('value')) * n / 100 + ')';
  }
});
IonDen commented 3 years ago

Hi, here is the demo: https://jsfiddle.net/qu8jsmy6/

const $range = $(".js-range-slider");
const totalInDollars = 5000;

$range.ionRangeSlider({
  skin: "big",
  type: "single",
  min: 0,
  max: 100,
  from: 50,
  prettify: (n) => {
    const current = totalInDollars / 100 * n;
    return `${n}% (\$${current})`;
  }
});

Usually problems like yours are happening then you are trying to do all steps in 1 line. Split your code. First calculate number in dollars and then it will work much better.

And it will be much easier to debug with console log.

aximuseng commented 3 years ago

That works except in my case the totalInDollars value is different for each slider on the page. This is why I tried to add a data attribute to each slider and then reference it with $(this).

IonDen commented 3 years ago

Yes of course you could get totalInDollars from data attribute. Don't forget to parseFloat it.

aximuseng commented 3 years ago

Not sure how to do this then:

https://jsfiddle.net/jasper502/ck9yuxmj/4/

IonDen commented 3 years ago

You can't access slider via $(this) in prettify. Workaround: https://jsfiddle.net/e2xzpoag/

aximuseng commented 3 years ago

Thanks - that looks like something I can work with 👍. Is direct access to the data attributes something that could be considered in a future release? I think my example show where that would be valuable.

IonDen commented 3 years ago

Yes, I will think about it.

aximuseng commented 3 years ago

I ended up settling on this (bac is my data attribute value):

$('.js-range-slider').each(function() {
  var bac;
  bac = parseFloat($(this).data('bac'));
  return $(this).ionRangeSlider({
    prettify: function(n) {
      var current;
      current = Math.round(bac / 100 * n);
      return n + '%' + ' ($' + current + ')';
    }
  });
});