Last active
November 27, 2022 15:20
-
-
Save prpanto/382802c2534a867b935a97213c4d1b76 to your computer and use it in GitHub Desktop.
Examples with multiple checkbox in VueJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<Checkbox v-for="person in people" :id="person.id" :label="person.name" :data="person" v-model="selectedPeople" /> | |
<pre>{{ selectedPeople }}</pre> | |
</template> | |
<script setup> | |
import { ref } from 'vue' | |
import Checkbox from './Checkbox.vue' | |
const people = ref([ | |
{ id: 1, name: 'John Doe' }, | |
{ id: 2, name: 'Jane Doe' }, | |
{ id: 3, name: 'Alex Doe' }, | |
]) | |
const selectedPeople = ref([]) | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<input :id="$attrs.id" type="checkbox" :value="data" v-model="model"> | |
<label :for="$attrs.id">{{ label }}</label><br> | |
</template> | |
<script setup> | |
import { computed, defineExpose, ref } from 'vue' | |
const props = defineProps({ | |
data: { | |
type: Object, | |
default: () => ({}) | |
}, | |
modelValue: Array, | |
label: String | |
}) | |
const emit = defineEmits(['update:modelValue']) | |
const model = computed({ | |
get: () => props.modelValue, | |
set: items => emit('update:modelValue', items) | |
}) | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<div> | |
<input id="all" type="checkbox" @click="checkAll()" v-model="isCheckAll"> | |
<label for="all">Select All</label> | |
</div><br> | |
<div v-for="person in people" :key="person.id"> | |
<input :id="person.id" type="checkbox" :value="person" v-model="selectedPeople" @change="allSelected()"> | |
<label :for="person.id">{{ person.name }}</label> | |
</div><br> | |
<pre>{{ selectedPeople }}</pre> | |
</template> | |
<script setup> | |
import { ref } from 'vue' | |
const people = ref([ | |
{ id: 1, name: 'John Doe' }, | |
{ id: 2, name: 'Jane Doe' }, | |
{ id: 3, name: 'Alex Doe' }, | |
]) | |
const selectedPeople = ref([]) | |
const isCheckAll = ref(false) | |
const checkAll = (value) => { | |
!isChecked.value | |
? selectedPeople.value = people | |
: selectedPeople.value = [] | |
} | |
const allSelected = () => isChecked.value = selectedPeople.value.length === people.length | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment