Simsons
Simsons

Reputation: 12735

Binding array items in Vue.js

I have an arry which contains some array and proporties in it.

Now I want to iterate through the array and create new rows for each of items.

This is what I am doing.

var orderDetails = [{
        "ID": "1",
        "NAME": "Item1",
        "Orders": [
            "ORD001"
        ],
        "Purchases": [
            "RhynoMock",
            "Ember"
        ],
        "ISENABLED": "false"
    },
    {
        "ID": "2",
        "NAME": "Item2",
        "Orders": [
            "ORD002",
            "ORD008"
        ],
        "Purchases": [
            "StufferShop"
        ],
        "ISENABLED": "false"
    }
];

var app = new Vue({
    el: '#ordersDiv',
    data: {
        allOrders: orderDetails
    }
});
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<div id="ordersDiv">
    <table>
        <thead>
            <tr>
                <th>Name</th>
            </tr>
        </thead>
        <tbody>
            <tr v-for="order in allOrders">{{ order.NAME }}</tr>
    </table>

</div>

Getting the error,

[Vue warn]: Error in render: "TypeError: Cannot read property 'NAME' of undefined"

Upvotes: 1

Views: 73

Answers (1)

Decade Moon
Decade Moon

Reputation: 34286

Your HTML is invalid, and it seems that Vue's template compiler or DOM renderer has trouble with it: <tr> must only have <td> or <th> child elements, it cannot have text node children.

Try this instead:

<tr v-for="order in allOrders">
  <td>{{ order.NAME }}</td>
</tr>

Upvotes: 2

Related Questions