You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
After searching for existing widgets, I found three categories of them:
12
+
13
+
- The ones that depends on PNG images.
14
+
- The ones that adds A LOT of JavaScript and CSS code to my project.
15
+
- The ones that adds A LOT of JavaScript and CSS code and depends on PNG images.
16
+
17
+
I don't want to add a whole multipurpose library just to put a few stars in my interface, I want my rating stars to look awesome in retina screens without worrying about image versions and dynamically replacing them, and Bootstrap already includes a set of beautifully designed vectorial icons by Glyphicons, so I thought I could create something simpler.
18
+
19
+
## Ok, enough talking, tell me how this thing works!
20
+
21
+
Download `bootstrap-rating-input.min.js` and put it wherever you usually put JavaScripts in your project. Include it on pages where you want to have forms with ratings:
That's all! When page loads, you'll find a few stars where you'd expect to find the input. It works just like most of rating plugins, but you don't need to learn anything about options or initializations, it just works out of the box.
30
+
31
+
### Wait, where has my input gone?
32
+
33
+
The plugin replaces your number input by a hidden field with identical name and id and adds interactive stars that will catch your clicks and save the selected values into the hidden field. In this way the form can be submitted or value readed by jQuery normally.
34
+
35
+
### Nice, but I want to use a different number of stars
36
+
37
+
Sure! You can set min and max values adding `data-min` and `data-max`:
By default once you set a value it remains set and you can only change it by another, but you can add a clear link by just defining the `data-clearable` attribute:
The content of `data-clearable` will appear as label for the link. You can set a space or a   to make it appear as a naked close icon.
48
+
49
+
### I don't want to be forced to add the `rating` class to the inputs
50
+
51
+
The `rating` class is used in combination with `input[type=number]` to let you autoload the rating plugin without coding anything, but you can apply this plugin to a input of any type by executing the method `rating` on a jQuery selection:
52
+
53
+
$('input.my_class').rating();
54
+
55
+
## Requirements
56
+
57
+
You know... [Twitter Bootstrap](http://twitter.github.io/bootstrap) and [jQuery](http://jquery.com)!
58
+
59
+
## Can I generate read-only stars for displaying?
60
+
61
+
If you think about it you don't want to use a plugin to generate static HTML code that is as simple as this:
You can easily generate such code with your favourite template engine and a loop. With Ruby and HAML it could look like this:
66
+
67
+
/ Given a variable val with the value you want to represent and a variable max that contains the maximum number of stars:
68
+
- max.times do |i|
69
+
%i{class: "icon-star#{'-empty' if i>val}"}
70
+
71
+
Well, HAML is awesome, but you are a programmer, so you'll be able to addapt this to your favorite language...
72
+
73
+
## It looks nice, but I want to complain because it doesn't fit my favorite use case
74
+
75
+
I have implemented this for my project in my environment and sharing it for free. Leave me an issue with your suggestions and I'll eventually push a fix, but this is MIT licensed, so you're welcome to fork this project, do pull requests with fixes and improvements, reimplement better versions of it for your own or do whatever you want, I'll be happy if it becomes useful or inspires at least one more person.
0 commit comments