How to use Star rating of Font-awesome with Django?

后端 未结 11 1896
挽巷
挽巷 2020-12-23 17:37

Fontawesome has a great star rating css extension, which looks really awesome.

However clicking on any of the span elements wouldn\'t really do anything. I don\'t

相关标签:
11条回答
  • 2020-12-23 17:52

    Consider Star Ratings With Very Little CSS from CssTricks. It uses no images, no Bootstrap, no Javascript. Pure CSS and Unicode stars, see this. Supported by all browsers except IE <= 6.

    Note: Javascript is not required for UI, but is required for sending data to the server and preserving the rating selection after the element loses focus.

    Also take a look at Accessible star rating widget with pure CSS. It is a pure CSS as well. Uses a technique discussed here - Custom radio and checkbox inputs using CSS. It's different from the first one in a way that the rating stays selected after the element loses the focus. Unfortunately, it is supported by fewer browsers.

    A quick note on CSS Selectors might be useful while reading that.

    UPDATE:

    Have just reread the post and clicked the link. The first link in my answer is the same as topic starter's. Apologies.

    Anyway, I chose not to delete the answer, as it contains a few links which I've been very glad to find and which might be useful for others.

    0 讨论(0)
  • 2020-12-23 17:55

    I did not see a simple answer using only bootstrap glyphicons and jquery. I am sure other people have come here looking for a quick copy and paste so I just wrote one.

    $(function(){
        $('.rating-select .btn').on('mouseover', function(){
            $(this).removeClass('btn-default').addClass('btn-warning');
            $(this).prevAll().removeClass('btn-default').addClass('btn-warning');
            $(this).nextAll().removeClass('btn-warning').addClass('btn-default');
        });
    
        $('.rating-select').on('mouseleave', function(){
            active = $(this).parent().find('.selected');
            if(active.length) {
                active.removeClass('btn-default').addClass('btn-warning');
                active.prevAll().removeClass('btn-default').addClass('btn-warning');
                active.nextAll().removeClass('btn-warning').addClass('btn-default');
            } else {
                $(this).find('.btn').removeClass('btn-warning').addClass('btn-default');
            }
        });
    
        $('.rating-select .btn').click(function(){
            if($(this).hasClass('selected')) {
                $('.rating-select .selected').removeClass('selected');
            } else {
                $('.rating-select .selected').removeClass('selected');
                $(this).addClass('selected');
            }
        });
    });
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="rating-select">
        <div class="btn btn-default btn-sm"><span class="glyphicon glyphicon-star-empty"></span></div>
        <div class="btn btn-default btn-sm"><span class="glyphicon glyphicon-star-empty"></span></div>
        <div class="btn btn-default btn-sm"><span class="glyphicon glyphicon-star-empty"></span></div>
        <div class="btn btn-default btn-sm"><span class="glyphicon glyphicon-star-empty"></span></div>
        <div class="btn btn-default btn-sm"><span class="glyphicon glyphicon-star-empty"></span></div>
    </div>

    To set the default value from the DB using django template, for each star do:

    <div class="btn btn-{% if rate.value > 0 %}warning{% else %}default{% endif %}{% if rate.value == 1 %} selected{% endif %} btn-sm"><span class="glyphicon glyphicon-star-empty"></span></div>
    
    0 讨论(0)
  • 2020-12-23 17:59

    I seen this post, but didn't want to use a plugin with more than I needed. So, I threw this together for a small project I'm working on. You don't need bootstrap to use this.

    HTML

    <div class="star-rating"> 
      <span class="fa fa-star-o" data-rating="1"></span>
      <span class="fa fa-star-o" data-rating="2"></span>
      <span class="fa fa-star-o" data-rating="3"></span>
      <span class="fa fa-star-o" data-rating="4"></span>
      <span class="fa fa-star-o" data-rating="5"></span>
      <input type="hidden" name="whatever" class="rating-value" value="3">
    </div>
    

    CSS

    .star-rating {
      line-height:32px;
      font-size:1.25em;
      cursor: pointer;
    }
    

    CoffeeScript

    $star_rating = $('.star-rating .fa')
    
    SetRatingStar = ->
      $star_rating.each ->
        if parseInt($star_rating.siblings('input.rating-value').val()) >= parseInt($(this).data('rating'))
          $(this).removeClass('fa-star-o').addClass('fa-star')
        else
          $(this).removeClass('fa-star').addClass('fa-star-o')
    
    $star_rating.on 'click', ->
        $star_rating.siblings('input.rating-value').val $(this).data('rating')
        SetRatingStar()
    
    SetRatingStar()
    

    Javascript:

    var $star_rating = $('.star-rating .fa');
    
    var SetRatingStar = function() {
      return $star_rating.each(function() {
        if (parseInt($star_rating.siblings('input.rating-value').val()) >= parseInt($(this).data('rating'))) {
          return $(this).removeClass('fa-star-o').addClass('fa-star');
        } else {
          return $(this).removeClass('fa-star').addClass('fa-star-o');
        }
      });
    };
    
    $star_rating.on('click', function() {
      $star_rating.siblings('input.rating-value').val($(this).data('rating'));
      return SetRatingStar();
    });
    
    SetRatingStar();
    
    0 讨论(0)
  • 2020-12-23 18:03

    This is the best plugin for star rating if you are using bootstrap:

    • 2Kb minified
    • Use Bootstrap Glyphicons
    • No extra CSS
    • You just have to add class="rating" to the input

    Github Project

    0 讨论(0)
  • 2020-12-23 18:04

    Have a look at django-ratings - that's what you're looking for.

    0 讨论(0)
  • 2020-12-23 18:06

    One more interesting method. Pure CSS, keeps selected value, uses radio-buttons (cool!) and FA fonts

    See demo

    Taken from this post

    HTML

    <div class="star-rating">
      <div class="star-rating__wrap">
        <input class="star-rating__input" id="star-rating-5" type="radio" name="rating" value="5">
        <label class="star-rating__ico fa fa-star-o fa-lg" for="star-rating-5" title="5 out of 5 stars"></label>
        <input class="star-rating__input" id="star-rating-4" type="radio" name="rating" value="4">
        <label class="star-rating__ico fa fa-star-o fa-lg" for="star-rating-4" title="4 out of 5 stars"></label>
        <input class="star-rating__input" id="star-rating-3" type="radio" name="rating" value="3">
        <label class="star-rating__ico fa fa-star-o fa-lg" for="star-rating-3" title="3 out of 5 stars"></label>
        <input class="star-rating__input" id="star-rating-2" type="radio" name="rating" value="2">
        <label class="star-rating__ico fa fa-star-o fa-lg" for="star-rating-2" title="2 out of 5 stars"></label>
        <input class="star-rating__input" id="star-rating-1" type="radio" name="rating" value="1">
        <label class="star-rating__ico fa fa-star-o fa-lg" for="star-rating-1" title="1 out of 5 stars"></label>
      </div>
    </div>
    

    CSS

    .star-rating{
        font-size: 0;
    }
    .star-rating__wrap{
        display: inline-block;
        font-size: 1rem;
    }
    .star-rating__wrap:after{
        content: "";
        display: table;
        clear: both;
    }
    .star-rating__ico{
        float: right;
        padding-left: 2px;
        cursor: pointer;
        color: #FFB300;
    }
    .star-rating__ico:last-child{
        padding-left: 0;
    }
    .star-rating__input{
        display: none;
    }
    .star-rating__ico:hover:before,
    .star-rating__ico:hover ~ .star-rating__ico:before,
    .star-rating__input:checked ~ .star-rating__ico:before
    {
        content: "\f005";
    }
    
    0 讨论(0)
提交回复
热议问题