views:

14

answers:

1

I'm using the YUI 2.7 library to handle a dual-slider (range slider) control in a webpage.

It works great-- however, I wanted to allow users to switch the range values by Ajax-- effectively changing the price range from "0-50,000" to a subset (eg. "50-250") without reloading the page.

The problem is that it appears the values from the existing slider do not get reset, even when I explicitly set them back to NULL inside the function to "rebuild" the slider.

The slider handles appear out of position after the ajax request, (way off the scale to the right) and the values of the slider apparently randomly fluctuate.

Is there a way to explicitly destroy the YUI slider object, beyond setting its reference to null? Or do I just need to redeclare the scale and min/max values somehow?

Thanks for any help (I'll try to post a link to an example asap)

here's the code:

function slider(bg,minthumb,maxthumb,minvalue,maxvalue,startmin,startmax,aSliderName,soptions) {
        var scaleFactor = null;
        var tickSize = null;
        var range = null;
        var dual_slider = null;
        var initVals = null;
        var Dom = null;

        range = options.sliderLength;
        if ((startmax - startmin) < soptions.sliderLength) {
            tickSize = (soptions.sliderLength / (startmax - startmin));
        }else{
            tickSize = 1;
        }

        initVals = [ 0,soptions.sliderLength ], // Values assigned during instantiation
        //Event = YAHOO.util.Event,
        dual_slider,
        scaleFactor = ((startmax - startmin) / soptions.sliderLength); 

        dual_slider = YAHOO.widget.Slider.getHorizDualSlider(
        bg,minthumb,maxthumb,range, tickSize, initVals);

        dual_slider.subscribe("change", function(instance) {
            priceMin = (dual_slider.minVal * scaleFactor) + startmin;
            priceMax = (dual_slider.maxVal * scaleFactor) + startmin;
        });

        dual_slider.subscribe("slideEnd", function(){ alert(priceMin + ' ' + priceMax); });
        return dual_slider;
    }
+1  A: 

Store the startmin, startmax, and scaleFactor on the dual_slider object, then in your ajax callback, update those properties with new values. Change your change event subscriber to reference this.startmin, this.startmax, and this.scaleFactor.

Slider and DualSlider only really understand the pixel offsets of the thumbs, and report the values as such. As you've done (and per most Slider examples), you need to apply a conversion factor to translate a pixel offset to a "value". This common idiom has been rolled into the core logic of the YUI 3 Slider (though there isn't yet a DualSlider in the library).

Here's an example that illustrates dynamically updating value ranges: http://yuiblog.com/sandbox/yui/v282/examples/slider/slider_factor_change.html

Luke
thanks Luke-- that's what I was wondering about, and you nailed it down with an example too! I appreciate the help.
julio