Code Monkey home page Code Monkey logo

angular-rangeslider's Introduction

angular-rangeslider

Current version: 0.0.14

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

It has been styled to match form elements styled by Twitter's Bootstrap CSS framework.

Requirements

  • Angular (v1.0.8+)
  • jQuery (v1.7+)

Looking for collaborators

This directive was written for a project completed way back in 2013. I've not needed to use it since and have no plans (or time) to continue development / maintenance.

I'm sure there are better rangesliders out there these days.

If anyone would like to become a collaborator please let me know: @iamdanielcrisp

Installation

Download the files from Github or use Bower:

$ bower install angular-rangeslider

Add the JS and CSS to your page:

<script src="bower_components/angular-rangeslider/angular.rangeSlider.js"></script>
<link rel="stylesheet" href="bower_components/angular-rangeslider/angular.rangeSlider.css">

Add the ui-rangeSlider module as a dependency for your app: angular.module('myApp', ['ui-rangeSlider']);

Bootstrap is not required.

If you use SCSS & Compass you can include the source SCSS directly into your project CSS if you add bower_components to your include path:

@import "angular-rangeslider/scss/rangeSlider"; // requires Compass

Demo

GitHub Pages

Project page

Quick example

A basic slider with a range of 0 to 100:

<div range-slider min="0" max="100" model-min="min" model-max="max"></div>

As the handles are moved the model values min and max will be updated in the parent controller.

Default example

Options

Options are set as attributes on the <div range-slider>

= two-way bindings

min - the minimum value the user can select (must be a number, can be a model property)

max - the maximum value the user can select (must be a number, can be a model property)

model-min - the model property for the min value, represents the position of the min handle

model-max - the model property for the max value, represents the position of the max handle

disabled - model property or boolean, disables the slider when true

@ attributes

orientation - slider orientation, default: 'horizontal' - options: 'horizontal' | 'vertical' | 'vertical left' | 'vertical right'

step - amount to change the value by when moving a handle, default: 0

decimal-places - the number of decimal places to round to, default: 0

filter - a built-in filter to apply to the displayed values, for example currency or currency:'$'

filter-options - options to pass to the filter

pin-handle - disable/hide one handle, default: null - options: 'min' | 'max'

prevent-equal-min-max - prevent the min and max values from being equal. The step value is used to set the minimum difference, otherwise a value of 1 is used.

attach-handle-values - move the value labels in sync with the slider handles when true, default: false

on-handle-up - call a function whenever a handle is released

on-handle-down - call a function whenever a handle is grabbed

getter-setter - enable getter / setter support for model values - options: true | false

Some more examples

Using model properties

The following properties are present in the scope:

// 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;

So we can include the directive in the HTML like this:

<div range-slider min="minPrice" max="maxPrice" model-min="userMinPrice" model-max="userMaxPrice" step="5"></div>

As the user moves the min and max handles the userMinPrice and userMaxPrice will be updated in increments of 5 in real-time in the model.

Using filters

Continuing from the example above we can format the values displayed to the user as currency.

<div range-slider min="minPrice" max="maxPrice" model-min="userMinPrice" model-max="userMaxPrice" step="5" filter="currency"></div>

This will automatically be localised by Angular, but we can force it to be USD by passing this as an option:

<div range-slider min="minPrice" max="maxPrice" model-min="userMinPrice" model-max="userMaxPrice" step="5" filter="currency" filter-options="USD$"></div>

Alternatively you can use Angular's filter notation directly in the filter attribute, such as filter="currency:'GBP £'", which would result in values like this: GBP £7,500.00.

<div range-slider min="minPrice" max="maxPrice" model-min="userMinPrice" model-max="userMaxPrice" step="5" filter="currency:'GBP £'"></div>

NOTE: If the filter-options attribute is defined you cannot use Angular filter notation. You must only use the filter name in the filter attribute.

Currency example

Making the slider vertical

Simply add one of the following values to the orientation attribute: 'vertical', 'vertical left' or 'vertical right'.

This will create a vertical slider that is centred in it's parent element:

<div range-slider min="0" max="100" model-min="min" model-max="max" orientation="vertical"></div>

Vertical example

To left-align the slider use 'vertical left':

<div range-slider min="0" max="100" model-min="min" model-max="max" orientation="vertical left"></div>

And to right-align the slider use 'vertical right':

<div range-slider min="0" max="100" model-min="min" model-max="max" orientation="vertical right"></div>

Disabling the slider

If you have a boolean property in your scope you can simply change this value to true to disable the slider:

$scope.sliderDisabled = false;

And then specify the property using the disabled attribute:

<div range-slider min="0" max="100" model-min="min" model-max="max" disabled="sliderDisabled"></div>

// clicking this button will toggle the sliderDisabled value between true and false
<button ng-click="sliderDisabled=!sliderDisabled">Toggle slider disabled status</button>

Disabled example

Pinning a handle

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:

<div range-slider min="0" max="100" model-max="max" pin-handle="min></div>

Pinned example

Move values with handles

Set the attach-handle-values attribute to true to have the values move with the slider handles. This works for either horizontal:

<div range-slider min="0" max="100" model-min="min" model-max="max" attach-handle-values="true"></div>

Attached handles horizontal example

or vertical:

<div range-slider min="0" max="100" model-min="min" model-max="max" attach-handle-values="true" orientation="vertical"></div>

Attached handles vertical example

Angular 1.0.* Support

If you use this directive with an older version of Angular (e.g. v1.0.*) then the directive automatically detects this and switches on legacy support.

This basically changes the optional isolate scope properties disabled, modelMin and modelMax so that they are no longer optional and must always be defined on the directive element.

So, this would give an error (Error: Non-assignable model expression: undefined (directive: rangeSlider)) if you were using Angular v1.0.8:

<div range-slider min="0" max="100" model-min="demo1.min" model-max="demo1.max"></div>

However this would work correctly:

<div range-slider min="0" max="100" model-min="demo1.min" model-max="demo1.max" disabled="false"></div>

Note, only tested in v1.0.8

To Do

  • Remove full jQuery dependency
  • Improve behaviour when model values are not valid (e.g. min is greater than max)
  • Improve the dev architecture (add jshint, tests, minification, auto-versioning etc)

Known Issues

  • The slider restricts the model value when editing outside the slider (e.g. in an ) but the only notification is made to the console
  • The min slider handle disappears behind the max slider handle

Credits

This was originally forked from Léon Gersen's brilliant noUiSlider:

https://github.com/leongersen/noUiSlider

Licence

This code is released under the MIT Licence

Copyright (c) 2013 Daniel Crisp

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.

angular-rangeslider's People

Contributors

baldurh avatar danielcrisp avatar devilshaircut avatar gabro avatar krausestefan avatar niyando avatar thejoebiz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

angular-rangeslider's Issues

Slider works but not seeing min & max values at either end

Maybe I'm missing something but Slider works but not seeing min & max values at either end. Have styling on the slider showing fine and can manipulate the slider but just missing the min/max values at either end under the slider.
Have added my own divs with binding to values which works but I'm pretty sure I should be seeing it by default...

Remove prevent-equal-min-max's step dependency

I'm attempting to set a buffer between the sliders and its larger than the Step value that I want. The depencency of the prevent-equal-min-max on the step value means I cannot customize it (right?)

How to set a custom template?

Would it be feasible to let users to add a custom template?
I need to use your range selector in a form-group, so I need to show values inline (inside the control).
So I thought I could put the values on the sliders, removing the arrows.

This is a screenshot of the result:

screenshot

To accomplish this I had to modify css and the template.
These are the simple changes:

custom.css:

.ngrs-range-slider {
  margin: 0;
  padding-right: 10px;
}
.ngrs-range-slider .ngrs-runner {
  height: 22px;
}
.ngrs-range-slider .ngrs-handle {
  width: 24px;
  text-align: right;
  padding-right: 6px;
}
.ngrs-range-slider .ngrs-handle-min i {
  background-image: url("");
}
.ngrs-range-slider .ngrs-handle-max i {
  background-image: url("");
}
.ngrs-value-inline {
  font-size: .9em;
}

template:

template: ['<div class="ngrs-range-slider">',
                '<div class="ngrs-runner">',
                '<div class="ngrs-handle ngrs-handle-min"><i class="ngrs-value-inline">{{filteredModelMin}}</i></div>',
                '<div class="ngrs-handle ngrs-handle-max"><i class="ngrs-value-inline">{{filteredModelMax}}</i></div>',
                '<div class="ngrs-join"></div>',
                '</div>',
                '<div class="ngrs-value-runner">',
                '<div class="ngrs-value ngrs-value-min" ng-show="showValues"><div>{{filteredModelMin}}</div></div>',
                '<div class="ngrs-value ngrs-value-max" ng-show="showValues"><div>{{filteredModelMax}}</div></div>',
                '</div>',
                '</div>'
            ].join(''),

The css can be put in a .css file to be loaded after the original one.
The template is instead currently hardcoded in js script.
Would it be possible to add a parameter for users to load a custom template?
Or, in alternative, could you think of an option like "showValuesInHandles"?

Add a direction option so on vertical orientation values can be bottom-to-top.

I need to add the slider but on vertical orientation with values from bottom-to-top. Currently it supports only top-to-bottom. Is it correct?

Update: I am able to workaround that by setting negative numbers for min/max/model-min but it would be nice to have an option to do that automatically.

<div range-slider
    min="-100"
    max="-30"
    model-min="model.zoomValue"
    orientation="vertical"
    on-handle-up="zoomChart()"
    pin-handle="max"
    show-values="false">
</div>

How to remove decimal point in slider min/max values

Hi,

How can I remove the decimal numbers from the min and max range values. I set the min to "50" and max to "10000" but the slider shows "50.00" and "10000.00" , I need "50" and "10000".

How would I do this?

This is my code at the moment:

<div range-slider min="demo.minPrice" max="demo.maxPrice" model-min="sliderRanges.min" model-max="sliderRanges.max" decimal-places="0" filter="currency" filter-options="£" step="10" disabled="false"></div>

Thank you in advance :)

Compatibility with angular 1.2.10

The current version of the rangeslider does not work when using angular 1.2.10.
The following error message will be thrown:

TypeError: Object [object Object] has no method 'show'
at Object.setPinHandle as fn

Can the j

can it be done, can someone make a PR ?

Angular 1.3

Does it work with Angular 1.3, cause I'm having some issues to implement this in my project.

HTML

<div range-slider min="price_min" max="price_max" model-min="price_min_handler" model-max="price_max_handler" disabled="false"></div>

JS

$scope.prices = [20,30,45,50,52,110,130,200,220];

    $scope.price_min = $scope.prices[0];
    $scope.price_max = $scope.prices[$scope.prices.length - 1];

    $scope.price_min_handler = $scope.price_min;
    $scope.price_max_handler = $scope.price_max;

I'm getting no errors yet the slider doesn't work.

Possibility to add fixed text value along the line ?

Hi Daniel,

I like very much your directive for our website and a great thank for that!

I have a question for a new feature. Do you think it would be possible to add some fixed text value along the line and keeping "magnetic" cursor to force user to choose only those ones? I did an example with Photoshop into attachment.

Thank,

have a nice day.

range-slider

Problem with z-index when modelMin is same as max on initial page load.

There is some logic in the handleMove function that adds a z-index to the handle if it's over a certain percentage, but it doesn't handle if the modelMin is over that percentage on initial page load.

I'm adding this:

if (scope.modelMin === scope.max) {
    angular.element(handles[0]).css('z-index', 3);
}

right before the end of the setModelMinMax function as a workaround for now, but I don't think that handles all cases or orientations.

not working in angularJs 1.4.0

To me the library doesnt work in angularJS 1.4.0 stable. Could you fix this? handlers show up but cannot move them. Thank you. You have a lightweight library that's why i like it. I tryed in 1.3.15, works. Bow install 1.4, doesn't work anymore.

use filter to convert value to hh:mm

I would like to display a range between two hh:mm values.
What I did now, was converting the values into numbers representing the minutes, like:

  • 00:00 = 0
  • 23:59 = 1439

is it possible converting the display of the values to something like:
parseInt(time/60)+':'+((time%60 > 10) ? (time%60) : '0'+(time%60)
using the filter?
I couldn't find any help on how to use the filter, except for the currency example.

Suggestion: invisible hitbox for mobile.

On mobile devices moving the sliders is difficult. An improvement would be to have an invisible ‘hitbox’ around the handles to improve the mobile user experience.

Gives error : TypeError: Object [object Object] has no method 'hide'

TypeError: Object [object Object] has no method 'hide'
at Object.setPinHandle (http://localhost/bower_components/angular-rangeslider/angular.rangeSlider.js:184:53)
at Object.applyFunction as fn
at Scope.$digest (http://localhost/bower_components/angular/angular.js:11808:29)
at Scope.$delegate.proto.$digest (:844:31)
at Scope.$apply (http://localhost/bower_components/angular/angular.js:12061:24)
at Scope.$delegate.proto.$apply (:855:30)
at done (http://localhost/bower_components/angular/angular.js:7843:45)
at completeRequest (http://localhost/bower_components/angular/angular.js:8026:7)
at XMLHttpRequest.xhr.onreadystatechange (http://localhost/bower_components/angular/angular.js:7982:11)

Angular version : 1.2.10
Jquery version : 2.1.0

Any idea ?

onHandleUp/onHandleDown doesn't call $apply

I used onHandleUp to update my model only after the user drops the slider; however, the angular $apply isn't called by default which makes the parameter somewhat non-angular. I think it should be called like this

 if (angular.isFunction(scope.onHandleUp)) {
      scope.$apply(function() {
             scope.onHandleUp();
      });
  }

And similarly for onHandleDown.

License missing

It would be beneficial to include license with this repository so other developers know on what terms we can use your directive.

Suggestion: Support snapping to values in a small array when the range of possible discrete values is limited

I have a collection of filters containing many ranges some of which are continuously variable and others of which are discrete and have a very limited number (5 - 10) of possible values. The continuously variable ranges are a natural fit for the range-slider. And the discrete ranges could also potentially be displayed using the range-slider. While the range-slider can indeed be used to display a small set of discrete values, the user-experience is not ideal because the (continuous) range-slider metaphor suggests to the user that all of the values including values between the discrete points are also valid. So I find that a more intuitive user experience is provided by showing the user a drop-down list of the discrete values. Nevertheless I think the range metaphor is quite helpful and intuitive when there are neither too few nor too many discrete values and when the user is typically wishing to select a range of them. But it is a much better experience for the user if the range snaps to a valid value. And it also provides feedback as to what that value is.

Handle up/down events not working

I'm using the following slider definition

<div range-slider
                    step='6'
                    onHandleUp='handleUp'
                    onHandleDown='handleDown'
                    min="sliderAge.min"
                    max="sliderAge.max"
                    model-max="sliderAge.maxModel"
                    model-min="sliderAge.minModel"

                    ></div>

while in my controller, I've got the following two functions:

    $scope.handleUp = function() {
        console.log("Handle up");
    }
    $scope.handleDown = function() {
        console.log("Handle down");
    }

Unfortuanately I can't see "Handle up" nor "Handle down". Could anybody elaborate ? Also, the documentation needs to be updated to reflect the onHandleUp/down usage (i'm not sure if I am using it right!)

Does range-slider supports non-integer steps?

Hi!
I have an array of values lying between 0 and 1.
Is it possible to set step to 0.01 to enable user to set the slider to [0.50;1.00]?
It seems to me, that it does not work this way: step = "0.01", could you suggest something?
Thank you!

Step Values in Decimal

Hi,

Just wanted to check if the "step" values can be given in decimal. I have "step" values for few sliders ranging from 0.1 to 0.5. I set the value to 0.1 but looks like it is not working. Any idea on how to resolve this ?

Thanks,
Anirban

The mix & max models can have same values.

There should be a way such that we can disallow the min and max models to have the same values. We can have a boolean attribute for the same and max from decreasing (and min from increasing) after difference between them becomes equal to step size.

create tag for new version to support bower

Currently bower will only pull down v0.0.03 as tags are not up to date.

Also, is there a reason there is a dependency on unstable-angular-complete instead of latest stable?

Thanks

Irregular step values

Are irregular step values supported? Currently i found no way to add irregular step values like 1, 5, 7 ,21, 32, 1400.

rangeSlider errors when used in combination with input type="number"

When the rangeSlider is used with an input field of type="number", on any change of the slider, Angular throws errors:

Error: [ngModel:numfmt] Expected 2160 to be a number
http://errors.angularjs.org/1.3.8/ngModel/numfmt?p0=2160
at REGEX_STRING_REGEXP (angular.js:63)
at Array. (angular.js:19863)
at Object.ngModelWatch (angular.js:20971)
at Scope.$get.Scope.$digest (angular.js:14217)
at Scope.$get.Scope.$apply (angular.js:14488)
at HTMLDocument. (angular.rangeSlider.js:583)
at HTMLDocument.jQuery.event.dispatch (jquery.js:5095)
at HTMLDocument.jQuery.event.add.elemData.handle (jquery.js:4766)

The value in the input fiels does update. It all works ok, but still the errors are thrown.

The error is not thrown when the input field is changed directly.
If input field does not have the type specified, there is no issue. It then works correctly, but all HTML5 goodness related to input type="number" is lost.

I created a plunker, which for some reason does not work (another issue?). Can't find the issue in the plunkr, but you can copy that code to chrome and see what I mean. Link to Plnkr: http://plnkr.co/edit/LFtWWGk9FV5XGDTaaPiA?p=info

Is there a way to use rangeSlider in combination with input type="number"?

fractional step

How would I use the slider for a range of 0 to 1 and step 0.1? simply setting step to 0.1 does not work.

Call a function when the slider values have changed

Usually range sliders are used to filter. But I can't determine when the slider values have changed.

ng-changeseems not to work and

$scope.min = 0
$scope.max = 10
$scope.$watch 'min', () -> console.log "hh"

Does also not work for me.

sliders don't respond to model changes

If you change model values in the on-handle-up function, the $watch is not triggered and changes are not visible until you manually move a slider; then all sliders on the page update to their correct positions.

You can see this behavior in this plunker with the three sliders, the third being the sum of the first two, and the second is updated to always be greater than the first.
http://plnkr.co/edit/tQRTWm1GYhSCuOyyoxiv?p=preview

fractionSize filter option for currency

Is there a way to set the fractionSize setting for the currency filter?
Using the common syntax in either filter or filter-options attributes won't work (filter='currency:$:0'or filter-options='$:0')

onMouseUp fires multiple times

I just implemented the range slider. Working well, but i noticed that the onMouseUp event gets called 1 time, the second time i release the handle it gets called 2 times, 3rd 3 times...and so on... The problem(i gues) is that $document.bind(offEvent, ...); is called every time when an handle is pressed. This leads to this that the offEvent is bound X times to a slider and the onMouseUp event gets called X times.

I want to bind an http get task on the onMouseUp, so i realy need the event to fire just once.

My quick fix was to unbind the offEvent every time before bind, but that is not the best solution i gues:
$document.unbind(offEvent);
$document.bind(offEvent, ...)
...

Slider snaps to 100 when initially model-max is not defined

I am noticing that the slider jumps to hundred when the value of model-max is not defined. This happens when I am using a field of a json object to bind to model-max. Something like options = {} and model-max binds to options.something. I am also setting pin-handle to min. Even if was set to 0 when null, it would be still graceful.

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.