获取Vuetify列表的选择

huangapple go评论59阅读模式
英文:

Get selection of vuetify list

问题

如何从Vuetify列表中获取所选项目/索引?

v-model应用于<v-list>标记似乎无效,我找不到任何有效的示例。

我想要一个图像/文件名列表,然后显示所选的图像。我的想法是使用<v-img :src="imgpath">,然后通过列表更改imgpath,使其成为一个响应式状态。或者我的想法完全错误吗?

我的尝试的最小示例:

<template>
  <v-app>
    <v-main>
      <v-card>
      	<v-list v-model="selection" :items="items"></v-list> 
    	</v-card>
      <v-card class="mt-5">
      	{{ selection }}
      </v-card>
    </v-main>
  </v-app>
</template>

<script>
export default {
    data: () => ({
      selection: 1,
      items: [
        { title: 'Item 0', value: 0 },
        { title: 'Item 1', value: 1 },
        { title: 'Item 2', value: 2 },
      ],
    }),
  }
</script>

预期行为:
selection状态根据列表中的所选项更改。

观察到的行为:
在视觉上,选择发生变化(不同项以灰色背景标记),但selection不会更改。

英文:

How do I get the selected item / index of from a vuetify list?

Applying v-model to the &lt;v-list&gt; tag does not work somehow and I can not find any working example.

I'd like to have a list of images / file names and then display the selected image. My idea was to have an &lt;v-img :src=&quot;imgpath&quot;&gt; and then imgpath beeing a reactive state that is changed via the list. Or is my idea completely wrong?

Minimum example of my try:

&lt;template&gt;
  &lt;v-app&gt;
    &lt;v-main&gt;
      &lt;v-card&gt;
      	&lt;v-list v-model=&#39;selection&#39; :items=&#39;items&#39;&gt;&lt;/v-list&gt; 
    	&lt;/v-card&gt;
      &lt;v-card class=&#39;mt-5&#39;&gt;
      	{{ selection }}
      &lt;/v-card&gt;
    &lt;/v-main&gt;
  &lt;/v-app&gt;
&lt;/template&gt;

&lt;script&gt;
export default {
    data: () =&gt; ({
      selection: 1,
      items: [
        { title: &#39;Item 0&#39;, value: 0 },
        { title: &#39;Item 1&#39;, value: 1 },
        { title: &#39;Item 2&#39;, value: 2 },
      ],
    }),
  }
&lt;/script&gt;

Expected behaviour:
The selection state changes according to the selected item in the list.

Observed behaviour:
Visually the selection changes (a different item is marked with a gray background), but the selection does not change.

答案1

得分: 2

这是翻译好的部分:

"这个要做你想要的事情非常不明显,在 Vuetify 文档中没有针对你认为是相当常见的用例的示例。

诀窍是忽略 v-model,而是使用 @click 事件和 v-list-item:active 属性。

模板:

<template>
   <v-list>
      <v-list-item v-for="item in items" 
                   :key="item.id" 
                   :active="item.id === selected"
                   @click="selected = item.id">

脚本:

<script setup lang="ts">
   import { ref } from "vue";

   const selected = ref("");

现在你可以使用多选等所有明显的操作,以及任何其他你可能想要的事情。"

英文:

It's super un-obvious how to do what you want, and there are no examples in the Vuetify documentation for what you'd think was a pretty common use-case.

The trick is to ignore v-model and instead use an @click event and the :active attribute of v-list-item.

Template:

&lt;template&gt;
   &lt;v-list&gt;
      &lt;v-list-item v-for=&quot;item in items&quot; 
                   :key=&quot;item.id&quot; 
                   :active=&quot;item.id === selected&quot;
                   @click=&quot;selected = item.id&quot;&gt;

Script:

&lt;script setup lang=&quot;ts&quot;&gt;
   import { ref } from &quot;vue&quot;;

   const selected = ref(&quot;&quot;);

You can now do all the obvious stuff with multiple select etc and anything else you might want.

答案2

得分: 1

您可以在循环中添加 `v-list-item`。在 `&lt;v-list-item&gt;` 中添加 `@click` 处理程序来更新 `selection` 数据。尝试以下内容:

    &lt;template&gt;
      &lt;v-app&gt;
        &lt;v-main&gt;
          &lt;v-card&gt;
            &lt;v-list&gt;
              &lt;v-list-item
                v-for=&quot;(item, index) in items&quot;
                :key=&quot;index&quot;
                @click=&quot;selection = index&quot;
              &gt;
                &lt;v-list-item-title&gt;{{ item.title }}&lt;/v-list-item-title&gt;
              &lt;/v-list-item&gt;
            &lt;/v-list&gt;
          &lt;/v-card&gt;
          &lt;v-card class=&quot;mt-5&quot;&gt;
            {{ selection }}
          &lt;/v-card&gt;
        &lt;/v-main&gt;
      &lt;/v-app&gt;
    &lt;/template&gt;
英文:

You can add v-list-item in a loop. On &lt;v-list-item&gt; add @click handler to update selection data. Try this:

&lt;template&gt;
  &lt;v-app&gt;
    &lt;v-main&gt;
      &lt;v-card&gt;
        &lt;v-list&gt;
          &lt;v-list-item
            v-for=&quot;(item, index) in items&quot;
            :key=&quot;index&quot;
            @click=&quot;selection = index&quot;
          &gt;
            &lt;v-list-item-title&gt;{{ item.title }}&lt;/v-list-item-title&gt;
          &lt;/v-list-item&gt;
        &lt;/v-list&gt;
      &lt;/v-card&gt;
      &lt;v-card class=&quot;mt-5&quot;&gt;
        {{ selection }}
      &lt;/v-card&gt;
    &lt;/v-main&gt;
  &lt;/v-app&gt;
&lt;/template&gt;

&lt;script&gt;
export default {
  data: () =&gt; ({
    selection: 0,
    items: [
      { title: &quot;Item 0&quot;, value: 0 },
      { title: &quot;Item 1&quot;, value: 1 },
      { title: &quot;Item 2&quot;, value: 2 },
    ],
  }),
};
&lt;/script&gt;

答案3

得分: 1

你想要使用 v-model:selected。我自己花了一些时间才弄明白,但这对我来说很有效。

<template>
  <v-app>
    <v-main>
      <v-card>
        <v-list v-model:selected='selection' :items='items'></v-list>
      </v-card>
      <v-card class='mt-5'>
        {{ selection }}
      </v-card>
    </v-main>
  </v-app>
</template>

<script>
export default {
    data: () => ({
      selection: [1], // this is an array of selected items
      items: [
        { title: 'Item 0', value: 0 },
        { title: 'Item 1', value: 1 },
        { title: 'Item 2', value: 2 },
      ],
    }),
  }
</script>
英文:

You want to use v-model:selected. Took me a while to figure this one out myself, but this works for me

&lt;template&gt;
  &lt;v-app&gt;
    &lt;v-main&gt;
      &lt;v-card&gt;
        &lt;v-list v-model:selected=&#39;selection&#39; :items=&#39;items&#39;&gt;&lt;/v-list&gt; 
        &lt;/v-card&gt;
      &lt;v-card class=&#39;mt-5&#39;&gt;
        {{ selection }} 
      &lt;/v-card&gt;
    &lt;/v-main&gt;
  &lt;/v-app&gt;
&lt;/template&gt;

&lt;script&gt;
export default {
    data: () =&gt; ({
      selection: [1], // this is an array of selected items
      items: [
        { title: &#39;Item 0&#39;, value: 0 },
        { title: &#39;Item 1&#39;, value: 1 },
        { title: &#39;Item 2&#39;, value: 2 },
      ],
    }),
  }
&lt;/script&gt;

huangapple
  • 本文由 发表于 2023年2月18日 21:23:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493631.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定