Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple jQuery Star Rating and AngularJs (ng-repeat)

Oh Boy, Oh Boy

I want to use this plugin http://www.jqueryscript.net/other/Simple-jQuery-Star-Rating-System-For-Bootstrap-3.html together with angularjs.

As long as I use a JSON that is initilized when I first time load the page it is working. When I modify or even just add elements to an array, the star system is not working anymore. It took me like forever to come so far to detect the error but I am not able to solve.

Here is what it look like when I push new elements.

enter image description here

When I looked at the google chrome elements I could see that the new generated Star Ratings are missing a bunch of code.

Generated when first time load the page

<div class="star-rating rating-md rating-active"><div class="clear-rating clear-rating-active" title="Clear"><i class="glyphicon glyphicon-minus-sign"></i></div><div class="rating-container rating-gly-star" data-content=""><div class="rating-stars" data-content="" style="width: 142px;"></div><input value="4" type="number" class="rating form-control hide" min="0" max="5" step="0.5"></div><div class="caption"><span class="label label-primary">Four Stars</span></div></div>

After I push a button to push new elements, new code looks like this

<input value="4" type="number" class="rating" min="0" max="5" step="0.5">

html code

 <div ng-repeat="x in allRecords"  >
                        <input id="input-21d" value="4" type="number" class="rating" min=0 max=5 step=0.5 data-size="sm">
                    </div>

Controller Init

Edi: My controller and module set up look like this, please consider this if you answer :)

var myApp  = angular.module('myApp', ['ngAnimate'])


 myApp.controller("ContactController", function ($scope,  $sce, $http, $timeout) {

$scope.allRecords = [
                 {
                     "Name" : "Alfreds Futterkiste",
                     "City" : "Berlin",
                     "Country" : "Germany"
                 },
                 {
                     "Name" : "Berglunds snabbköp",
                     "City" : "Luleå",
                     "Country" : "Sweden"
                 },
                 {
                     "Name" : "Magazzini Alimentari Riuniti",
                     "City" : "Bergamo",
                     "Country" : "Italy"
                 },
                 {
                     "Name" : "Wolski Zajazd",
                     "City" : "Warszawa",
                     "Country" : "Poland"
                 }
             ]
             ;


    $scope.addElements= function() {
    var obj = { Name: 'Hello world!' };
                $scope.allRecords.push(obj);
};

});

SOLUTION After adding new elements I simply reloaded the star-rating.js

  $(function() {
                function load_script() {
                    $('#myscript').remove();
                    $.getScript("js/star-rating.js?s=", function() {
                        $('script:last').attr('id', 'myscript');                            
                    });
                }
                load_script();
            });

I guess it is not the best way but I so far I do not know how else to do this. And because the star-rating.js is only 22kb big, there shouldnt be later any performance issues.

Edit 2 I switched to this Star Rating. http://rateit.codeplex.com/

But the "refresh problem" remains, this means I have to reload the .js in this case "jquery.rateit.js". I can live with that if I have more time and patience I am going to make it better :D

like image 267
Tower Jimmy Avatar asked Nov 10 '22 13:11

Tower Jimmy


1 Answers

everytime you add new object, angularjs just add new input element, which jquery-rating does not know about that, in order to integrate with angularjs, you have to create a new directive, like this:

var myApp = angular.module('myApp', ['ngAnimate']);
myApp.controller("ContactController", function ($scope,  $sce, $http, $timeout) {
       $scope.allRecords = [
         {
             "Name": "Alfreds Futterkiste",
             "City": "Berlin",
             "Country": "Germany"
         },
         {
             "Name": "Berglunds snabbköp",
             "City": "Luleå",
             "Country": "Sweden"
         },
         {
             "Name": "Magazzini Alimentari Riuniti",
             "City": "Bergamo",
             "Country": "Italy"
         },
         {
             "Name": "Wolski Zajazd",
             "City": "Warszawa",
             "Country": "Poland"
         }
       ];

       $scope.addElements = function () {
           var obj = { Name: 'Hello world!' };
           $scope.allRecords.push(obj);
       }
   }).directive('myStart', function () {
       return function (scope, element, attrs) {
           $(element).rating();
       };
   });

and them html (remove class rating, so jquery-rating does not automatically create Rating):

<div ng-app="App1" ng-controller="Controller1">
<input ng-repeat="x in allRecords" my-start  id="rating-system" type="number"  min="1" max="5" step="1">
 <button ng-click="addElements()">Click me</button>

so every time, an input tag created, we will call $.rating, to apply to that new input.

like image 84
nhabuiduc Avatar answered Nov 14 '22 23:11

nhabuiduc