Login

Admin actions as buttons instead of a menu [v2]

Author:
itavor
Posted:
December 15, 2011
Language:
JavaScript
Version:
1.3
Score:
0 (after 0 ratings)

Add this to your admin change_list.html template to replace the actions drop-down with buttons.

This is a rewritten version of snippet 1931 with Django 1.3 compatibility and cleaner code. Thanks to andybak for the idea.

 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
(function ($) {

    function fix_actions() {
        var container = $('div.actions');

        if (container.find('option').length < 10) {
            container.find('label, button').hide();

            var buttons = $('<div></div>')
                .prependTo(container)
                .css('display', 'inline')
                .addClass('class', 'action-buttons');

            container.find('option:gt(0)').each(function () {
                $('<button>')
                    .appendTo(buttons)
                    .attr('name', this.value)
                    .addClass('button')
                    .text(this.text)
                    .click(function () {
                        container.find('select')
                            .find(':selected').attr('selected', '').end()
                            .find('[value=' + this.name + ']').attr('selected', 'selected');
                        $('#changelist-form button[name="index"]').click();
                    });
            });
        }
    };

    $(function () {
        fix_actions();
    });
})(django.jQuery);

More like this

  1. Django Collapsed Stacked Inlines by applecat 1 year, 10 months ago
  2. Django Collapsed Stacked Inlines by mkarajohn 3 years, 11 months ago
  3. Dynamically adding forms to a formset. OOP version. by halfnibble 9 years, 7 months ago
  4. Convert multiple select for m2m to multiple checkboxes in django admin form by abidibo 11 years, 8 months ago
  5. Django admin inline ordering - javascript only implementation by ojhilt 12 years ago

Comments

Please login first before commenting.