bootstrap-slider icon indicating copy to clipboard operation
bootstrap-slider copied to clipboard

How to update tick labels after initialization in JS?

Open paniwani opened this issue 9 years ago • 22 comments

Is there a way to update the tick labels for the slider after the slider has already been initialized in JS?

After the user interacts with a different portion of my app, I would like to change the tick label text. Is this possible? I tried using setAttribute() on the slider but ran into errors and I was not able to get it working.

paniwani avatar Sep 23 '15 17:09 paniwani

Have you tried using .setAttribute?

seiyria avatar Sep 23 '15 17:09 seiyria

Here's my initialization code:

$("#alphaSlider").slider({ id: "alphaSlider", min: 0, max: 1, step: 0.1, value: 0.5, ticks: [0, 1], ticks_labels: ["CT", "PT"], ticks_snap_bounds: 0 });

Then elsewhere in my code, I try to change the tick labels:

$('#alphaSlider').setAttribute("ticks_labels", ["CT", "MRI"]);

But I just get this error in the console:

Uncaught TypeError: $(...).setAttribute is not a function

paniwani avatar Sep 23 '15 18:09 paniwani

Try $('#alphaSlider').slider().setAttribute(...) and $('#alphaSlider').slider('setAttribute', ...).

seiyria avatar Sep 23 '15 18:09 seiyria

Uncaught TypeError: $(...).slider(...).setAttribute is not a function

It also reset my slider, losing all the parameters I set previously.

paniwani avatar Sep 23 '15 18:09 paniwani

Notice I posted a second one that might also work. Try that and let me know if that doesn't work. If it doesn't, as per normal request, please provide a jsfiddle.

seiyria avatar Sep 23 '15 18:09 seiyria

I can also confirm with paniwani that both methods described above do not work. I do not have a fiddle off-hand.

rbyrnes avatar Nov 24 '15 15:11 rbyrnes

+1. Agree. Resetting the ticks and ticks_labels does not appear to work. Even after calling slider.setAttribute(...) and slider.refresh().

Here is a jsfiddle demonstrating this bug: http://jsfiddle.net/mo6v9x7h/2/

ryanoglesby08 avatar Dec 21 '15 21:12 ryanoglesby08

I'm having this problem too, trying to set the tooltip to show.

$(obj).bootstrapSlider('setAttribute','tooltip','show');

doesn't work.

dessalines avatar Jan 15 '16 18:01 dessalines

$('#obj').slider('setAttribute', 'ticks_labels', [1,2,3,4,5,6]); $('#obj').slider('setAttribute', 'ticks', [1,2,3,4,5,6]); $('#obj').slider('refresh');

It does nothing.

leadmitry avatar Jan 18 '16 09:01 leadmitry

I've also tested and can confirm that there seems to be a problem here.

I tested setValue, and have that working fine, but setAttribute doesn't seem to work (using this syntax) - $('#exID').slider('setAttribute','tooltip','hide');

owenpt avatar Feb 21 '16 11:02 owenpt

This is fixed by adding that code in createNewSlider, near line 540:

if (updateSlider === true) {
    if (Array.isArray(this.options.ticks_labels) && this.options.ticks_labels.length > 0) { 
        for (i = 0; i < this.options.ticks_labels.length; i++) {        
            this.tickLabels[i].innerHTML = this.options.ticks_labels[i]
        }
    }
}

Tick labels will now be refreshed when calling the method 'refresh'.

nreynis avatar Jun 15 '16 15:06 nreynis

@ywg it works for me. Thanks indeed!

SimmerChan avatar Nov 28 '16 12:11 SimmerChan

We would appreciate a PR to fix this!

rovolution avatar Nov 28 '16 15:11 rovolution

Fixed here: https://github.com/OnekO/bootstrap-slider/commit/b99bbff58f028d3b3331bc4fa8877023d8f1d674

Not enough testing for PR, apparently working ok.

OnekO avatar Jun 08 '17 13:06 OnekO

I've been able to work around this problem by triggering a resize event which will cause a redraw.
window.dispatchEvent(new Event('resize'));

mmaday avatar Jun 22 '17 15:06 mmaday

I am also having this problem and the proposed fix did the trick!

new problem: if I do this but ALSO want to change the ticks themselves, the new ticks_labels are spaced/placed where the OLD ticks would have been. Eg:

exampleSlider.slider().on('slideStop', function() { var val = exampleSlider.slider('getValue') exampleSlider.slider('setAttribute', 'ticks', ['7', '100','200', '250', '290']) exampleSlider.slider('setAttribute', 'ticks_labels', ['A', 'B','C', 'D', 'E']) exampleSlider.slider('refresh') exampleSlider.slider('setValue', val) });

JonasEc avatar Aug 09 '17 05:08 JonasEc

@seiyria The $("#example").slider('setAttribute', 'ticks', [1, 2, 10]); still doesn't work. Is there a way to set the ticks after the initialization of the slider? Or it can only be set at creation?

yidingalan avatar Aug 11 '17 14:08 yidingalan

@seiyria I am still not able to change tick_labels with v 9.8.0:

$('#obj').slider('setAttribute', 'ticks_labels', ["x", "y", "z"]); $('#obj').slider('refresh');

See https://jsfiddle.net/50am4r24/ Is this the correct usage? Thanks!

keywest1 avatar Aug 16 '17 20:08 keywest1

this bug is still open because it hasn't been addressed, please stop pinging me about it.

seiyria avatar Aug 16 '17 20:08 seiyria

My work-around that worked for me:

$scope.mc_period_slider_ticks = [1, 60, 120, 180, 240, 360, 480, 600]; $scope.mc_period_slider_ticks_labels = ["1", "60", "120", "180", "240", "300", "360", "420"]; $scope.mc_period_slider_max = 420;

        var $slider = $('#mc-period-slider').parent();
        
        angular.forEach($slider.find('.slider-tick-label'), function (tick, index) {                
            $(tick).text($scope.mc_period_slider_ticks_labels[index]);            
        });

        $('#mc-period-slider').bootstrapSlider('setAttribute', 'max', $scope.mc_period_slider_max);            
        $('#mc-period-slider').bootstrapSlider('setAttribute', 'ticks', $scope.mc_period_slider_ticks);
        $('#mc-period-slider').bootstrapSlider('setAttribute', 'ticks_labels', $scope.mc_period_slider_ticks_labels);
        $('#mc-period-slider').bootstrapSlider('setAttribute', 'ticks_tooltip', false);      // important      
        $('#mc-period-slider').bootstrapSlider('refresh');
        $('#mc-period-slider').bootstrapSlider('setValue', parseInt(newValue));

`

haimca avatar Apr 28 '19 09:04 haimca

I was able to make it work with this commit. https://github.com/OnekO/bootstrap-slider/commit/b99bbff58f028d3b3331bc4fa8877023d8f1d674

@seiyria - would that be a good candidate for a PR?

Only improvement I'd add would be to compare if the array differs with the previously supplied one. Maybe some array comparison like here https://stackoverflow.com/a/14853974/10728554 or here https://stackoverflow.com/a/19746771/10728554 and only re-apply the label ticks if arrays differ. But that's more of a performance optimization.

mastef avatar Jan 20 '20 11:01 mastef

If you can add a test for it and show me what this looks like (jsfiddle or similar), I would accept a PR. Thanks for your interest!

seiyria avatar Jan 20 '20 15:01 seiyria