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

16,576

Solution 1

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'
    });
})

Solution 2

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

Solution 3

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

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

Solution 4

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);
    });
Share:
16,576

Related videos on Youtube

ralbatross
Author by

ralbatross

Full-stack web programmer. Experience with LAMP, WordPress, Javascript (node, jQuery, etc), Laravel, .NET, ASP.NET, C/C++, Java

Updated on June 04, 2022

Comments

  • ralbatross
    ralbatross about 2 years

    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.

  • MegaBytes
    MegaBytes over 9 years
    I also have the functionality to rearrange the images and store it on server, how can I get the new order of files?
  • Samphors
    Samphors over 8 years
    Really nice, but could I ask you how to create call back function after sort-able success?
  • Atan
    Atan almost 8 years
    Thanks for the snippet, worked for me. However, in my dropzone build, filename is stored inside the span element so I wrote var name = el.innerHTML; instead of var name = el.getAttribute('data-name');
  • Lev Lukomsky
    Lev Lukomsky almost 8 years
    Also I added this: $dropzone.sortable({start: function(e, ui) { $dropzone.removeClass('dz-clickable'); ui.item.removeClass('dz-success') }, stop: function() $dropzone.addClass('dz-clickable') }, update: function(e, ui) { console.log('Item reordered!', ui.item); } – remove dz-clickable to disable open file dialog after dragging. remove dz-success because animation played every time after drag. Also I had problem with item disappearing while dragging – there was bug in jquery-ui's sortable.js – replaced this.document[0] with this.document[0].body on line 1008
  • Aaron
    Aaron almost 8 years
    @LevLukomsky What is "$dropzone"? That doesn't resolve to anything.
  • Lev Lukomsky
    Lev Lukomsky almost 8 years
    @aaron assume that $dropzone = $('.images-dropzone'); new Dropzone($dropzone.get(0), options); $dropzone.sortable(sortable_options);
  • Mike Wiesenhart
    Mike Wiesenhart over 7 years
    @ralbatross Am I correct in assuming that this should not go into the Dropzone.js file and should be entered in a separate .js file?
  • ralbatross
    ralbatross over 7 years
    @MikeWiesenhart Correct
  • Mike Wiesenhart
    Mike Wiesenhart over 7 years
    @ralbatross Great. The above code worked for me, but they are not saving like that. Any suggestions?
  • ralbatross
    ralbatross over 7 years
    @MikeWiesenhart that's probably worth a separate SO question, but basically I had to maintain a bit of extra meta-data in the dropzone items and then send that along in the POST, as well as in the response from the POST
  • Nate Beaty
    Nate Beaty about 7 years
    Same fix as Atan, as well as defining newQueue before trying to push anything to it: var queue = uploadzone.files, newQueue = [];