Login

Drag and drop ordering of admin list elements using jQuery UI

Author:
johj
Posted:
June 8, 2010
Language:
Python
Version:
1.2
Score:
5 (after 5 ratings)

Adds drag-and-drop ordering of rows in the admin list view.

The only requirements is that the model has a field holding the position and that the field is made list_editable in the ModelAdmin. The changes of the ordering are applied after clicking 'Save'.

The included javascript uses jQuery UI's sortable plugin

Inspired by snippets #1053 and #998. Another similar snippet using AJAX is #2047.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# models.py

class MyModel(models.Model):
    position = models.IntegerField()    # The position field
    ...

    def save(self, *args, **kwargs):
        model = self.__class__
        
        if self.position is None:
            # Append
            try:
                last = model.objects.order_by('-position')[0]
                self.position = last.position + 1
            except IndexError:
                # First row
                self.position = 0
        
        return super(MyModel, self).save(*args, **kwargs)
    
    class Meta:
        ordering = ('position',)


# admin.py

class MyModelAdmin(admin.ModelAdmin):

    class Media:
        js = (
            'js/jquery.min.js',
            'js/jquery-ui.min.js',
            'js/admin-list-reorder.js',
        )
    
    list_editable = ['position']  # 'position' is the name of the model field which holds the position of an element

admin.site.register(MyModel, MyModelAdmin)


# admin-list-reorder.js

$(document).ready(function() {
    // Set this to the name of the column holding the position
    pos_field = 'position';
    
    // Determine the column number of the position field
    pos_col = null;
    
    cols = $('#result_list tbody tr:first').children()
    
    for (i = 0; i < cols.length; i++) {
        inputs = $(cols[i]).find('input[name*=' + pos_field + ']')
        
        if (inputs.length > 0) {
            // Found!
            pos_col = i;
            break;
        }
    }
    
    if (pos_col == null) {
        return;
    }
    
    // Some visual enhancements
    header = $('#result_list thead tr').children()[pos_col]
    $(header).css('width', '1em')
    $(header).children('a').text('#')
    
    // Hide position field
    $('#result_list tbody tr').each(function(index) {
        pos_td = $(this).children()[pos_col]
        input = $(pos_td).children('input').first()
        //input.attr('type', 'hidden')
        input.hide()
        
        label = $('<strong>' + input.attr('value') + '</strong>')
        $(pos_td).append(label)
    });
    
    // Determine sorted column and order
    sorted = $('#result_list thead th.sorted')
    sorted_col = $('#result_list thead th').index(sorted)
    sort_order = sorted.hasClass('descending') ? 'desc' : 'asc';
    
    if (sorted_col != pos_col) {
        // Sorted column is not position column, bail out
        console.info("Sorted column is not %s, bailing out", pos_field);
        return;
    }
    
    $('#result_list tbody tr').css('cursor', 'move')
    
    // Make tbody > tr sortable
    $('#result_list tbody').sortable({
        axis: 'y',
        items: 'tr',
        cursor: 'move',
        update: function(event, ui) {
            item = ui.item
            items = $(this).find('tr').get()
            
            if (sort_order == 'desc') {
                // Reverse order
                items.reverse()
            }
            
            $(items).each(function(index) {
                pos_td = $(this).children()[pos_col]
                input = $(pos_td).children('input').first()
                label = $(pos_td).children('strong').first()
                
                input.attr('value', index)
                label.text(index)
            });
            
            // Update row classes
            $(this).find('tr').removeClass('row1').removeClass('row2')
            $(this).find('tr:even').addClass('row1')
            $(this).find('tr:odd').addClass('row2')
        }
    });
});

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

flipflop (on July 8, 2010):

Can you modify this to work with Grappelli?

#

valentina (on July 15, 2010):

Wher can I put jQuery UI's sortable plugin? In this way doesn't work:

... class Media: js = ( ....... ...... '/static/js/ui.core.js', '/static/js/ui.sortable.js',

    )

#

vbert (on January 18, 2011):

why ie8 cat error in line:

header = $('#result_list thead tr').children()[pos_col]

??

#

savilmik (on January 20, 2011):

Nice work. Works like a charm on Chrome. I did some additions for hiding the ordering column in javascript:

Replaced line 68 and 69 with

$(header).hide();

and added line after 79:

$(pos_td).hide();

#

aquamatt (on July 13, 2012):

To make this work in IE8, whilst somewhat obscure, search and replace header with e.g. heder; header, even as a variable name, seems to cause IE8 consternation

Line 101 is redundant.

#

feliperfranca (on November 16, 2012):

ImproperlyConfigured at / 'MyModelAdmin.list_editable[0]' refers to 'position' which is not defined in 'list_display'.

#

Please login first before commenting.