Guest User

Untitled

a guest
Aug 14th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. On/Off icon for boolean field in list_editable modelAdmin
  2. .hidden {
  3. position:absolute;
  4. left:-99999px;
  5. width:0;
  6. height:0;
  7. overflow:hidden;
  8. }
  9.  
  10. (function($){
  11. var on_image = '/static/admin/img/admin/icon-yes.gif';
  12. var off_image = '/static/admin/img/admin/icon-no.gif';
  13.  
  14. $(document).ready(function(){
  15. var $checkbox = $('.checkbox_field input');
  16. // Can't simply `hide()` as its value will not be posted
  17. $checkbox.addClass('hidden');
  18. var $img = $('<img/>');
  19. if ($checkbox.attr('checked')) {
  20. $img.attr('href', on_image);
  21. $img.attr('alt', 'On');
  22. } else {
  23. $img.attr('href', off_image);
  24. $img.attr('alt', 'Off');
  25. }
  26. $img.insertAfter($checkbox);
  27.  
  28. $img.click(function(){
  29. var $img = $(this);
  30. var $checkbox = $img.siblings('input');
  31.  
  32. if ($img.attr('href') == on_image) {
  33. $img.attr('href', off_image);
  34. $img.attr('alt', 'Off');
  35. $checkbox.attr('checked', false);
  36. } else {
  37. $img.attr('href', on_image);
  38. $img.attr('alt', 'On');
  39. $checkbox.attr('checked', true);
  40. }
  41. });
  42. });
  43. )(django.jQuery);
Add Comment
Please, Sign In to add comment