I want to dynamically display data on list depending upon the key I select. The list of items can have multiple keys. I want to dynamically choose the data that I want to display onto the list without hard coding the actual key.
<template>
<v-card
max-width="500"
>
<v-list>
<v-list-item-group v-model="model">
<v-list-item
v-for="(item, i) in items"
:key="i"
>
<v-list-item-icon>
<v-icon v-text="item.icon"></v-icon>
</v-list-item-icon>
<v-list-item-content>
<v-list-item-title v-text="item.data_to_display"></v-list-item-title>
</v-list-item-content>
</v-list-item>
</v-list-item-group>
</v-list>
</v-card>
</template>
<script>
export default {
data: () => ({
data_to_display: 'name', // or data_to_display: 'text'
items: [
{
age: 34,
name: 'abc',
marks: null
},
{
age: 12,
name: '',
marks: 60
},
{
age: '20,
name: 'lmn',
marks: 70
},
],
model: 1,
}),
}
</script>
The above list of items has multiple keys. I want to display any one of them name, age or marks depending upon the key I choose from the script
CodePudding user response:
Like @Sami commented you can use key to show data, and in computed property filter only ones with values:
new Vue({
el: '#app',
vuetify: new Vuetify(),
data() {
return {
data_to_display: 'name',
items: [{age: 34, name: 'abc', marks: null}, {age: 12, name: '', marks: 60}, {age: 20, name: 'lmn', marks: 70 },],
model: 1,
}
},
computed: {
filteredItems() {
return this.items.filter(i => i[this.data_to_display] )
}
}
})
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/[email protected]/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet">
<div id="app">
<v-app>
<v-main>
<v-container>
<v-card
max-width="500"
>
<v-list>
<v-list-item-group v-model="model">
<v-list-item
v-for="(item, i) in filteredItems"
:key="i"
>
<v-list-item-icon>
<v-icon v-text="item.icon"></v-icon>
</v-list-item-icon>
<v-list-item-content>
<v-list-item-title v-text="item[data_to_display]"></v-list-item-title>
</v-list-item-content>
</v-list-item>
</v-list-item-group>
</v-list>
</v-card>
</v-container>
</v-main>
</v-app>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.js"></script>
CodePudding user response:
Convert v-text="item.data_to_display"
to v-text="item[data_to_display]"
will resolve the issue.
You need to use brackets
if the property name has special characters. Bracket notation can be quite useful if you want to search for a property's values dynamically.
As in one of the object you have empty value for name
property, It is showing as blank.
Working Demo :
new Vue({
el: "#app",
vuetify: new Vuetify(),
data: {
data_to_display: 'name',
model: 1,
items: [{
age: 34,
name: 'abc',
marks: null
}, {
age: 12,
name: '',
marks: 60
}, {
age: '20',
name: 'lmn',
marks: 70
}]
}
})
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet">
<div id="app">
<v-app>
<v-main>
<v-container>
<v-card
max-width="500"
>
<v-list>
<v-list-item-group v-model="model">
<v-list-item
v-for="(item, i) in items"
:key="i"
>
<v-list-item-icon>
<v-icon v-text="item.icon"></v-icon>
</v-list-item-icon>
<v-list-item-content>
<v-list-item-title v-text="item[data_to_display]"></v-list-item-title>
</v-list-item-content>
</v-list-item>
</v-list-item-group>
</v-list>
</v-card>
</v-container>
</v-main>
</v-app>
</div>