0

How can I make a simpel HTML-table with this JSON:

[
   [
      "",
      "Current",
      "New",
      "AB",
      "DEF",
      "GHI",
      "JL"
   ],
   [
      "Foobar",
      "Aa ",
      "234",
      "44",
      "aax",
      "44",
      "xx"
   ],
   [
      "Row 2",
      "ii",
      "",
      "iik",
      "asd",
      "cc",
      "f"
   ]
]

Each part from the Array should be a column.

Tried this, but not working:

<tbody v-bind:key="index">
                <tr
        v-for="(col, index) in foobar"
        v-bind:key="index"
    >
        <td
            v-for="(row, subindex) in foobar[i]"
            v-bind:key="subindex"
        >
            {{ foobar[index][subindex] }}!
        </td>
    </tr>
</tbody>

What am i doing wrong

1 Answer 1

1

You need to iterate over foobar[index] in the second loop:

new Vue({
  el:"#app",
  data() {
    return {
      foobar: [
        ["", "Current", "New", "AB", "DEF", "GHI", "JL"],
        ["Foobar", "Aa ", "234", "44", "aax", "44", "xx"],
        ["Row 2", "ii", "", "iik", "asd", "cc", "f"],
      ],
    };
  },
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <table>
    <tbody>
      <tr v-for="(col, index) in foobar" :key="index">
        <td v-for="(row, subindex) in foobar[index]" :key="subindex">
          {{ foobar[index][subindex] }}!
        </td>
      </tr>
    </tbody>
  </table>
</div>

A better version:

new Vue({
  el:"#app",
  data() {
    return {
      foobar: [
        ["", "Current", "New", "AB", "DEF", "GHI", "JL"],
        ["Foobar", "Aa ", "234", "44", "aax", "44", "xx"],
        ["Row 2", "ii", "", "iik", "asd", "cc", "f"],
      ],
    };
  },
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <table>
    <tbody>
      <tr v-for="(row, index) in foobar" :key="index">
        <td v-for="(col, subindex) in row" :key="subindex">
          {{ col }}!
        </td>
      </tr>
    </tbody>
  </table>
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.