Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an html table with Jade iterating an array

I'm starting with node expressjs framework and I came across this problem I can't solve.

I'm trying to display a table with some blog posts (yes, a blog...) but I don't get it done.

This is the Jade template code:

div
  table
    thead
      tr: th Posts
    tbody
      each post, i in userPosts
        tr(class=(i % 2 == 0) ? 'odd' : 'even'): a(href='/admin/post/' + post.id) #{post.author} - #{post.title}

And this is the HTML output:

<div>
  <a href="/admin/post/1">Post 1</a>
  <a href="/admin/post/2">Post 2</a>
  <a href="/admin/post/3">Post 3</a>
  <table>
    <thead>
      <tr>
        <th>Posts</th>
      </tr>
    </thead>
    <tbody>
      <tr class="odd"></tr>
      <tr class="even"></tr>
      <tr class="odd"></tr>
    </tbody>
  </table>
</div>

So, any ideas?

like image 482
PaquitoSoft Avatar asked Oct 27 '11 19:10

PaquitoSoft


3 Answers

I found that the problem was that I was missing the TD tag for each TR. So the jade code should be like this:

div
  table
    thead
      tr: th Posts
    tbody
      each post, i in userPosts
        tr
          td 
            a(href='/admin/post/' + post.id) #{post.author} - #{post.title}
like image 50
PaquitoSoft Avatar answered Oct 01 '22 23:10

PaquitoSoft


try this

div
  table
    thead
      tr: th Posts
    tbody
      each post, i in userPosts
        tr(class=(i % 2 == 0) ? 'odd' : 'even') 
          td
            a(href='/admin/post/' + post.id) #{post.author} - #{post.title}
like image 27
Chance Avatar answered Oct 01 '22 23:10

Chance


With current pug version didn't work for me. Instead I modified the code to the following pattern:

div
  table
    thead
      tr
        th title...
    tbody
      each post in userPosts
        tr
          td= post.author
          td= post.title
like image 1
Nikolas H Avatar answered Oct 01 '22 22:10

Nikolas H