Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render 3 column table using an array #575

Closed
shebinleo opened this issue Apr 6, 2012 · 2 comments
Closed

Render 3 column table using an array #575

shebinleo opened this issue Apr 6, 2012 · 2 comments

Comments

@shebinleo
Copy link

I am having trouble in rendering a 3 column table using an array

var list = [1, 2, 3, 4, 5,  6]

code I am currently using is

table
   - each value, index in list
     - if (index%3==0) {
       tr
     - }
       td=value

what I am getting using code above is

<table>
  <tbody>
    <tr></tr>
    <tr>
       <td>1</td>
       <td>2</td>
       <td>3</td>
    </tr>
    <tr></tr>
    <tr>
      <td>4</td>
      <td>5</td>
      <td>6</td>
    </tr>
  </tbody>
</table>

what I want is the table below without the extra tr

<table>
  <tbody>
    <tr>
       <td>1</td>
       <td>2</td>
       <td>3</td>
    </tr>
    <tr>
      <td>4</td>
      <td>5</td>
      <td>6</td>
    </tr>
  </tbody>
</table>
@mjadobson
Copy link

It's hard to tell from the code you've given here, but I think it may be more appropriate to present your array as floated list items, rather than a table.

Regardless, this is how I would tackle this with tables:

var list = [1,2,3,4,5,6,7];
var listChunks = [];

for (var i = 0; i < Math.ceil(list.length / 3); i++) {
  listChunks.push(list.slice(3 * i, 3 * (i + 1)));
}
table
  tbody
    each chunk in listChunks
      tr
        each val in chunk
          td= val

@shebinleo
Copy link
Author

Thank you, floated list items is the right solution for my problem.

Thank you for the table code too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants