Rumah > Soal Jawab > teks badan
P粉2037924682023-08-31 15:28:23
Nampaknya ini boleh dilakukan dengan cara yang lebih mudah.
fooItems
Mungkin perlu ada keadaan awal "disemak".
Hanya klik selectedHandler
中,只需调用emit()
.
toggleAll
最终将创建一个与 internalModel
Fungsi digunakan bersama.
Berikut ialah contoh Vue SFC Playground. < /p>
HomeView.vue:
<script setup lang="ts"> import { ref } from 'vue'; import AppList, { type Item } from './AppList.vue'; const fooItems = ref<Item[]>([ { id: 1, name: 'foo 1', checked: false }, { id: 2, name: 'foo 2', checked: false }, { id: 3, name: 'foo 3', checked: false }, { id: 4, name: 'foo 4', checked: false }, { id: 5, name: 'foo 5', checked: true }, ]); const fooSelected = ref<number[]>([]); fooItems.value.map(item => item.checked && fooSelected.value.push(item.id)) </script> <template> <AppList :items="fooItems" v-model:selected="fooSelected"></AppList> <div>fooselected: {{ fooSelected }}</div> </template>
AppList.view:
<script setup lang="ts"> import { ref } from 'vue'; export interface Item { id: number; name: string; checked: boolean } const props = defineProps<{ items: Item[]; selected: number[] }>(); const emit = defineEmits(['update:selected']); const internalModel = ref(props.selected); const selectedHandler = () => emit('update:selected', internalModel.value); const toggleAll = ($event) => { internalModel.value = []; if ( ($event.target as HTMLInputElement).checked ) { props.items.map(item => internalModel.value.push(item.id)); } emit('update:selected', internalModel.value); }; </script> <template> <label> <input type="checkbox" @change="toggleAll($event)" :checked="internalModel.length === items.length" /> toggle all </label> <ul> <li v-for="item in items" :key="item.id"> <label> <input type="checkbox" :value="item.id" v-model="internalModel" @change="selectedHandler(item.id)" :checked="item.checked"/> <span>{{ item.name }}</span> </label> </li> </ul> internalModel: {{ internalModel }} </template>