search

Home  >  Q&A  >  body text

VSelect - Select does not work with custom 'project' slot in Vuetify 3

<p>I'm using VSelect from Vuetify 3 in my Vue 3 application and I'm trying to use item slots. But my VSelect options become unselectable</p> <p>I have this VSelect element: </p> <pre class="brush:php;toolbar:false;"><v-select v-model="tag" :items="tags" variant="solo" label="Tags" > <template #item="{ item }" > <v-list> <v-list-item :title="item.title" /> </v-list> </template> </v-select></pre> <p>Tags and tags in data:</p> <pre class="brush:php;toolbar:false;">tag: null, tags: [ { title: 'example1', value: 0 }, { title: 'example2', value: 1 }, ],</pre> <p>In the output, I selected the option with the option, but the option is not selectable: </p> <p>So how to set slot "item" using selectable options for VSelect component in Vuetify 3? </p>
P粉166779363P粉166779363448 days ago532

reply all(1)I'll reply

  • P粉786800174

    P粉7868001742023-08-27 00:44:36

    The props object passed to the slot contains an onClick callback, which you need to bind for selection to work:

    <template #item="{ item, props: { onClick } }" >
      <v-list-item :title="item.title" @click="onClick"/>
    </template>
    

    Documentation is a bit sparse at the moment, the type given is Record. In Vuetify 3.4, other values ​​are key, title, value and ref (template reference from underlying VVritualScroll) Used to update the height of the scroll bar.

    When using a component with a title property (e.g. VListItem), you can bind the entire props object:

    <template #item="{ props }" >
      <v-list-item v-bind="props"/>
    </template>
    

    (BTW, the #item slot has its content rendered into the v-list, no need to wrap it again)

    This is a snippet:

    const { createApp, ref } = Vue;
    const { createVuetify } = Vuetify
    const vuetify = createVuetify()
    createApp({
      setup(){
        
        return {
           tag: ref(null),
            tags: [
              { title: 'example1', value: 0 },
              { title: 'example2', value: 1 },
            ],
        }
      }
    }).use(vuetify).mount('#app')
    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/vuetify@3.1.6/dist/vuetify.min.css" />
    <link href="https://cdn.jsdelivr.net/npm/@mdi/font@5.x/css/materialdesignicons.min.css" rel="stylesheet">
    <div id="app" class="d-flex justify-center">
      <v-app>
        <div>
        <v-select
          style="width: 300px;"
          v-model="tag"
          :items="tags"
          variant="solo"
          label="Tags"
        >
          <template #item="{ item, props: {onClick, title, value} }" >
            <v-list-item :title="item.title"  @click="onClick"/>
          </template>
        </v-select>
          
          Selected: {{tag}}
        </div>
      </v-app>
    </div>
    <script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vuetify@3.1.6/dist/vuetify.min.js"></script>

    reply
    0
  • Cancelreply