New jQuery Rater Plugin for Star Ratings

Posted in ASP.NET, BlogSvc, C#, jQuery by Jarrett on 9/29/2008 1:37:00 AM - CST

The jQuery Star Rating widget is a neat control to add to your website.  It has many options that allow you to easily customize it.  However, it’s usage is based on a fully degradable form submission model which means you must use markup containing an option list.  Unfortunately, this means it becomes quite complex to support a rating model that fires off an ajax request to submit a new rating.  Therefore, the developer is left to worry about:

  • Writing code on the server to generate the option list only for users that have not already rated
  • If the user has already rated, the server must generate different markup, or set the widget to disabled
  • The developer must write javascript to handle the widget callback and build an ajax request
  • No built in support for rating count and updated rating result
  • No built in support for a failed rating

To make life easier, I wrote the jQuery Rater Plugin that should reduce the complexity of implementing an ajax rating scenario. 

jQuery Rater Plugin Demo

Features

  • Auto Ajax posting
  • Supports rating update and rating count
  • Shows previous rating before user has rated
  • Markup is same for rated and unrated
  • Supports step ratings (partial stars)

View the Demo Page

First, lets take a look at the markup which is contained in an ASP.NET user control.  However, you could write this in your server language of choice.  There are three things being set below.  The width of the stars, the current rating, and the count of ratings.

RaterMarkup

The only markup elements required by the plugin are the “ui-rater-starsOff” and “ui-rater-starsOn” spans.  Also, they must be contained within an element that has an id you can reference.

We only need to activate the plugin when the user has not already rated the entry.

SetupPlugin

The only option I’ve set is the address to post the rating to.  When the user performs a rating, the plugin will automatically build an ajax request to the postHref.  It includes the id and chosen rating value in the form post data.

This plugin does more with less code than the existing jQuery Star Rating Widget because it foregoes the degrade-ability in favor of ajax.

  • Previous Javascript size: ui.core.packed.js 4KB + ui.stars.pack.js 4KB + rate.js 2KB = 10KB
  • New Javascript size: jquery.rater.js 3KB (unpacked)

View the source code

To see a full server implementation, see the source code of BlogSvc.net.

Download jQuery Rater Plugin (javascript + demo html + image + sample server page)

Found this plugin useful? Please kick it - kick it on DotNetKicks.com

Comments

Comments Feed
Posted by DotNetKicks on 9/22/2008 12:01:28 AM - CST
DotNetKicks
You've been kicked! A good thing.
Posted by Daniel on 10/5/2008 9:52:59 PM - CST
Daniel
Hi Jarrett, I'm liking the plug in so far - how would I go about using multiple ratings on my page and passing an id for the relevant rating div to my ASHX on the query string? I'm guessing, something like: $('.testRater').rater({ postHref: 'http://blah/rateThis.ashx?id=' + $(this).attr('id') }); ... Thanks, D
Posted by Daniel on 10/5/2008 10:12:04 PM - CST
Daniel
aha - to answer my own question, put the ID on the div and find all such div's by marking them with the class 'testRater': $('.testRater').each(function(){ $(this).rater({ postHref: 'http://blah/rateThis.ashx?id=' + $(this).attr('id') }); }); Thanks Jarrett! D
Posted by Jarrett on 10/8/2008 12:22:17 PM - CST
Jarrett
Hey Daniel, glad you figured it out. Also, the plugin will automatically post the id along with the rating during the ajax request so it isn't necessary (unless you want) to use a querystring.
Posted by Dino Baskovic on 11/8/2008 5:30:32 PM - CST
Dino Baskovic
I noticed on jquery's site they use 'rate.php' for the callback. I take it you're a .NET guy. Have you come across any good PHP examples? I can write my own, but rather not take the time if one it already in the wild. Otherwise, GREAT work here!
Posted by Ian Serlin on 11/13/2008 3:43:16 AM - CST
Ian Serlin
Hey Jarret, Thanks for the plugin, short and sweet, I like it. I made a 5 line modification that allows the rater to work even if it's initially contained within a hidden element (for instance, roll-over the content to see the rater). The problem is that if .rater(...) is called on a hidden element then .height() and .width() are both 0, at least in FF3 and thus opts.size is 0. If you'd like the code just shoot me an email. Ian Serlin
Posted by co. on 11/18/2008 6:42:51 PM - CST
co.
firstly, great script. i have chosen it for as default for voting. now, how to disable voting when already voted? i get information from cookie, but cant figure out how to make javascript disable onmouseovers
Posted by flyaflya on 11/27/2008 8:49:45 PM - CST
flyaflya
bugs in ie:1.when move the mouse to tail, the rating image became very long, I guess line: width = Math.ceil(width / (opts.size / opts.step)) * opts.size / opts.step; can't work correctly in ie, so I insert a line, if (width > 90) width = 90; after it, then It work fine. 2.start.png has a shadow in ie, ie can't support png perfectly, convert it to gif can fix it.
Posted by flyaflya on 11/27/2008 10:34:54 PM - CST
flyaflya
another bug is in line 11:if (opts.id == 'undefined') opts.id = $this.attr('id'); should remove comma like this: if (opts.id == undefined) opts.id = $this.attr('id');
Posted by norbert on 12/1/2008 12:21:57 PM - CST
norbert
Big thanks! Life with u is much more easier!
Posted by Jarrett on 12/13/2008 12:55:44 AM - CST
Jarrett
I'm testing an updated version 1.1 of the rater plugin. It includes various fixes pointed out above.
Posted by Jarrett on 12/22/2008 9:37:40 AM - CST
Jarrett
I just noticed there are some patches for this plugin on the jQuery site.
Trackback from BlogSvc News on 12/24/2008 12:55:21 PM - CST

Wizard Release 0.8

The eighth release of BlogSvc includes a new setup wizard. Also, this release contains the…
Posted by chosen on 1/4/2009 11:46:49 AM - CST
chosen
man, your example is not working for me, I copied the complete code and just change the ID and there is nothing working with it, writing my own now -.-

Add Comment

Please enter your name.
will not be published, shows your gravatar Please enter a valid email.
Please enter a valid website.
Please supply a comment.
4.0 (98)
on 12/22/2008 9:34:12 AM - CST