user4217999
user4217999

Reputation:

How to calculate and display ratings for font-awesome using jQuery

I am trying to display the stars and append the <i> tag dynamically based on the count.

It's working fine, but the problem is if it has floating value then it displays the star in full, I need the star to be half (CSS class fa-star-half-o).

This is what I tried:

var ratingValue = 3.489;

for (var j = 0; j < ratingValue; j++) {
  $(".rating").append('<i class="fa fa-star" aria-hidden="true"></i>');
}
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css" rel="stylesheet"/>
<div class="rating">
</div>

Upvotes: 8

Views: 2848

Answers (5)

madalinivascu
madalinivascu

Reputation: 32354

try using some math

var ratingValue = 3.489;
var roundedValue = Math.trunc(ratingValue);

for (var j = 0; j < roundedValue; j++) {
  $(".rating").append('<i class="fa fa-star" aria-hidden="true"></i>');
}
var k = 0;
if (ratingValue -roundedValue  > 0.4 && ratingValue -roundedValue < 1) {
  k = 1;
  $(".rating").append('<i class="fa fa-star-half-o" aria-hidden="true"></i>');
}
for (var i = Math.trunc(ratingValue)+k; i < 5; i++) {
  $(".rating").append('<i class="fa fa-star-o" aria-hidden="true"></i>');
}

https://jsfiddle.net/8vmbc1a7/4/

Upvotes: 4

Jaydo
Jaydo

Reputation: 1859

If j <= ratingValue add a full star, else if j < ratingValue + 1 add a half-star, else add an empty star.

var ratingValue = 3.489;

for (var j = 1; j <= 5; j++) {
  $(".rating").append('<i class="fa fa-star' + ((j <= ratingValue) ? '' : ((j < ratingValue + 1) ? '-half-o' : '-o')) + '" aria-hidden="true"></i>');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css" rel="stylesheet" />

<div class="rating">
</div>

Upvotes: 3

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

You can achieve what you want like below,

var ratingValue = 3.489, rounded = (ratingValue | 0);
var decimal = ratingValue - rounded, $rating = $(".rating");

for (var j = 0; j < rounded ; j++) {
   $rating.append('<i class="fa fa-star" aria-hidden="true"></i>');
}

if(decimal) {
  $rating.append('<i class="fa fa-star-half" aria-hidden="true"></i>');
}

DEMO

Edit as per your new requirement,

There is no need for 2 different for loops (simple math is enough)

var ratingValue = 3.9, rounded = (ratingValue | 0);

for (var j = 0; j < 5 ; j++) {
  $(".rating").append(
         '<i class="fa '+ ((j < rounded) 
                  ? "fa-star" 
                  : ((((ratingValue - j) > 0) && ((ratingValue - j) < 1)) 
                     ? "fa-star-half-o" 
                     : "fa-star-o")) 
         +'" aria-hidden="true"></i>');
}

DEMO

And to make the code more readable, we can do like this,

var ratingValue = 1.9,
  rounded = (ratingValue | 0),
  str;

for (var j = 0; j < 5; j++) {
  str = '<i class="fa ';
  if (j < rounded) {
    str += "fa-star";
  } else if ((ratingValue - j) > 0 && (ratingValue - j) < 1) {
    str += "fa-star-half-o";
  } else {
    str += "fa-star-o";
  }
  str += '" aria-hidden="true"></i>';
  $(".rating").append(str);
}

DEMO

Upvotes: 6

Alex Art.
Alex Art.

Reputation: 8781

You can round down the value and calculate the difference between the rounded value and the actual rating value: var dif = ratingValue - roundValue

  • if dif > 0.5 display a full star
  • if 0.5 > dif > 0.1 display a half star
  • if dif < 0.1 no additional stars displayed

    var ratingValue = 3.489;
    var floorVal = Math.floor(ratingValue);
    for(var j=0; j<floorVal; j++){
       $(".rating").append( '<i class="fa fa-star" aria-hidden="true"></i>' );
    }
    var dif = ratingValue - floorVal;
    if(dif  > 0.5)
    {
       $(".rating").append( '<i class="fa fa-star" aria-hidden="true"></i>' )
    }
    else if(dif > 0.1)
    {
       $(".rating").append( '<i class="fa fa-star-half-o" aria-hidden="true"></i>' );
    }
    

Fiddle

Upvotes: 0

Himel Nag Rana
Himel Nag Rana

Reputation: 744

updated your fiddle: https://jsfiddle.net/32L669tv/2/ take a look.

The code:

var ratingValue = 3.489;
var intRatingVal = parseInt(ratingValue);

for(var j=0; j < intRatingVal; j++){
  $(".rating").append( '<i class="fa fa-star" aria-hidden="true"></i>' );
}

if ((ratingValue - intRatingVal) > 0) {
    $(".rating").append( '<i class="fa fa-star-half-o" aria-hidden="true"></i>' );
}

The basic thing is -- if there are floating number in rating -- you don't need a single loop to show half star. just show full star based on full part and then half based on if there are any floats.

Upvotes: 1

Related Questions