Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete 1 file on 3 files uploaded on vuetify?

I read on the docs : https://vuetifyjs.com/en/components/file-inputs#selection-slot

My code like this :

<v-file-input
  v-model="files"
  placeholder="Upload your documents"
  label="File input"
  multiple
  prepend-icon="mdi-paperclip"
>
  <template v-slot:selection="{ text }">
    <v-chip
      small
      label
      color="primary"
    >
      {{ text }}
    </v-chip>
  </template>
</v-file-input>

My codepen : https://codepen.io/positivethinking639/pen/vYONqKa?editable=true&editors=101

When upload 3 images and delete, it will delete all images. I want the user to be able to delete 1 image according to his choice. So the user can delete 1 picture

How can I do it?

like image 214
moses toh Avatar asked Feb 13 '20 08:02

moses toh


1 Answers

Configure the chips to delete with a handler method.

  • Add 'close' attribute to v-chip to get a close button on each file

  • Add a handler to the chip, passing index (and text if you want to prompt)

  • (optional) remove the clear-all button on VFileInput to prevent user confusion

Template

<v-file-input
  ...
  :clearable="false"
>
  <template v-slot:selection="{ index, text }">
    <v-chip
      ...
      close
      @click:close="deleteChip(index, text)"
    >
      {{ text }}
    </v-chip>
  </template>

Component

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data: () => ({
    files: [],
  }),
  methods: {
    deleteChip(index, text) {
      // Prompt here with text if required
      this.files.splice(index, 1)
    },
  }
})
like image 150
Richard Matsen Avatar answered Oct 13 '22 12:10

Richard Matsen