Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

257
Vistas
How to dynamically change data shown on the v-list in vuejs?

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

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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/font@6.x/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/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/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>

about 4 years ago · Juan Pablo Isaza Denunciar

0

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/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/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>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda