The problem is that the fields first_name
and last_name
do not exist in the JSON
returned by randomuser
. Instead we can find name.first
and name.last
.
To be able to access nested values we can use Scoped field slots
where we declare the slot
equal to the name of field.key
and within this add the expression data.item.*
to print the desired value.
Solution:
You could do it like this:
<template>
<b-table :fields="fields" :items="personas">
<template slot="first_name" slot-scope="data">
{{data.item.name.first}}
</template>
<template slot="last_name" slot-scope="data">
{{data.item.name.last}}
</template>
</b-table>
</template>
<script>
export default {
name: 'Agenda',
data () {
return {
fields: [
{ key: 'first_name', label: 'Last Name' },
{ key: 'last_name', label: 'First Name' }
],
personas: []
}
},
created: function () {
this.obtenerAPI()
},
methods: {
obtenerAPI () {
this.$http.get('https://randomuser.me/api/?results=10')
.then(function (respuesta) {
this.personas = respuesta.body.results
})
}
}
}
</script>
Demo:
var app = new Vue({
el: '#app',
data () {
return {
fields: [
{ key: 'first_name', label: 'Last Name' },
{ key: 'last_name', label: 'First Name' }
],
personas: []
}
},
created: function () {
this.obtenerAPI()
},
methods: {
obtenerAPI () {
this.$http.get('https://randomuser.me/api/?results=10')
.then(function (respuesta) {
this.personas = respuesta.body.results
})
}
}
})
<link rel="stylesheet prefetch" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css">
<link rel="stylesheet prefetch" href="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.css">
<script src="//cdn.jsdelivr.net/npm/[email protected]/dist/vue.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="//unpkg.com/babel-polyfill@latest/dist/polyfill.min.js"></script>
<script src="//unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.js"></script>
<div id="app">
<template>
<b-table :fields="fields" :items="personas">
<template slot="first_name" slot-scope="data">
{{data.item.name.first}}
</template>
<template slot="last_name" slot-scope="data">
{{data.item.name.last}}
</template>
</b-table>
</template>
</div>