Login

Drag and drop ordering of admin list elements for Grappelli [v2]

Author:
brejoc
Posted:
January 1, 2013
Language:
Python
Version:
1.4
Tags:
django sorting django-admin sortable grappelli
Score:
4 (after 4 ratings)

Adds drag-and-drop ordering of rows in the admin list view for Grappelli. This is a updated version of Snippet #2306 that works with the current version of Grappelli.

The model needs to have a field holding the position and that field has to be made list_editable in the ModelAdmin. The changes of the ordering are applied after clicking 'Save'.

  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
125
126
127
# 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/admin_list_reorder.js',
        )
    
    list_display = ('position',)   # Don't forget to add the other fields that should be displayed in the list view here
    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

django.jQuery(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 = django.jQuery('.grp-changelist-results tbody tr:first').children()
    
    for (i = 0; i < cols.length; i++) {
        inputs = django.jQuery(cols[i]).find("*").filter(function() {
            return /^form(.*?)position$/.test(django.jQuery(this).attr("name"));
        })
        //inputs = django.jQuery(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 = django.jQuery('.grp-changelist-results thead tr').children()[pos_col]
    django.jQuery(header).css('width', '1em')
    django.jQuery(header).children('a').text('#')
    
    // Hide position field
    django.jQuery('.grp-changelist-results tbody tr').each(function(index) {
        pos_td = django.jQuery(this).children()[pos_col];
        input = django.jQuery(pos_td).children('input').first();
        input.hide();
        
        label = django.jQuery('<strong>' + input.attr('value') + '</strong>');
        django.jQuery(pos_td).append(label);
    });
    
    // Determine sorted column and order
    sorted = django.jQuery('.grp-changelist-results thead th.sorted');
    sorted_col = django.jQuery('.grp-changelist-results 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;
    }
    
    django.jQuery('.grp-changelist-results tbody tr').css('cursor', 'move');
    
    django.jQuery('.grp-changelist-results tbody').sortable({
        axis: 'y',
        items: 'tr',
        cursor: 'move',
        update: function(event, ui) {
            item = ui.item;
            items = django.jQuery(this).find('tr').get();
            
            if (sort_order == 'desc') {
                // Reverse order
                items.reverse();
            }
            
            django.jQuery(items).each(function(index) {
                pos_td = django.jQuery(this).children()[pos_col];
                input = django.jQuery(pos_td).children('input').first();
                label = django.jQuery(pos_td).children('strong').first();
                
                input.attr('value', index);
                label.text(index);
            });
            
            // Update row classes
            django.jQuery(this).find('tr').removeClass('row1').removeClass('row2');
            django.jQuery(this).find('tr:even').addClass('row1');
            django.jQuery(this).find('tr:odd').addClass('row2');
        }
    });

    // Broken table fix. Seems to collide with internal css.
    django.jQuery('tbody.ui-sortable').removeClass('ui-sortable');
});

More like this

  1. Drag and drop admin list items by chrsgrrtt 4 years, 11 months ago
  2. Drag and drop ordering of admin list elements for Grappelli by sjaensch 4 years, 7 months ago
  3. Ajax ordering models on the change list page of the admin using drag and drop with jQuery UI by spoof 5 years, 2 months ago
  4. Drag and drop ordering of admin list elements using jQuery UI by johj 5 years, 1 month ago
  5. Django admin inline ordering - javascript only implementation by ojhilt 2 years, 7 months ago

Comments

itsmike (on April 5, 2013):

Also works with Django v1.5

#

MaxSilent (on April 7, 2013):

I have some problems with js

Uncaught TypeError: Object [object Object] has no method 'actions' ultralit.loc:56 Uncaught TypeError: Object #[HTML_REMOVED] has no method 'sortable' admin_list_reorder.js:55

What does that mean ?

#

akaihola (on June 6, 2013):

The regex on line 53 should use the value of the pos_field variable instead of hard-coding "position".

#

legnav (on December 14, 2013):

Works in Django 1.6 but need to change the definition of MyModelAdmin with something like as follows:

Class MyModelAdmin(admin.ModelAdmin):
#.....
    list_display_links = ('field_1',)
    list_display = ('field_1', 'field_1', ...,  'position',)
    list_editable = ('position',)

#

Please login first before commenting.