views:

122

answers:

4

Hi,

how do I create a list of li's (they look like simple boxes by using css and float left) with the certain specifications:

  • 7 li's in a row
  • the row is a closed ul-tag
  • at the end there should be 8 rows of ul's
  • the last li (the 7th) should have the class="last".

this is what i got so far, to create li's through an array (i need an array, because i need the values):

<script type="text/javascript">
    $(function () {
     $.each([39, 6, 1, 3, 5, 1, 1, 1, 1, 1, 1], function(index, value) { 
       $("#heatmap ul").append("<li class='box' value="+value+">"+index+"</li>"); 
     });
    });
</script>

I appreciate your help and your time, Nicole

A: 
$("#heatmap ul li:last").addClass('last');

should do the trick (as jQuery can parse CSS3+ selectors)

Kolky
A: 

I'd cache your selector and use the :nth-child(7n) selector (every 7th child) afterwards, like this:

$(function () {
  var ul = $("#heatmap ul");
  $.each([39, 6, 1, 3, 5, 1, 1, 1, 1, 1, 1], function(index, value) { 
    ul.append("<li class='box' value="+value+">"+index+"</li>"); 
  });
  ul.children(":nth-child(7n)").addClass("last");
});

This prevents it finding that <ul> for every loop in the function.

If you float your <li> elements, and give them a width, and the <ul> 7x that width, they will float in rows of 7, like this:

li { float: left; width: 100px; }
ul { width: 700px }

Best to show a demonstration of the floating approach: here's a quick demo of what I mean, this uses 1 <ul> instead of n <ul> elements, much simpler :)

Nick Craver
works perfectly too! thank you
thank you for the demo too, very cool :)
is there a way that i can create those li's AND "empty li's", which would make up a total number of 56? i could add like 46 "0"'s to the array but thats kinda annoying :)
@user309980 - At the end, or in amongst the array?
Nick Craver
If you mean at the end of the array, try this: http://jsfiddle.net/9w2zg/3/ I just check 56 - count created, and loop that many times creating `<li>` elements before doing the `last` class, just move the class line if you don't want those blanks as part of the `class="last"` group.
Nick Craver
wow thank you, well you asked...would it be possible amongst the array too :)
@user309980 - Not really sure how you'd pull that off, since you wouldn't know at *which* places to stick the 0's in, best to put them in the array in that case I think.
Nick Craver
ok that works too, thank you very much
@user309980 - Welcome :) Be sure to check an answer as accepted with the checkmark on the left when a question is resolved...helps the next person that googles and finds this get the answer quicker :)
Nick Craver
A: 
$("#heatmap li").last().addClass('last');

Kind Regards

--Andy

jAndy
+1  A: 

Something like this I guess?

var values = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ];

var $heatmap = $('#heatmap');

var currentRow = "";
for(var i = 0; i < values.length; i++) {
    currentRow += "<li>" + values[i] + "</li>";

    if(i % 7 == 0) {
        $heatmap.append("<ul>" + currentRow + "</ul>");
        currentRow = "";
    }
}

if(currentRow !== "") {
    $heatmap.append("<ul>" + currentRow + "</ul>");
}

$heatmap.find("ul li:last").addClass("last");

Outputs an <ul> per every 7 items in the array, and adds the last class to the last element in the generated <li/>'s

Jan Jongboom
this seems to be the perfect answer but somehow doesnt work, the ul stays empty.
nevermind im stupid thank you it works perfectly
Then please accept the answer by clicking the 'mark' icon next to this answer.
Jan Jongboom