Vue 3 custom checkbox component with v-model and array of items

Fersek

Desperately in need of your help guys.

So basically I have a custom checkbox component whit a v-model. I use a v-for loop on the component to display checkboxes with the names from the array. In the parent component I have two columns Available and Selected. The idea is that if I check one of the boxes in the Available column it should appear on the Selected column. The problem is that it displays letter by letter and not the full name.

I am able to achieve the desired result without having a checkbox component, but since I will be needing checkboxes a lot throught my project I want to have a component for it.

Please follow the link for the code: CodeSandBox

Dont mind the difference in styling.

The problem:

Problem

The desired outcome:

Desired outcome

StevenSiebert

There are two problems. The first problem is, that you have your v-model set to v-model="filter.filterCollection", so a checkbox you select will be stored into the array as a string and if you select another checkbox the string gets overwritten. The second problem is, that you call that stored string as an array. That causes, that your string, which is an array of letters, will be rendered for each letter. So 'Number' is like ["N", "u", "m", "b", "e", "r"].

To solve your problem, you need to store every selection with its own reference in your v-model. To cover your needs of correct listing and correct deleting you need to apply the following changes:

Your checkbox loop

<Checkbox
   v-for="(item, index) in items"
   :key="item.id"
   :label="item.name"
   :id="index"
   :isChecked="isChecked(index)" // this is new
   @remove-selected-filter="removeSelectedFilter" // This is new
   :modelValue="item.name"
   v-model="filter.filterCollection[index]" // Change this
/>

Your v-model

filter: {
        filterCollection: {} // Object instead of array
      }

Methods in FilterPopUp.vue

methods: {
    removeSelectedFilter(index) {
      delete this.filter.filterCollection[index];
    },
    isChecked(index) {
      return !!this.filter.filterCollection[index];
    }
  }

Your Checkbox.vue:

<template>
  <label>
    <p>{{ label }}</p>
    <input
      type="checkbox"
      :id="id"
      :value="modelValue"
      :checked="isChecked"
      @change="emitUncheck($event.target.checked)"
      @input="$emit('update:modelValue', $event.target.value)"
    />
    <span class="checkmark"></span>
  </label>
</template>

<script>
export default {
  name: "Checkbox",
  props: {
    modelValue: { type: String, default: "" },
    isChecked: Boolean,
    label: { type: String },
    value: { type: Array },
    id: { type: Number },
  },
  methods: {
    emitUncheck(event) {
      if(!event){
        this.$emit('remove-selected-filter', this.id);
      }
    }
  }
};
</script>

This should now display your items properly, delete the items properly and unselect the checkboxes after deleting the items.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Custom component in ionic v3

Vue.js Component with v-model

VueJS. v-model in custom component

Vue.JS checkbox without v-model

Vue: v-model and input event in custom component derived of a custom component

Vue.js custom select component with v-model

v-model for child component and v-model inside child component Vue

Vue Component with Stripe JS v3

How to enable v-model on custom component?

Vue does not update items in v-for from Vuex with dynamic component

vue checkbox v-model binding not working properly in the current version

Custom Bootstrap-Vue checkbox component

How declare v-model for custom component in vue render function?

use vue and v-model with a css specific checkbox

Custom Vue select component not updating selected option when v-model value changed

How to use v-model on component in vue 3 script setup

Vue 3 select component how to bind 2 attributes to 2 v-model

Vue3: problems with v-model, it does not update the component

How to pass v-model value to child's child component in Vue 3

V-model not working in ant design input component vue 3

VueJs 3 checkbox v-model on array of objects not working properly

How to get an Array data with custom Component in Vue

Vue 3 multiple v-model from child component return empty result

v-model with boolean and array type on same checkbox component

vue 3 script setup bind v-model to child custom component multiple checkbox

Vue - using v-model on a <component> with :is

v-model not working with <component> in Vue 3?

Vue 3 v-model binding on a nested input component

Vue 3 Option api: Parent element not receiving v-model input from child component