0

Just a small problem i can't get my head around right now:

I have a list of objects presented in a table. One of the objects values is a score. I can use a Django template tag to show this as a number but i want to use my jquery plug-in to show stars instead. Not sure how to iterate over this. I'm trying this:

{% for result in mylist %}

<td>{{ result.type }}</td>
<td>{{ result.description }}</td>
<td>{{ result.rating.votes }}</td>

<td><div class="raty" data-number="{{ result.rating.score }}"></div></td>

{% endfor %}

And further down i got this:

<script>
$('.raty').raty({ readOnly: true, score: $('.raty').attr('value') });
</script>

The problem is that it shows the same score for every object with the jquery..

EDIT: I got it to work with this:

<script>
$('.raty').each(function() {
  $(this).raty({ readOnly: true, score: $(this).attr('data-number') });
});
</script>
user3199840
  • 525
  • 2
  • 13
  • 35

1 Answers1

1

A div element does not have a value attribute. Try using a hidden input element. Something like this:

<table>
    {% for result in mylist %}
        <tr><td><input type='hidden' class='hidden_score' value='{{ result.rating.score }}'></input><div class="raty"></div></td></tr>
     {% endfor %}
</table>

And then in your script:

$.each($('.hidden_score'), function( index, value ) {
    var myval = $(this).val();
    $(this).parent().find( '.raty').raty({ readOnly:true, score:myval});
});

So, for each of elements having a class of hidden_score, you will get their value make a "raty" with the elemens belonging to the parent of each element (so they are siblings) with the correct score.

Serafeim
  • 14,962
  • 14
  • 91
  • 133
  • Thank you for your answer. Nah this was not exactly what i was thinking of. Now i got an inputfield for each score and only the first one shows some stars (not right score but just "empty stars"). But i'll try to modify it and see what i get. – user3199840 Feb 13 '14 at 20:48
  • No wrong. The first field does show stars+ input field and the rest objects show inputfields with numbers in them.. – user3199840 Feb 13 '14 at 21:11
  • oops I forgot to add type='hidden' to the input ! – Serafeim Feb 14 '14 at 06:43
  • Yes that would make more sense :) But the problem is the iteration it's only showing the first objects value like stars. – user3199840 Feb 14 '14 at 12:37
  • Oops ! I haven't seen the {% for %}... Please check my updated answer ! – Serafeim Feb 14 '14 at 15:12
  • Hehe ok. No it's not working but i think the $.each is something i'll look into. It feels like it's something with that.. – user3199840 Feb 14 '14 at 16:18
  • Why not working? Try doing an alert(myval) to be sure that the correct value is shown each time. Also be sure that the correct ``.raty`` div can be found from ``find``... – Serafeim Feb 14 '14 at 16:25
  • Not sure why it didn't work but i managed do make a solution without the hidden field. Thanks for getting me in the right direction! – user3199840 Feb 15 '14 at 00:35