UNPKG

angular-rangeslider

Version:

Angular RangeSlider is a directive that creates an interactive slider that allows a user to change model values

268 lines (180 loc) 12.8 kB
<!DOCTYPE html> <html> <head> <meta charset='utf-8'> <meta http-equiv="X-UA-Compatible" content="chrome=1"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <link href='https://fonts.googleapis.com/css?family=Architects+Daughter' rel='stylesheet' type='text/css'> <link rel="stylesheet" type="text/css" href="stylesheets/stylesheet.css" media="screen" /> <link rel="stylesheet" type="text/css" href="stylesheets/pygment_trac.css" media="screen" /> <link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" /> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <title>AngularJS RangeSlider by danielcrisp</title> </head> <body> <header> <div class="inner"> <h1>AngularJS RangeSlider</h1> <h2>Simple directive that creates a range slider for Angular, styled to match Bootstrap-styled form elements</h2> <a href="https://github.com/danielcrisp/angular-rangeslider" class="button"><small>View project on</small>GitHub</a> </div> </header> <div id="content-wrapper"> <div class="inner clearfix"> <section id="main-content"> <h1> <a name="angular-rangeslider" class="anchor" href="#angular-rangeslider"><span class="octicon octicon-link"></span></a>AngularJS RangeSlider</h1> <p><em>Current version: 0.0.14</em></p> <p>Angular RangeSlider is a directive that creates an interactive slider that allows a user to change model values.</p> <p>It has been styled to match form elements styled by <a href="http://twitter.github.io/bootstrap/">Twitter's Bootstrap CSS framework</a>.</p> <h4> <a name="requirements" class="anchor" href="#requirements"><span class="octicon octicon-link"></span></a>Requirements</h4> <ul> <li>Angular (v1.0.8+)</li> <li>jQuery (v1.7+)</li> </ul> <h2> <a name="quick-example" class="anchor" href="#quick-example"><span class="octicon octicon-link"></span></a>Demo</h2> <p>Take a look at the <a href="demo/">live demo page</a></p> <p>Or the <a href="demo/legacy.html">legacy Angular demo page</a></p> <h2> <a name="quick-example" class="anchor" href="#quick-example"><span class="octicon octicon-link"></span></a>Quick example</h2> <p>A basic slider with a range of 0 to 100:</p> <pre><code>&lt;div range-slider min="0" max="100" model-min="min" model-max="max"&gt;&lt;/div&gt; </code></pre> <p>As the handles are moved the model values <code>min</code> and <code>max</code> will be updated in the parent controller.</p> <p><img src="screenshots/slider.png" alt="Default example"></p> <h2> <a name="options" class="anchor" href="#options"><span class="octicon octicon-link"></span></a>Options</h2> <p>Options are set as attributes on the <code>&lt;div range-slider&gt;</code></p> <h3> <a name="-two-way-bindings" class="anchor" href="#-two-way-bindings"><span class="octicon octicon-link"></span></a><code>=</code> two-way bindings</h3> <p><code>min</code> - the minimum value the user can select (must be a number, can be a model property)</p> <p><code>max</code> - the maximum value the user can select (must be a number, can be a model property)</p> <p><code>model-min</code> - the model property for the min value, represents the position of the min handle</p> <p><code>model-max</code> - the model property for the max value, represents the position of the max handle</p> <p><code>disabled</code> - model property or boolean, disables the slider when <code>true</code></p> <h3> <a name="-attributes" class="anchor" href="#-attributes"><span class="octicon octicon-link"></span></a><code>@</code> attributes</h3> <p><code>orientation</code> - slider orientation, default: 'horizontal' - options: 'horizontal' | 'vertical' | 'vertical left' | 'vertical right'</p> <p><code>step</code> - amount to change the value by when moving a handle, default: 0</p> <p><code>decimal-places</code> - the number of decimal places to round to, default: 0</p> <p><code>filter</code> - a built-in filter to apply to the displayed values, for example <code>currency</code> or <code>currency:'$'</code></p> <p><code>filter-options</code> - options to pass to the filter</p> <p><code>pin-handle</code> - disable/hide one handle, default: null - options: 'min' | 'max'</p> <p><code>prevent-equal-min-max</code> - prevent the <code>min</code> and <code>max</code> values from being equal. The <code>step</code> value is used to set the minimum difference, otherwise a value of <code>1</code> is used.</p> <p><code>attach-handle-values</code> - move the value labels in sync with the slider handles when <code>true</code>, default: <code>false</code></p> <h2> <a name="some-more-examples" class="anchor" href="#some-more-examples"><span class="octicon octicon-link"></span></a>Some more examples</h2> <h3> <a name="using-model-properties" class="anchor" href="#using-model-properties"><span class="octicon octicon-link"></span></a>Using model properties</h3> <p>The following properties are present in the scope:</p> <pre><code>// set available range $scope.minPrice = 100; $scope.maxPrice = 999; // default the user's values to the available range $scope.userMinPrice = $scope.minPrice; $scope.userMaxPrice = $scope.maxPrice; </code></pre> <p>So we can include the directive in the HTML like this:</p> <pre><code>&lt;div range-slider min="minPrice" max="maxPrice" model-min="userMinPrice" model-max="userMaxPrice" step="5"&gt;&lt;/div&gt; </code></pre> <p>As the user moves the min and max handles the <code>userMinPrice</code> and <code>userMaxPrice</code> will be updated in increments of <code>5</code> in real-time in the model.</p> <h3> <a name="using-filters" class="anchor" href="#using-filters"><span class="octicon octicon-link"></span></a>Using filters</h3> <p>Continuing from the example above we can format the values displayed to the user as currency.</p> <pre><code>&lt;div range-slider min="minPrice" max="maxPrice" model-min="userMinPrice" model-max="userMaxPrice" step="5" filter="currency"&gt;&lt;/div&gt; </code></pre> <p>This will automatically be localised by Angular, but we can force it to be USD by passing this as an option:</p> <pre><code>&lt;div range-slider min="minPrice" max="maxPrice" model-min="userMinPrice" model-max="userMaxPrice" step="5" filter="currency" filter-options="USD$"&gt;&lt;/div&gt; </code></pre> <p>Alternatively you can use Angular's filter notation directly in the <code>filter</code> attribute, such as <code>filter="currency:'GBP £'"</code>, which would result in values like this: <code>GBP £7,500.00</code>.</p> <pre><code>&lt;div range-slider min="minPrice" max="maxPrice" model-min="userMinPrice" model-max="userMaxPrice" step="5" filter="currency:'GBP £'"&gt;&lt;/div&gt; </code></pre> <p><strong>NOTE:</strong> If the <code>filter-options</code> attribute is defined you **cannot** use Angular filter notation. You must only use the filter name in the <code>filter</code> attribute. <p><img src="screenshots/currency.png" alt="Currency example"></p> <h3> <a name="making-the-slider-vertical" class="anchor" href="#making-the-slider-vertical"><span class="octicon octicon-link"></span></a>Making the slider vertical</h3> <p>Simply add one of the following values to the <code>orientation</code> attribute: 'vertical', 'vertical left' or 'vertical right'.</p> <p>This will create a vertical slider that is centred in it's parent element:</p> <pre><code>&lt;div range-slider min="0" max="100" model-min="min" model-max="max" orientation="vertical"&gt;&lt;/div&gt; </code></pre> <p><img src="screenshots/vertical.png" alt="Vertical example"></p> <p>To left-align the slider use 'vertical left':</p> <pre><code>&lt;div range-slider min="0" max="100" model-min="min" model-max="max" orientation="vertical left"&gt;&lt;/div&gt; </code></pre> <p>And to right-align the slider use 'vertical right':</p> <pre><code>&lt;div range-slider min="0" max="100" model-min="min" model-max="max" orientation="vertical right"&gt;&lt;/div&gt; </code></pre> <h3> <a name="disabling-the-slider" class="anchor" href="#disabling-the-slider"><span class="octicon octicon-link"></span></a>Disabling the slider</h3> <p>If you have a boolean property in your scope you can simply change this value to <code>true</code> to disable the slider:</p> <pre><code>$scope.sliderDisabled = false; </code></pre> <p>And then specify the property using the disabled attribute:</p> <pre><code>&lt;div range-slider min="0" max="100" model-min="min" model-max="max" disabled="sliderDisabled"&gt;&lt;/div&gt; // clicking this button will toggle the sliderDisabled value between true and false &lt;button ng-click="sliderDisabled=!sliderDisabled"&gt;Toggle slider disabled status&lt;/button&gt; </code></pre> <p><img src="screenshots/disabled.png" alt="Disabled example"></p> <h3> <a name="pinning-a-handle" class="anchor" href="#pinning-a-handle"><span class="octicon octicon-link"></span></a>Pinning a handle</h3> <p>If you would like only allow setting one value, effectively creating a single-value silder, set the pin-handle attribute to 'min' or 'max'. You may then omit the corresponding model-xxx property:</p> <pre><code>&lt;div range-slider min="0" max="100" model-max="max" pin-handle="min&gt;&lt;/div&gt; </code></pre> <p><img src="screenshots/pinned.png" alt="Pinned example"></p> <h2> <a name="to-do" class="anchor" href="#to-do"><span class="octicon octicon-link"></span></a>To Do</h2> <ul> <li>Remove full jQuery dependency</li> <li>Make it work in older Angular versions</li> <li>Add option to move values with handles</li> <li>Improve behaviour when model values are not valid (e.g. min is greater than max)</li> </ul><h2> <a name="known-issues" class="anchor" href="#known-issues"><span class="octicon octicon-link"></span></a>Known Issues</h2> <ul> <li>The slider restricts the model value when editing outside the slider (e.g. in an ) but the only notification is made to the <code>console</code> </li> <li>The min slider handle disappears behind the max slider handle</li> </ul><h2> <a name="credits" class="anchor" href="#credits"><span class="octicon octicon-link"></span></a>Credits</h2> <p>This was originally forked from <a href="http://refreshless.com/">Léon Gersen's</a> brilliant noUiSlider:</p> <p><a href="https://github.com/leongersen/noUiSlider">https://github.com/leongersen/noUiSlider</a></p> <h2> <a name="licence" class="anchor" href="#licence"><span class="octicon octicon-link"></span></a>Licence</h2> <p>This code is released under the <a href="http://opensource.org/licenses/MIT">MIT Licence</a></p> <p>Copyright (c) 2013 Daniel Crisp</p> <p>Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:</p> <p>The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.</p> <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</p> </section> <aside id="sidebar"> <a href="https://github.com/danielcrisp/angular-rangeslider/zipball/master" class="button"> <small>Download</small> .zip file </a> <a href="https://github.com/danielcrisp/angular-rangeslider/tarball/master" class="button"> <small>Download</small> .tar.gz file </a> <p class="repo-owner"><a href="https://github.com/danielcrisp/angular-rangeslider"></a> is maintained by <a href="https://github.com/danielcrisp">danielcrisp</a>.</p> <p>This page was generated by <a href="https://pages.github.com">GitHub Pages</a> using the Architect theme by <a href="https://twitter.com/jasonlong">Jason Long</a>.</p> </aside> </div> </div> </body> </html>