Login

Allow editing of the selected object of the filter_vertical/filter_horizontal widget with jquery

Author:
jpic
Posted:
October 10, 2008
Language:
JavaScript
Version:
Not specified
Tags:
admin jquery widget filter_horizontal filter_vertical
Score:
-3 (after 3 ratings)

This patch allows to open a popup to edit the selected object of either the left or right select input of a filter_horizontal or filter_vertical field, when pressing the "Insert" key.

Being a noob with general client-side issue, it's served as a patch that suits my needs. Maybe it's possible to do it cleaner by overloading the javascript methods, or get rid of the jquery dependency in order to integrate it into django trunk ... Any contribution is welcome! It's licensed under WTFPL license.

Requires jquery.

 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
Index: django/contrib/admin/media/js/SelectFilter2.js
===================================================================
--- django/contrib/admin/media/js/SelectFilter2.js      (revision 9220)
+++ django/contrib/admin/media/js/SelectFilter2.js      (working copy)
@@ -71,6 +71,8 @@
         addEvent(filter_input, 'keydown', function(e) { SelectFilter.filter_key_down(e, field_id); });
         addEvent(from_box, 'dblclick', function() { SelectBox.move(field_id + '_from', field_id + '_to'); });
         addEvent(to_box, 'dblclick', function() { SelectBox.move(field_id + '_to', field_id + '_from'); });
+        addEvent(from_box, 'keypress', function(e) { SelectBox.edit(e, field_id + '_from'); });
+        addEvent(to_box, 'keypress', function(e) { SelectBox.edit(e, field_id + '_to'); });
         addEvent(findForm(from_box), 'submit', function() { SelectBox.select_all(field_id + '_to'); });
         SelectBox.init(field_id + '_from');
         SelectBox.init(field_id + '_to');
Index: django/contrib/admin/media/js/SelectBox.js
===================================================================
--- django/contrib/admin/media/js/SelectBox.js  (revision 9220)
+++ django/contrib/admin/media/js/SelectBox.js  (working copy)
@@ -107,5 +107,16 @@
         for (var i = 0; i < box.options.length; i++) {
             box.options[i].selected = 'selected';
         }
+    },
+    edit: function(event, id) {
+        // 45 = Insert
+        if ((event.which && event.which == 45) || (event.keyCode && event.keyCode == 45)) {
+            var selected_id = document.getElementById(id).value;
+            // Thanks trovster from #jquery@freenode!
+            var link = $('#'+id).parents().find('a.add-another:first').attr('href')+'../'+selected_id
+            var win = window.open(link + '?_popup=1', link, 'height=600,width=1000,resizable=yes,scrollbars=yes');
+            win.focus();
+            return false;
+        }
     }
 }

More like this

  1. admin: edit related object shortcut by christian 7 years, 5 months ago
  2. jQuery Double Click Edit ManyToMany in Admin by justhamade 5 years, 1 month ago
  3. Multiple Select Widget with jQueryUI by maguspk 4 years, 8 months ago
  4. Select Dropdown Widget with jQueryUI by maguspk 4 years, 8 months ago
  5. DisableableSelectWidget by notanumber 2 years, 9 months ago

Comments

Please login first before commenting.