2

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
    class="mx-auto"
    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

5
  • 1
    You have the item, just take whatever you want from it. item[key] Commented Mar 13, 2022 at 6:46
  • Do you mean item[data_to_display] ? Commented Mar 13, 2022 at 6:50
  • Thanks it worked... But it is also displaying some blank values i.e. spaces if one of the objects has an empty values. Commented Mar 13, 2022 at 6:56
  • For that you’ll need to make a computed list which filters values out Commented Mar 13, 2022 at 6:57
  • Can you please elaborate what exactly is computed list? Commented Mar 13, 2022 at 6:58

2 Answers 2

2

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
          class="mx-auto"
          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>

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

4 Comments

Do you think this is a feasible solution as there are 1000s of objects within items array? I tried adding a v-if statement inside <v-list-item-content v-if="item !== null"> but this also didn't work
@Mrunall Veer hey mate, why not you only filter for items with values, as you can se it works in example?
Because I will have to iterate the loop 1000 times each time I switch the key. Can I add a v-if statement instead?
I think when you change the key you are already iterating in v-for in template, so it's the same because you are now connected at filteredItems in template
1

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
                class="mx-auto"
                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>

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.