0

I have this JSON-array which looks like this:

"data": {
   "cells": [
    [
     {
      "rowIndex": 0,
      "columnIndex": 0,
      "value": "<p>This is some value</p>",
      "type": "th",
      "scope": "col"
     },
     {
      "rowIndex": 0,
      "columnIndex": 1,
      "value": "<p>Another value</p>",
      "type": "th",
      "scope": "col"
     }
    ],
    [
     {
      "rowIndex": 1,
      "columnIndex": 0,
      "value": "<p>Blabla blabla</p>",
      "type": "td",
      "scope": null
     },
     {
      "rowIndex": 1,
      "columnIndex": 1,
      "value": "<p>Lorem ipsum</p>",
      "type": "td",
      "scope": null
     }
    ]
   ],
 }

Now I want to render this data in a table in my vuejs-app, so I tried to do this:

First get the data through computed value:

computed: {
    tableData() {
        return this.content.data.cells;
    },
},

Then render the data through a table:

<table>
    <tr v-for="cell in tableData" :key="cell.id">
        <template v-if="cell.type === 'th'">
            <th v-for="header in cell" :key="header">
                {{ header.value }}
            </th>
        </template>
        <template v-if="cell.type === 'td'">
            <td v-for="celldata in cell" :key="celldata">
                {{ celldata.value }}
            </td>
        </template>
    </tr>
</table>

But this shows nothing, the fields are just empty. What am I doing wrong?

2 Answers 2

1
  1. Given that the cells array contains one array for each row, you must test row type with row[0].type
  2. Cell content is html, so using interpolation {{ }} is not enough - you need to use v-html
  3. You are using :key="cell.id" but I do not see any id field in the data itself...
  4. You can simplify the template and eliminate duplicity by using special Vue is attribute

const data = {
  "cells": [
    [{
        "rowIndex": 0,
        "columnIndex": 0,
        "value": "<p>This is some value</p>",
        "type": "th",
        "scope": "col"
      },
      {
        "rowIndex": 0,
        "columnIndex": 1,
        "value": "<p>Another value</p>",
        "type": "th",
        "scope": "col"
      }
    ],
    [{
        "rowIndex": 1,
        "columnIndex": 0,
        "value": "<p>Blabla blabla</p>",
        "type": "td",
        "scope": null
      },
      {
        "rowIndex": 1,
        "columnIndex": 1,
        "value": "<p>Lorem ipsum</p>",
        "type": "td",
        "scope": null
      }
    ]
  ],
}

const vm = new Vue({
  el: "#app",
  data() {
    return {
      data
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css">

<div id="app">
  <table class="table is-bordered">
    <tr v-for="(row, idx) in data.cells" :key="idx">
      <th v-for="cell in row" :is="cell.type" :key="`r${idx}_c${cell.columnIndex}`" v-html="cell.value">
      </th>
    </tr>
  </table>
</div>

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

Comments

0

You're working with JSON data. You really need to parse the json back into javascript datatype first with the JSON.parse() method.

You can read up on this on mdn

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.