Displaying star rating inside DataTables

Displaying star rating inside DataTables

GstgGstg Posts: 66Questions: 4Answers: 0
edited February 2022 in Free community support

I've done some searching, and I'm just not seeing, what I think is an easy solution to the display I"m trying to facilitate in DataTables:

1 - I'm trying to display a rating as X checked stars and (5-X) unchecked stars

Here is how I do it in php code:

echo '<br>Rating: '.str_repeat('<span class="fa fa-star checked"></span>', intval($review_rating+.5)). str_repeat('<span class="fa fa-star"></span>', (5-intval($review_rating+.5))). '('.$num_of_reviews.')';

2 - I currently am using 2 sets of code:

       A - `{  data: 'rating',
        render: function ( data, type, row ) {
            return row.review_rating;
            }
    },`

       B - `(table.column(6).visible() ? '<a href="/profile_reviews/?v=' + d.user_id + '"> <b>Rating:</b> ' + d.review_rating + '</a><br>' : '')+`  

I'm trying to figure out how to implement the function below into the A & B code sets:

str_repeat('<span class="fa fa-star checked"></span>', intval($review_rating+.5))

Thank so much for any help

Replies

  • kthorngrenkthorngren Posts: 21,183Questions: 26Answers: 4,925

    Use Javascript string repeat(). Looks like str_repeat() is a PHP method which won't work.

    Change intval($review_rating+.5) to intval(row.review_rating+.5). See the parameter docs in the columns.render docs. row contains the full row data.

    Kevin

  • GstgGstg Posts: 66Questions: 4Answers: 0

    Thank you for responding so quickly.

    I reviewed the colums.render options and found the <span> option, but not how to repeat the span for X times inside the functions. Thoughts?

    "render": function ( data, type, row, meta ) { return type === 'display' && data.length > 40 ? '<span title="'+data+'">'+data.substr( 0, 38 )+'...</span>' : data;

    Is there another way to repeat <span> for intval(row.review_rating+.5) times?

    As mentioned I'm looking to display X number of colored stars and (5-X) number of non-colored stars to show a review.

    Thanks

  • kthorngrenkthorngren Posts: 21,183Questions: 26Answers: 4,925
  • GstgGstg Posts: 66Questions: 4Answers: 0

    That worked great!! Thanks

Sign In or Register to comment.