Login

Admin actions as buttons instead of a menu

Author:
andybak
Posted:
February 19, 2010
Language:
JavaScript
Version:
1.6
Score:
2 (after 2 ratings)

Add this to any admin changelist and your actions drop-down will be replaced with user-friendly buttons.

Why mess around with templates and subclassing admin classes when you can just mangle the page with jQuery! ;-)

It also adds a 'select all' label to explain the mystery top check-box (well it was a mystery to several of my clients).

The line "if ($('div.actions option:gt(0)').length<=8)" checks that there aren't more than 8 actions and falls back to the drop-down if there are.

Requires jQuery to be loaded.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
function do_action(action) {
    $('div.actions option[selected]').attr('selected', '');
    $('div.actions option[value='+action+']').attr('selected', 'selected');
    $("div.actions").parent().submit();
}

function fix_actions() {
    $('th.action-checkbox-column:first').prepend('select<br />all<br />');
    if ($('div.actions option:gt(0)').length<=8) { // Only do this for short lists. Probably need to tweak this number.
        
        $('div.actions label, div.actions button').hide();
        actions_html='<div id="action_buttons" style="padding: 2px 0 2px 0;">';
        $('div.actions option:gt(0)').each(function(i) {
            actions_html+='<a href="javascript:void(0);" onclick="do_action(\''+this.value+'\');">'+this.text+'</a>';
        });
        actions_html+='</div>';
        $('div.actions').append(actions_html);
    }
}

More like this

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

Comments

david_bgk (on February 20, 2010):

Looks interesting! Can you provide a screenshot of this feature?

#

andybak (on February 26, 2010):

I could but trying the code would take you less time than it will take me to upload a screenshot ;-)

#

hellboy (on January 11, 2011):

How can I use it in my projects?

{% extends "admin/change_list.html" %} {% block extrahead %}

<script type="text/javascript" src="{% staticfile 'js/jquery-1.3.2.min.js' %}"></script> <script type="text/javascript" src="{% staticfile 'js/jquery-ui-1.7.2.custom.min.js' %}"></script> <script type="text/javascript"> <script type="text/javascript" src="/amedia/js/admin/actions_as_buttons.js"></script>

{% endblock %}

and?

how to call fix_actions?

#

Please login first before commenting.