I need to get id of my existing images into dropzone preview and ability of sending sort order to backend
What I have
- get existing images
- sort images
What I need
- Add images IDs to preview
- send sort order to back-end
Code
Each part of the code is commented for better understanding
Dropzone.autoDiscover = false;
var myDropzone = new Dropzone("#my-awesome-dropzone", {
headers: {
"X-CSRF-TOKEN": $("meta[name='csrf-token']").attr("content")
},
acceptedFiles: ".jpeg,.jpg,.png,.gif",
dictDefaultMessage: "Drag an image here to upload, or click to select one",
maxFiles: 15, // Maximum Number of Files
maxFilesize: 8, // MB
addRemoveLinks: true,
url: '{{ url('admin/dropzoneStore') }}/'+encodeURI('{{$product->id}}'),
dictRemoveFile: 'Remove',
dictFileTooBig: 'Image is bigger than 8MB',
// get uploaded images in dropzone box
init: function() {
myDropzone = this;
// get current images
const images = @json($images);
$.each(images, function(index, value) {
var mockFile = {name: value.name, size: value.size, id: value.id };
myDropzone.options.addedfile.call(myDropzone, mockFile);
myDropzone.options.thumbnail.call(myDropzone, mockFile, "/images/"+value.name);
myDropzone.options.complete.call(myDropzone, mockFile);
myDropzone.options.success.call(myDropzone, mockFile);
});
}
});
// remove files from both view and database
myDropzone.on("removedfile", function(file,response) {
$.ajax({
url: '{{ url('admin/destroyOnEdit') }}/'+encodeURI(file.name),
type: 'DELETE',
dataType: "JSON",
data: {
"name": file.name,
"_method": 'DELETE',
"_token": "{{ csrf_token() }}",
},
success:function(data) {
myDropzone.removeFile(file);
}
});
});
// sort images in preview (need to send this sort to back-end and update images "sort" column value)
$(function() {
$(".dropzone").sortable({
items: '.dz-preview',
cursor: 'move',
opacity: 0.5,
containment: '.dropzone',
distance: 20,
tolerance: 'pointer',
stop: function(event, ui) {
var cloned = $('div#botofform').clone()
$('#botofform').html("")
$('.dropzone .dz-complete').each(function() {
var data_id = $(this).data('id')
console.log('event', event)
console.log('ui', ui)
console.log('data_id', data_id)
$(cloned).find("input[data-id=" + data_id + "]").clone().appendTo($('#botofform'))
});
}
});
});
Any idea?