Is there a way to do drag-and-drop re-ordering of the preview elements in a dropzone.js instance?

StackOverflow https://stackoverflow.com/questions/22666735

Question

I have a dropzone.js instance on a web page with the following options:

autoProcessQueue:false
uploadMultiple:true
parallelUploads:20
maxFiles:20

It is programmatically instantiated, as it is part of a larger form. I have it rigged up to process the queue when the form is submitted.

The goal is for my users to be able to use the dropzone to manage images for an item, so I'd like them to be able to re-order the images by dragging and dropping the dropzone.js image previews. Is there a good way to do this? I've tried using jquery-ui's sortable but it doesn't seem to play nice with dropzone.js.

Was it helpful?

Solution

I've got it working now using jquery-ui's sortable. The trick was to make sure to use the 'items' option in sortable to pick only the dz-preview elements, because dropzone.js has the dz-message element along with the dz-preview elements in the main container. Here's how my code looks:

The HTML:

<div id="image-dropzone" class="dropzone square">

The script:

$(function() {
    $("#image-dropzone").sortable({
        items:'.dz-preview',
        cursor: 'move',
        opacity: 0.5,
        containment: '#image-dropzone',
        distance: 20,
        tolerance: 'pointer'
    });
})

OTHER TIPS

Besides the code from ralbatross you will need to set the order of the file queue of dropzone..

Something like:

$("#uploadzone").sortable({
    items: '.dz-preview',
    cursor: 'move',
    opacity: 0.5,
    containment: '#uploadzone',
    distance: 20,
    tolerance: 'pointer',
    stop: function () {

        var queue = uploadzone.files;
        $('#uploadzone .dz-preview .dz-filename [data-dz-name]').each(function (count, el) {           
            var name = el.getAttribute('data-name');
            queue.forEach(function(file) {
               if (file.name === name) {
                    newQueue.push(file);
               }
            });
        });

        uploadzone.files = newQueue;

    }
});

And remember that the file is processed async, i keep an hashtable for reference when the file is done and save the order at the end.

It doesn't work with duplicate filenames

You can use SortableJS (https://github.com/SortableJS/Sortable)

new Sortable(document.getElementById('dropzone'), {
  draggable: '.dz-preview'
})

Here's another option without any plugins. On the success event callback, you can do some manual sorting:

   var rows = $('#dropzoneForm').children('.dz-image-preview').get();

    rows.sort(function (row1, row2) {
        var Row1 = $(row1).children('.preview').find('img').attr('alt');   
        var Row2 = $(row2).children('.preview').find('img').attr('alt');
        if (Row1 < Row2) {
            return -1;
        }

        if (Row1 > Row2) {
            return 1;
        }
        return 0;
    });


    $.each(rows, function (index, row) {
        $('#dropzoneForm').append(row);
    });

this is how to reorder the preview images with duplicate file names

 //allow the reordering of the images being rendered
    $('.dropzone').sortable({
            items: '.dz-preview',
            cursor: 'move',
            opacity: 0.5,
            containment: '.dropzone',
            distance: 20,
            tolerance: 'pointer',
            stop: function () {
                var queue = myDropzone.files;
                newQueue = [];
                $('.dropzone .dz-preview .dz-filename [data-dz-name]').each(function (count, el) {
                    var name = el.innerHTML;

                    queue.every(function(file, index){ 
                        if (file.name === name) {
                            newQueue.push(file);
                            return false;
                        }
                        return true;
                    });
                });
               myDropzone.files = newQueue;
          }
        });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top