Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoUISlider Tooltip only show integers

I want that the tooltip on my slider only shows integers like "130" and not "130.00". I just dont know where i could start.

Here is my code:

$( document ).ready(function() {
var groesseslider = document.getElementById('slider-tooltip');

noUiSlider.create(groesseslider, {
    start: [ 145 ],
    step: 1,
    range: {
        'min': 100,
        'max': 250
    }
});
    });
$( document ).ready(function() {
    var groesseslider = document.getElementById('slider-tooltip');
var tipHandles = groesseslider.getElementsByClassName('noUi-handle'),
    tooltips = [];

// Add divs to the slider handles.
for ( var i = 0; i < tipHandles.length; i++ ){
    tooltips[i] = document.createElement('div');
    tipHandles[i].appendChild(tooltips[i]);
}

// When the slider changes, write the value to the tooltips.
groesseslider.noUiSlider.on('update', function( values, handle ){
    tooltips[handle].innerHTML = values[handle];
});
    });

My JS Code: http://jsfiddle.net/miiauwz/66a5ahm0/

like image 244
Juloius Avatar asked Jul 25 '15 22:07

Juloius


4 Answers

This can work..

var sliderFormat = document.getElementById('slider-format');

noUiSlider.create(sliderFormat, {
start: [ 20 ],
...
format: {
    from: function(value) {
            return parseInt(value);
        },
    to: function(value) {
            return parseInt(value);
        }
    }
});
like image 74
user2540197 Avatar answered Nov 13 '22 17:11

user2540197


You can either try using the unencoded value like described in noUISlider's documentation about events and their binding

slider.noUiSlider.on("update", function(values, handle, unencoded ) {
    // values: Current slider values;
    // handle: Handle that caused the event;
    // unencoded: Slider values without formatting;
});

or another possibility would be using the format option on slider creation (but haven't tried it myself yet):

noUiSlider.create(slider, {
    start: [ 20000 ],
    ...
    format: wNumb({
        decimals: 0, // default is 2
        thousand: '.', // thousand delimiter
        postfix: ' (US $)', // gets appended after the number
    })
});

The drawback is you have to download the wNumb-Library separately from here: http://refreshless.com/wnumb/.


Another way without wNumb

After having another look at the examples from noUISlider, I found this way for manually formatting (at the bottom of the page):

var sliderFormat = document.getElementById('slider-format');

noUiSlider.create(sliderFormat, {
    start: [ 20 ],
    ...

    format: {
      to: function ( value ) {
        return value + ',-';
      },
      from: function ( value ) {
        return value.replace(',-', '');
      }
    }
});
like image 25
SaSchu Avatar answered Nov 13 '22 18:11

SaSchu


If you don't think you'll ever need to have decimal places on your site, you can search the jquery.nouislider.min.js file for toFixed(2) and replace with toFixed(0).

like image 41
Bill_VA Avatar answered Nov 13 '22 17:11

Bill_VA


I you don't want to use wNumb - library , this method might work. This will give you value without decimals. Hope this helps.

value.split('.')[0]
like image 1
achharsh Avatar answered Nov 13 '22 18:11

achharsh