Slider Widget

input-slider.htm (hint: right click, save as)

  1<style>
  2/* Align multiple slider inputs by suitably fixing their value width: */
  3.form-inline .form-control.slider-value {
  4  width: 80px;
  5}
  6</style>
  7
  8<div class="panel panel-primary">
  9  <div class="panel-heading">Slider Widget</div>
 10  <div class="panel-body">
 11
 12    <h3>Standard Numerical Inputs</h3>
 13
 14    <p>The number input is just the bootstrap default:</p>
 15
 16    <input class="form-control" type="number" id="input-num1" name="num1" value="80" min="0" max="100" step="1">
 17    <br/>
 18
 19    <p>The range input has some styling optimization for touch devices:</p>
 20
 21    <input class="form-control" type="range" id="input-rng1" name="rng1" value="80" min="0" max="100" step="1">
 22    <br/>
 23
 24    <h3>Slider Widget</h3>
 25
 26    <p>
 27      In many cases you'd like to give especially the touchscreen user a combination of these input
 28      elements, and as sliding may be too imprecise you also need large buttons for single step changes.
 29      That's what the slider widget does. It also adds the option of a checkbox and a default value to
 30      reflect if the user control shall be applied.
 31    </p>
 32
 33    <div class="row">
 34      <div class="col-md-9">
 35
 36        <p>Sliders can easily be created from minimal markup using the <code>.slider()</code> plugin,
 37          with configuration given by data attributes and/or dynamic options:</p>
 38
 39        <div class="form-control slider" id="sld1" data-min="-5" data-max="50" data-step="0.5"
 40          data-default="40" data-unit="%" data-disabled="false" data-checked="true" data-value="10" />
 41        <br/>
 42
 43        <div class="form-control slider" id="sld2" />
 44        <br/>
 45
 46        <p>…and slider state, limits and value can be controlled using the same method:<br/>
 47          <button type="button" class="btn btn-default" onclick="$('#sld1').slider({ value:42 })">
 48            Set sld1 value to 42
 49          </button>
 50          <button type="button" class="btn btn-default" onclick="$('#sld1').slider({ checked:true })">
 51            Check sld1
 52          </button>
 53          <button type="button" class="btn btn-default" onclick="$('#sld1').slider({ disabled:true })">
 54            Disable sld1
 55          </button>
 56          <button type="button" class="btn btn-default" onclick="$('#sld1').slider({ disabled:false })">
 57            Enable sld1
 58          </button>
 59        </p>
 60        <br/>
 61
 62        <p>If you need even more control, you can create the slider markup yourself as well:</p>
 63
 64        <div class="form-control slider" id="sld3" data-default="50" data-reset="false"
 65          data-value="80" data-min="-10" data-max="100" data-step="1">
 66          <div class="slider-control form-inline">
 67            <input class="slider-enable" type="checkbox" checked>
 68            <input class="form-control slider-value" type="number" id="input-sld3" name="sld3">
 69            <span class="slider-unit">%</span>
 70            <input class="btn btn-default slider-down" type="button" value="➖">
 71            <input class="btn btn-default slider-set" type="button" value="Lo" data-set="25">
 72            <input class="btn btn-default slider-set" type="button" value="Hi" data-set="75">
 73            <input class="btn btn-default slider-up" type="button" value="➕">
 74          </div>
 75          <input class="slider-input" type="range">
 76        </div>
 77        <br/>
 78
 79      </div>
 80      <div class="col-md-3">
 81
 82        <p><u>Events &amp; values</u>:</p>
 83        <pre id="show-sldev"></pre>
 84
 85      </div>
 86    </div>
 87
 88    <br/>
 89
 90    <p>
 91      Checkbox, buttons and unit are optional. You can reduce the widget to just a number or
 92      just a range input, the input then needs to have the <code>slider-input</code> class.
 93    </p>
 94
 95    <p>
 96      The checkbox element defines the default value to be set on unchecking in
 97      <code>data-default</code>. By default, the checkbox will restore the previous user
 98      value when re-checked, to disable this, set <code>data-reset</code> to "true".
 99      To reset the value to the default from a script, call the <code>.slider()</code>
100      method with <code>value: null</code> (this resets both the actual and the stored
101      user value).
102    </p>
103
104    <p>
105      Values and checkbox status need to be consistent on init, or be set by your script.
106      To hook into value changes, attach event handlers to events <code>input</code>
107      and/or <code>change</code> as usual. Read the <code>checked</code> property to get the
108      checkbox state.
109    </p>
110
111  </div>
112</div>
113
114<script>
115(function(){
116
117  /* Show page source: */
118  var pagesrc = $('#main').html();
119  $('.panel-heading').prepend('<button type="button" class="btn btn-sm btn-info action-showsrc"' +
120    ' style="float:right; position:relative; top:-5px;">Show page source</button>');
121  $('.action-showsrc').on('click', function() {
122    $('<div/>').dialog({
123      title: 'Source Code',
124      body: '<pre style="font-size:85%; height:calc(100vh - 230px);">'
125        + encode_html(pagesrc) + '</pre>',
126      size: 'lg',
127    });
128  });
129
130  /* Init sliders: */
131  $('.slider').slider();
132  $('#sld2').slider({ min:-10, max:10, step:0.1, default:2.5, unit:'kW', checked:false, value:-3.8 });
133
134  /* Show slider events & values: */
135  var sldev = {};
136  $('#input-sld1, #input-sld2, #input-sld3').on('input change', function(ev) {
137    sldev[this.name] = $.extend(sldev[this.name], { checked: this.checked });
138    // Note: this.value is unvalidated here for a direct entry, but the validation is simple:
139    sldev[this.name][ev.type] = Math.max(this.min, Math.min(this.max, 1*this.value));
140    $('#show-sldev').text(JSON.stringify(sldev, null, 2));
141  });
142
143})();
144</script>