0

I'm trying to create range slider and have some problems.. I need create slider with scale of 5 values - 1000, 10 000, 20 000, 30 000 and 50 000. In slider we can click only on these values.

Also I need three inputs which shows input with current value of slider and can be editable, for example - 15 000 - and slider will show correct, and two disabled for editing inputs for match operations.

Here is example what I do

<div id="slider"></div>
<div>
    sum_kredit
</div>
<span>$<input id="sum_kredit" /></span>
<div>
    year
    <input id="year" disabled/>
</div>
<div>
    two_year
    <input id="two_year" disabled/>
</div>


$("#slider").slider({
    range: "min",
    value: 1000,
    step: 1000,
    min: 1000,
    max: 50000,
    slide: function (event, ui) {
        $("#sum_kredit").val(ui.value);
        $("#year").val((ui.value)/4);
        $("#two_year").val((ui.value)/5);
    }
});
$("#sum_kredit").change(function () {
    $("#slider").slider("value",this.value);
});

So, help me please with scale and correct showing slider if we type in first input non-default values..

May be it will better use another solutions for this purpose?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Vladyslav Sharapat
  • 93
  • 1
  • 4
  • 11
  • I would consider an array of values. Like: `[1000, 10000, 2000, ...]` Then set `min` to 0, and `max` to array length. As the slider moves, you will get value `0, 1, 2...` This can then be the index used in the array. – Twisty Jan 06 '18 at 17:57

1 Answers1

0

Expanding upon my comment.

$(function() {
  var myValues = [
    1000,
    10000,
    20000,
    30000,
    50000
  ];
  $("#slider").slider({
    range: "min",
    value: 0,
    step: 1,
    min: 0,
    max: myValues.length - 1,
    slide: function(event, ui) {
      var i = ui.value;
      $("#sum_kredit").val(myValues[i]);
      $("#year").val(myValues[i] / 4);
      $("#two_year").val(myValues[i] / 5);
    }
  });
  $("#sum_kredit").change(function() {
    var val = parseInt($(this).val());
    $("#slider").slider("value", myValues.indexOf(val));
  });
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div id="slider"></div>
<div>
  sum_kredit
</div>
<span>$<input id="sum_kredit" /></span>
<div>
  year
  <input id="year" disabled/>
</div>
<div>
  two_year
  <input id="two_year" disabled/>
</div>

As you can see, you can easily reference the array to get your specific values. And you can get the index of that value using .indexOf() if you need to go the other direction.

Update

If you want a slider that can handle unknown potential values, I would advise working with it like a percentile (0 to 100). For example, if the user enters 25000; this is not in the array, yet falls between 20000 and 30000. Each coordinated breakpoint would be at 20, we'd want to land at 45 for this.

$(function() {
  var myValues = [
    1000,
    10000,
    20000,
    30000,
    50000
  ];
  $("#slider").slider({
    range: "min",
    value: 0,
    step: 1,
    min: 0,
    max: 100,
    slide: function(event, ui) {
      var i = 0;
      if (ui.value > 20) {
        i = Math.floor(ui.value / 20);
      }
      if (ui.value > 80) {
        i = 4;
      }
      $("#sum_kredit").val(myValues[i]);
      $("#year").val(myValues[i] / 4);
      $("#two_year").val(myValues[i] / 5);
    }
  });

  $("#slider .tic").each(function(ind, elem) {
    var c = $(elem).attr("class").split(" ");
    var i = c.indexOf("tic");
    c.splice(i, 1);
    var pos = c.join("");
    $(".slide-labels ." + pos).position({
      my: "center bottom",
      at: "right top-4",
      of: $(elem)
    });
  });

  $("#sum_kredit").change(function() {
    var val = parseInt($(this).val());
    var point = 0;
    if (myValues.indexOf(val) != -1) {
      point = (myValues.indexOf(val) + 1) * 20;
      console.log("Setting point from array: " + point);
    } else {
      if (val < 1000) {
        point = Math.round(val / 50);
        console.log("Setting point: " + val + " / 50 = " + point);
      }
      if (val > 1000 && val < 10000) {
        point = Math.round(val / 450) + 20;
        console.log("Setting point: " + val + " / 450 + 20 = " + point);
      }
      if (val > 10000 && val < 20000) {
        point = Math.floor(val / 500) + 20;
        console.log("Setting point: " + val + " / 500 + 20 = " + point);
      }
      if (val > 20000 && val < 30000) {
        point = Math.floor(val / 500) + 20;
        console.log("Setting point: " + val + " / 500 + 20 = " + point);
      }
      if (val > 30000 && val < 50000) {
        point = Math.floor(val / 1000) + 50;
        console.log("Setting point: " + val + " / 1000 + 50 = " + point);
      }
    }
    $("#slider").slider("value", point);
    $("#year").val(val / 4);
    $("#two_year").val(val / 5);
  });
});
#slider {
  background: transparent;
}

#slider .tic {
  border-right: 1px solid #ccc;
  width: 20%;
  height: 100%;
  display: inline-block;
  z-index: 1000;
}

.slide-labels {
  font-size: 11px;
  position: relative;
  margin-bottom: 2px;
}

.slide-labels .label {
  display: inline-block;
}

.slide-labels .label.first {
  top: -2px;
}

.slide-labels .label.last {
  float: right;
  top: -2px;
}
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div class="slide-labels">
  <div class="label pos-0 first">
    $0
  </div>
  <div class="label pos-1">
    $1,000
  </div>
  <div class="label pos-2">
    $10,000
  </div>
  <div class="label pos-3">
    $20,000
  </div>
  <div class="label pos-4">
    $30,000
  </div>
  <div class="label pos-5 last">
    $50,000
  </div>
</div>
<div id="slider">
  <div class="tic pos-1">
  </div>
  <div class="tic pos-2">
  </div>
  <div class="tic pos-3">
  </div>
  <div class="tic pos-4">
  </div>
</div>
<div>
  sum_kredit
</div>
<span>$<input id="sum_kredit" /></span>
<div>
  year
  <input id="year" disabled/>
</div>
<div>
  two_year
  <input id="two_year" disabled/>
</div>

It becomes a complicated calculation to adjust for different scales.

Twisty
  • 30,304
  • 2
  • 26
  • 45
  • thank you for your answer, but if I choose $25000 - the slider label is goes to the beginning instead to stay between breakpoints - $20000 and $30000. May be it's possible to make near breakpoint label like ' | /n value' and correct move label if we change value in first input? – Vladyslav Sharapat Jan 08 '18 at 17:37
  • @VladyslavSharapat 25000 is not one of the values. If the user enters it manually, which way do you want the slider to go? 20000 or 30000? You did not specify how you wanted it to handle entries of that nature. – Twisty Jan 08 '18 at 17:41
  • sorry for misunderstanding, but I need if user choose $25000 that label will be between 20000 and 30000. if $5000 - between 0 and 10000 and so on. So it should be in proportion to the slider. – Vladyslav Sharapat Jan 08 '18 at 17:51
  • @VladyslavSharapat so you want to be able to set the value of the slider to a value that does not exist in the array? What if the user enters a value more than 50000? – Twisty Jan 08 '18 at 17:59
  • Yes, I need that user will be able to set value that does no exist in the array, but may be it's possible to make array with range 1000..50000, and in slider only 6 breakpoins - 1000, 10000, 20000, 30000, 40000 and 50000. If user set less than 1000 - it will work validator that says - 'minimum value is 1000' and conversely if more 50000 'maximum value is 50000' – Vladyslav Sharapat Jan 08 '18 at 18:04
  • Thank you so much, this is almost what I need:) I've tried another sollutions like noulider but I have more problems than jquery ui slider.. Just last question - how it's possible to improve this slider - just make step 10 and slider label will be shows proportional OR update all inputes if we chose on slider non-standart value? If we do 10 step - is it possible to make steps like link on value? – Vladyslav Sharapat Jan 08 '18 at 21:57
  • @VladyslavSharapat you can set the `step` option (would advise 19), yet this will break the ability to set the slider to a value that does not meet one of the step values. – Twisty Jan 09 '18 at 00:43