Guest User

Untitled

a guest
May 26th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. <template>
  2. <div class="card">
  3. <div class="card-header">
  4. <h3 class="card-title">Imagens</h3>
  5. <div class="card-options">
  6. <button class="btn btn-primary" @click="$refs.fileInput.click()">
  7. <i class="fa fa-plus"></i>
  8. <input type="file" ref="fileInput" multiple hidden @change="onFileChange">
  9. </button>
  10. </div>
  11. </div>
  12. <div class="card-body">
  13. <div class="row">
  14. <div class="col-lg-4"
  15. v-for="(image, index) in images"
  16. :key="index">
  17. <img :src="image" alt="Image">
  18. <button type="button" class="btn btn-danger" @click="removeImage(index)">
  19. <i class="fa fa-minus"></i>
  20. </button>
  21. </div>
  22. </div>
  23. </div>
  24. </div>
  25. </template>
  26.  
  27. <script>
  28. export default {
  29. name: 'ImagesForm',
  30. props: {
  31. model: {
  32. type: Object
  33. },
  34. images: {
  35. type: Array
  36. },
  37. validations: {
  38. type: Object
  39. }
  40. },
  41. mounted () {
  42. this.images.map((e, i) => this.createFile(e, i))
  43. },
  44. methods: {
  45. onFileChange (e) {
  46. const files = e.target.files || e.dataTransfer.files
  47. if (!files.length) {
  48. return
  49. }
  50. Array.from(files).forEach(file => this.createImage(file))
  51. Array.from(files).forEach(file => this.model.images.push(file))
  52. },
  53. createImage (file) {
  54. const reader = new FileReader()
  55. reader.onload = e => this.images.push(e.target.result)
  56. reader.readAsDataURL(file)
  57. },
  58. createFile (url, index) {
  59.  
  60. },
  61. removeImage (index) {
  62. this.images.splice(index, 1)
  63. this.model.images = Array.from(this.model.images).filter((_, i) => i !== index)
  64. }
  65. }
  66. }
  67. </script>
Add Comment
Please, Sign In to add comment