jQuery UI - Slider - How to add values

前端 未结 1 1540
隐瞒了意图╮
隐瞒了意图╮ 2021-01-14 22:29

fiddle - I\'ve got set of values. Is it possible to add new handle or remove some of them without destroying and rebuilding slide instance?

Something like $(\'

相关标签:
1条回答
  • 2021-01-14 23:11

    If you can upgrade to a version 1.10.x of jquery ui, you'd be able to do something like the following:

    (function ($) {
        $.widget('my-namespace.customSlider', $.ui.slider, {
            addValue: function( val ) {
                //Add your code here for testing that the value is not in the list
                this.options.values.push(val);
                this._refresh();
            },
            removeValue: function( ) {
                this.options.values.pop( );
                this._refresh();
            }
        });
    })(jQuery);
    

    After that, you'd be able to use it like so:

    $("#slider").customSlider({/* options */});
    

    And add values:

    $("#slider").customSlider('addValue', 5);
    

    In this code, we're creating a new slider widget that inherits from the existing jquery-ui slider. In the addValue method, it's manually updating widget's internal array of values, and then calling the private _refresh() method of the original jQuery-ui slider, which is how the slider generates the handles in the first place when the widget is created. The _refresh() method was added in one of the more recent versions of jQuery ui. If you can't upgrade, this technique would still be possible, but you'd have to write code to inject the markup and bind the drag events.

    Here's a fiddle showing this in action http://jsfiddle.net/ac2A3/5/

    Your code to handle the slide event would have to change a little since it relies on the widget's values being in order.

    0 讨论(0)
提交回复
热议问题