2

I am trying to add a select all checkbox inside v-select. It's working fine with options API in vue js. But when working with composition API, couldn't find a way to workable it yet. My attempt is as below.

     setup() {
       const fruits = ['Apples', 'Apricots', 'Avocado', 'Bananas']
       let selectedFruits = []
       const likesAllFruit = computed(() => {
         return selectedFruits.length === fruits.length
       })
       const likesSomeFruit = computed(() => {
         return selectedFruits.length > 0 && !likesAllFruit.value
       })
       const icon = computed(() => {
         if (likesAllFruit.value) return 'mdi-close-box'
         if (likesSomeFruit.value) return 'mdi-minus-box'
         return 'mdi-checkbox-blank-outline'
       })
       const toggle = async () => {   
         if (likesAllFruit.value) {
           selectedFruits = []
         } else {
           selectedFruits = fruits.slice()
         }
         await nextTick()
       }
       return {
        fruits,
        selectedFruits,
        likesAllFruit,
        likesSomeFruit,
        icon,
        toggle,
      }
    },

I used https://vuetifyjs.com/en/components/selects/#append-and-prepend-item to build this as per in the document. Anyone knows where I was wrong this with in composition API?

(I am using vue js 2 version with composition API plugging)

5
  • What version of vuetify do you use? Commented Aug 1, 2021 at 12:03
  • ^1.11.3 version. No idea why it's not working with composition API only. Any issue in above code? Commented Aug 1, 2021 at 15:19
  • Vuetify is not compatible with vue 3, officially stackoverflow.com/q/62871984/2073738 Commented Aug 1, 2021 at 16:05
  • fruits and selectedFruits should be using ref(). Vuetify is not supporting vue3. They currently have an alpha version for vue3. Commented Aug 1, 2021 at 16:06
  • I am using vue js 2 version with composition API plugging. Commented Aug 1, 2021 at 18:44

1 Answer 1

0

Below way works.

setup() {
  const fruits = ref([
    { text: 'Apples', value: 'Apples' },
    { text: 'Apricots', value: 'Apricots' },
    { text: 'Avocado', value: 'Avocado' },
    { text: 'Bananas', value: 'Bananas' },
  ])
  let selectedFruits = ref([{}])
  const likesAllFruit = computed(() => {
    return selectedFruits.value.length === fruits.value.length
  })
  const likesSomeFruit = computed(() => {
    return selectedFruits.value.length > 0 && !likesAllFruit.value
  })
  const icon = computed(() => {
    if (likesAllFruit.value) return 'mdi-close-box'
    if (likesSomeFruit.value) return 'mdi-minus-box'
    return 'mdi-checkbox-blank-outline'
  })
  const toggle = async () => {
    if (likesAllFruit.value) {
     selectedFruits.value = []
    } else {
     selectedFruits.value = fruits.value.slice()
    }
    await nextTick()
  }
  return {
    fruits,
    selectedFruits,
    likesAllFruit,
    likesSomeFruit,
    icon,
    toggle,
  }
},

Thanks all!!!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.