antennaio / jquery-bar-rating

jQuery Bar Rating Plugin - minimal, light-weight jQuery ratings.
http://antennaio.github.io/jquery-bar-rating/
MIT License
737 stars 260 forks source link

How to initialize this plugin using class instead of id #80

Closed jitheshkt closed 7 years ago

jitheshkt commented 7 years ago

All the provided examples are using id as selector and passed 'initialRating' in the initializing function. But in my case I have a loop of start rating available objects. Looping entire initialization is not ideal. So I have used class to initialize and it works. But now the 'initialRating' is hard coded. I have current rating of each my item in 'data-current-rating' attribute. How to get this value in the initialization ?

Here is my code :

$('.shop-rating').barrating({
            theme: 'fontawesome-stars-o',
            showSelectedRating: false,
            initialRating: '3', //I want to get this from 'data-current-rating'.
            onSelect: function(value, text, event) {
                var el          = this;
                var shop_id     = el.$elem.data('shop-id');
                if (!value) {
                    /*$('#example-fontawesome-o')
                        .barrating('clear');*/
                } else {
                    $.ajaxSetup({
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        }
                    });
                    $.ajax({
                            type: "POST",
                            url: '{!! route('doRating') !!}',
                            data: {'shop_id':shop_id, 'rating' : value},
                            success: function(response) {
                              console.log(response);
                            }
                          },'json');
                }
            }
        });
jitheshkt commented 7 years ago

Never mind, I managed it with a each function. Something like

  $('.shop-rating').each(function(index, value) {
  var currentRating     = $(this).data('current-rating');
  $('.shop-rating').barrating({