Monday, 15 September 2014

node.js - How to insert new tr every third iteration in Jade -



node.js - How to insert new tr every third iteration in Jade -

i'm new in node.js , jade.

i searched solutions without success (maybe asked wrong questions in google, don't know).

i want create table rows in each loop in jade. thing after every 3rd td want insert new tr. it's quite simple jade can't accomplish that.

my jade file: table thead tr td header tbody each item, in items if (i % 3 === 0) tr td a(href="#{baseurl}/admin.html?id=#{item.id}")

i know wrong if statement. tried many configurations without luck. i'm sure quite easy issue.

thanks in advance help!

edit

based on @laurent perrin reply modified little code. creates tr, 3 td , new tr it's little closer...

new jade if (i % 3 === 0) tr td: a(href="#{baseurl}/admin.html?id=#{item.id}") dsdsd #{i} generated html <tr></tr> <td><a href="...">0</a></td> <td><a href="...">1</a></td> <td><a href="...">2</a></td> <tr></tr>

edit: code should want, it's not elegant:

table thead tr: td header tbody - for(var = 0, nbrows = items.length/3; < nbrows; i++) { tr if items[3*i] td: a(href="#{baseurl}/admin.html?id=#{items[3*i].id}") if items[3*i + 1] td: a(href="#{baseurl}/admin.html?id=#{items[3*i + 1].id}") if items[3*i + 2] td: a(href="#{baseurl}/admin.html?id=#{items[3*i + 2].id}") - }

what instead tweak model create more jade-friendly, grouping items rows:

function getrows(items) { homecoming items.reduce(function (prev, item, i) { if(i % 3 === 0) prev.push([item]); else prev[prev.length - 1].push(item); homecoming prev; }, []); }

this turn:

[{id:1},{id:2},{id:3},{id:4},{id:5}]

into:

[ [{id:1},{id:2},{id:3}], [{id:4},{id:5}] ]

then jade code becomes much simpler:

table thead tr: td header tbody each row in rows tr each item in row td: a(href="#{baseurl}/admin.html?id=#{item.id}")

node.js jade

No comments:

Post a Comment