0

in vue js how to load nested array to a html table. when I use v-for inside v-for it qives an error Property or method "key" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option.

[
   {
      jobtype_id:"1",
      jobtype_code:"L001",
      jobtype_name:"Labour",
      jobtype_order:"1",
      jobtype_comment:"1",
      jobs:[
         {
            jobinvoicedtlid:"1",
            JobInvNo:"JBIN0016",
            JobCardNo:"",
            JobType:"1",
            JobCode:null,
            JobDescription:"Wheel alignment",
            JobQty:"2",
            JobPrice:"800.00",
            JobTotalAmount:"1600.00",
            JobDiscount:"0.00",
            JobNetAmount:"1600.00",
            JobDiscountType:"1",
            JobinvoiceTimestamp:"2147483647",
            Description:"Labour"
         },
         {
            jobinvoicedtlid:"2",
            JobInvNo:"JBIN0016",
            JobCardNo:"",
            JobType:"1",
            JobCode:null,
            JobDescription:"Full Service",
            JobQty:"4",
            JobPrice:"250.00",
            JobTotalAmount:"1000.00",
            JobDiscount:"0.00",
            JobNetAmount:"1000.00",
            JobDiscountType:"1",
            JobinvoiceTimestamp:"2147483647",
            Description:"Labour"
         }
      ]
   },
   {
      jobtype_id:"2",
      jobtype_code:"S002",
      jobtype_name:"Parts Outside",
      jobtype_order:"3",
      jobtype_comment:null,
      jobs:[
         {
            jobinvoicedtlid:"3",
            JobInvNo:"JBIN0016",
            JobCardNo:"",
            JobType:"2",
            JobCode:null,
            JobDescription:"Oil Change",
            JobQty:"5",
            JobPrice:"500.00",
            JobTotalAmount:"2500.00",
            JobDiscount:"0.00",
            JobNetAmount:"2500.00",
            JobDiscountType:"1",
            JobinvoiceTimestamp:"2147483647",
            Description:"Parts Outside"
         }
      ]
   }
]
<tbody>
    <tr v-for="item,key in printdata">
        <td colspan='6'> <b>{{item.jobtype_name}}</b></td>
             <table border="1">
                 <tr v-for="itm in printdata.jobs">
                     <td>itm.JobDescription</td>
                     <td></td>
                     <td></td>
                     <td></td>
                     <td></td>
                     <td></td>
                  </tr>
            </table>
        </tr>     
  </tbody>

this kind of result im expecting enter image description here

3
  • where is the JSON placed? share Vue app instance code Commented Jul 25, 2017 at 7:52
  • you have to defined the JSON object in vue instance 'data' Commented Jul 25, 2017 at 7:56
  • it is load from ajax and assign to data property as well Commented Jul 25, 2017 at 7:58

4 Answers 4

1

Key is not defined means you are using key in v-for which is not exist in your JSON data. you need to bind key in a different way

    <tr v-for="(item, index) in printdata" v-bind:key="index">
        

To know more about Vue.js key and v-for index

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

Comments

0

You can nest v-for without trouble. You just have a few things mixed up. This works for me with your data:

<template>
  <div class="hello">
  <tbody>
    <tr v-for="item in printdata">
        <td colspan='6'> <b>{{item.jobtype_name}}</b></td>
            <table border="1">
                <tr v-for="job in item.jobs">
                    <td>{{job}}</td>
                  </tr>
            </table>
        </tr>     
  </tbody>

  </div>
</template>

In the outside v-for 'item' is one item from your array. The inside v-for loops over the item.jobs from the first loop.

I'm not sure what you are trying to get with the layout, but you might also try putting the inner v-for on the <td> rather then the <tr> element like:

<tr>
    <td v-for="job in item.jobs">{{job.Description}}</td>
</tr>

4 Comments

I have tried this Mark_M. but it will gives an error Property or method "item" is not defined on the instance but referenced during render. Make sure to declare reactive data properties in the data option.
It's running with no errors here @KushanShanuka. You said you are getting the data from ajax? Maybe you need to use a v-if to prevent the table from trying to render before the ajax arrives.
I'll try @Mark_M.. acctually im looking for this kind of result i.sstatic.net/hNlry.png.
can you here the code example please.. thank you @Mark_M
0

Try something like this

<table>
  <tr>
    <td v-for='item in items'>
      // Use 'item' from now now
      <table>
        <tr v-for='depth in item'>
          <td>{{ depth.description }}</td>
        </tr>
      </table>
    </td>
  </tr>
</table>

To use a v-for inside a v-for, you got to use the result of the iteration.

Comments

0

It seems you have syntax issue (missing parenthesis) . Try this:

<tbody>
    <tr v-for="(item,key) in printdata">
        <td colspan='6'> <b>{{item.jobtype_name}}</b></td>
             <table border="1">
                 <tr v-for="itm in printdata.jobs">
                     <td>itm.JobDescription</td>
                     <td></td>
                     <td></td>
                     <td></td>
                     <td></td>
                     <td></td>
                  </tr>
            </table>
        </tr>     
  </tbody>

You can read more about the efficient approach of Vue to handle list rendering using the key attribute here

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.