Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- On/Off icon for boolean field in list_editable modelAdmin
- .hidden {
- position:absolute;
- left:-99999px;
- width:0;
- height:0;
- overflow:hidden;
- }
- (function($){
- var on_image = '/static/admin/img/admin/icon-yes.gif';
- var off_image = '/static/admin/img/admin/icon-no.gif';
- $(document).ready(function(){
- var $checkbox = $('.checkbox_field input');
- // Can't simply `hide()` as its value will not be posted
- $checkbox.addClass('hidden');
- var $img = $('<img/>');
- if ($checkbox.attr('checked')) {
- $img.attr('href', on_image);
- $img.attr('alt', 'On');
- } else {
- $img.attr('href', off_image);
- $img.attr('alt', 'Off');
- }
- $img.insertAfter($checkbox);
- $img.click(function(){
- var $img = $(this);
- var $checkbox = $img.siblings('input');
- if ($img.attr('href') == on_image) {
- $img.attr('href', off_image);
- $img.attr('alt', 'Off');
- $checkbox.attr('checked', false);
- } else {
- $img.attr('href', on_image);
- $img.attr('alt', 'On');
- $checkbox.attr('checked', true);
- }
- });
- });
- )(django.jQuery);
Add Comment
Please, Sign In to add comment