英文:
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 <v-list>
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 <v-img :src="imgpath">
and then imgpath
beeing a reactive state that is changed via the list. Or is my idea completely wrong?
Minimum example of my try:
<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>
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:
<template>
<v-list>
<v-list-item v-for="item in items"
:key="item.id"
:active="item.id === selected"
@click="selected = item.id">
Script:
<script setup lang="ts">
import { ref } from "vue";
const selected = ref("");
You can now do all the obvious stuff with multiple select etc and anything else you might want.
答案2
得分: 1
您可以在循环中添加 `v-list-item`。在 `<v-list-item>` 中添加 `@click` 处理程序来更新 `selection` 数据。尝试以下内容:
<template>
<v-app>
<v-main>
<v-card>
<v-list>
<v-list-item
v-for="(item, index) in items"
:key="index"
@click="selection = index"
>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item>
</v-list>
</v-card>
<v-card class="mt-5">
{{ selection }}
</v-card>
</v-main>
</v-app>
</template>
英文:
You can add v-list-item
in a loop. On <v-list-item>
add @click
handler to update selection
data. Try this:
<template>
<v-app>
<v-main>
<v-card>
<v-list>
<v-list-item
v-for="(item, index) in items"
:key="index"
@click="selection = index"
>
<v-list-item-title>{{ item.title }}</v-list-item-title>
</v-list-item>
</v-list>
</v-card>
<v-card class="mt-5">
{{ selection }}
</v-card>
</v-main>
</v-app>
</template>
<script>
export default {
data: () => ({
selection: 0,
items: [
{ title: "Item 0", value: 0 },
{ title: "Item 1", value: 1 },
{ title: "Item 2", value: 2 },
],
}),
};
</script>
答案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
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论