0

I have the following JSON data:

[
  {
    "amount": 75,
    "date": "2020-04-22",
    "entradas": [
      {
        "date": "2020-04-22",
        "amount": "100.00",
        "title": "test 1"
      },
      {
        "date": "2020-04-22",
        "amount": "-25.00",
        "title": "test 2"
      }
    ]
  },
  {
    "amount": 10,
    "date": "2020-04-30",
    "entradas": [
      {
        "date": "2020-04-30",
        "amount": "10.00",
        "title": "test 3"
      }
    ]
  }
]

What I would like to achieve is render all these "entradas" records together, so in this case it should render 3 rows at the Data Table, one for each "entradas".

I have the following basic method to load the json by axios:

    methods: {
        loadData () {
            axios.get('dataapi')
                    .then(response => this.dataItems = response.data)
        },
    }

and

    data () {
        return {
            headers: [
                {
                    text: 'Data',
                    align: 'start',
                    sortable: false,
                    value: 'date',
                },
                { text: 'Title', value: 'title', sortable: false },
                { text: 'Amount', value: 'amount', sortable: false },
            ],
            dataItems: []
        }
    },
<template>
    <v-app id="testdatatable">
        <v-data-table
        :headers="headers"
        :items="dataItems.entradas"
        class="elevation-1"
        ></v-data-table>
    </v-app>
</template>

The problem is that when I use :items="dataItems.entradas" it just doesn't render anything. The same problems happens if I use just :items="dataItems" and at the headers use something like value: 'entradas.title'

I've tried some workaround with .map function, as I've seem some similar examples, but I didn't figured out how could that help in this situation...

2 Answers 2

1

You should only need a simple reduce to flatten the nested item arrays..

  methods: {
        loadData () {
            axios.get('dataapi')
                  .then((response) => {
                      this.dataItems = response.data.reduce(function(acc, v) {
                             return [...acc, ...v.entradas]
                      },[])
                  })
        },
    }

  <v-data-table
    :headers="headers"
    :items="dataItems"
    class="elevation-1"
  ></v-data-table>

Demo (see flatItems() computed)

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

Comments

0

Try this

...
data () {
    return {
        headers: [
            {
                text: 'Data',
                align: 'start',
                sortable: false,
                value: 'date',
            },
            { text: 'Title', value: 'title', sortable: false },
            { text: 'Amount', value: 'amount', sortable: false },
        ],
        dataItems: [],
    }
},
methods: {
    loadData () {
        axios
            .get('dataapi')
            .then(response => {
                this.dataItems = response.data.map(dataItem => {
                    const { amount, date, entradas } = dataItem

                    entradas.map(entrada => {
                        return {
                            amount, 
                            date,
                            ...entrada
                        }
                    })
                })
            })
    },
}
...
<template>
    <v-app id="testdatatable">
        <v-data-table
            :headers="headers"
            :items="dataItems"
            class="elevation-1"
        ></v-data-table>
    </v-app>
</template>

1 Comment

dataItems resulted 2 unknown records

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.