Skip to content Skip to sidebar Skip to footer

JQuery UI Slider - Combined Max For Multiple Sliders That Also Have Individual Max Values

- Solved - I Know they exist, but I haven't found a slider that can easily meet some very basic needs and I'm sure I'm not the only one frustrated with this problem. This is what

Solution 1:

JSFiddle: http://jsfiddle.net/mikecruz/bAntY/

Javascript / Jquery:

var sliders = $("#sliders .slider");

sliders.each(function() {
    var max = document.getElementById("sliderMax").value;
    var value = Number($(this).text(), 10),
        availableTotal = max;

    $(this).empty().slider({
        value: 0,
        min: 0,
        max: $(this).siblings().attr("max"),
        range: "max",
        step: 1,
        animate: 100,
        disabled: (function(curMax) {
            if (curMax < 1) {
                return 1;
            }
            return 0;
        })($(this).siblings().attr("max")),
        stop: function(event, ui) {
            // Update text box to current value and call .change()
            $(this).siblings().attr("value", ui.value);
            $(this).siblings().trigger('change');
        }
    });
});

$(".value").change(function() {
    var thisAmount = Number($(this).prop("value"));
    var totalMax = Number(document.getElementById("sliderMax").value);
    var indMin = Number($(this).attr("min"));
    var indMax = Number($(this).attr("max"));
    var total = 0;

    //Get the values of all other text boxes
    $('.value').not(this).each(function() {
        total += Number($(this).prop("value"));
    });

    //Find the remaining from our total sliders max
    var remaining = totalMax - total;

    if(remaining < 0) {
      remaining = 0;   
    }
    //if we are under our minimums, go for it! Otherwise, reduce the number.
    if (thisAmount >= indMin && thisAmount < indMax && thisAmount < totalMax && thisAmount < remaining) {
        $(this).siblings(".slider").slider("option", "value", thisAmount);
        //total += thisAmount;
    }
    else {
        //var setMax = ((indMax + totalMax) - Math.abs(indMax - totalMax)) / 2;
        var setMax = Math.min(indMax, totalMax, remaining);
        $(this).siblings(".slider").slider("option", "value", setMax);
        $(this).prop("value", setMax);
        //total += (thisAmount - setMax);
    }

    //above was getting buggy, so lets just reset total and get it again
    total = 0;
    //Get the values of all text boxes
    $('.value').each(function() {
        total += Number($(this).prop("value"));
    });

    //Find our new remaining number after updating total for this value
    remaining = totalMax - total;
    if(remaining < 0) {
      remaining = 0;  
    }
    //Set each slider to the current point and update their max values.
    $('.value').each(function() {
    var sliderVal = Number($(this).prop("value"));
    var sliderMin = Number($(this).attr("min"));
    var sliderMax = Number($(this).attr("max"));
    var setNewMax = (((sliderMax + totalMax) - Math.abs(sliderMax - totalMax)) / 2);
    var newMax = sliderVal + remaining;

    $(this).prop("max", newMax);        
    if(newMax < setNewMax) {
        $(this).siblings('.slider').slider("option", "max", newMax);
        $(this).siblings('span').text('/' + (newMax));
    }
    else {
        $(this).siblings('.slider').slider("option", "max", setNewMax);
        $(this).siblings('span').text('/' + (setNewMax));
    }
    $(this).siblings(".slider").slider("option", "value", sliderVal);
    });

    $('#sliderTotal').attr("value", total);
});

HTML:
You need an li for each slider
You can use CSS to additionally style different parts. E.g. give slideBG a background image.
You might want to make the totaling boxes hidden and, of course, you might script the max value.

<form>
<ul id="sliders">
    <li>
        <span class="slideBG"><div class="slider"></div>
        <input type="text" class="value" name="slider1" value="0" min="0" max="0"/>
        <span>0</span></span>
    </li>
    <li>
        <span class="slideBG"><div class="slider"></div>
        <input type="text" class="value" name="slider2" value="0" min="0" max="500" />
        <span>0</span></span>
    </li>
    <li>
        <span class="slideBG"><div class="slider"></div>
        <input type="text" class="value" name="slider3" value="0" min="0" max="500" />
        <span>0</span></span>
    </li>
    <li>
        <span class="slideBG"><div class="slider"></div>
        <input type="text" class="value" name="slider4" value="0" min="0" max="500"/>
           <span>0</span></span>
    </li>
    <li>
        <span class="slideBG"><div class="slider"></div>
        <input type="text" class="value" name="slider5" value="0" min="0" max="500"/>
           <span>0</span></span>
    </li>
    <li>
        <span class="slideBG"><div class="slider"></div>
        <input type="text" class="value" name="slider6" value="0" min="0" max="500"/>
           <span>0</span></span>
    </li>
</ul>
    <br><label for="sliderMax">Total Max:</label>
    <input type="text" id="sliderMax" name="sliderMax" value="1000"/>
    <br><br><label for="sliderTotal">Slider Totals:</label>
    <input type="text" id="sliderTotal" name="sliderTotal" value="0" />
</form>

EDIT:

You might want to add this in to keep folks from using the text box if the slider is disabled:

$(".slider").each(function() {
    var disabled = Number($(this).slider("option", "disabled"));
    if(disabled == 1) {
        $(this).siblings('.value').attr('disabled', 'disabled');    
    }
});

Solution 2:

Hi I just tried something similar but implemented it in a different way. I have multiple sliders with one max. All the sliders' values together cannot add up to be more than the total. Ex. 3 sliders with a total of 100%. The sliders can be 10%, 50%, 40%.

This doesn't answer everything you want but it might be good for other users looking for something similar.

var sliders = $('.slider');
sliders.each(function(i) {
  var slider = $(sliders[i]);
  var input = $('#' + slider.attr('datanamespace'));
  slider.slider({
    value: slider.attr('datatotal'),
    min: 0,
    max: max,
    step: 1,
    slide: function( event, ui ) {
      if (ui.value < slider.attr('datavalue')) {
        return false;
      }
      var inputs = $('.slideInput');
      var total = ui.value;

      for (var i = 0; i < inputs.length; i++) {
        var inp = $(inputs[i]);
        if (inp.attr('id') != input.attr('id')) {
          total = total + parseInt(inp.val());
        }
      }

      if (total > max) {
        return false;
      }
      input.val(ui.value);
    }
  });
  input.val(slider.slider("value"));
});

Post a Comment for "JQuery UI Slider - Combined Max For Multiple Sliders That Also Have Individual Max Values"