Mark Lavin avatar Mark Lavin committed 469e2c8

Added AutoCompleteSelectWidget.

Comments (0)

Files changed (3)

selectable/base.py

     def get_item_value(self, item):
         return smart_unicode(item)
 
+    def get_item(self, value):
+        return []
+
     def format_item(self, item):
          return {
             'id': self.get_item_id(item),

selectable/forms/widgets.py

 
 __all__ = (
     'AutoCompleteWidget',
+    'AutoCompleteSelectWidget',
 )
 
 
         self.attrs[u'data-selectable-url'] = url
         self.attrs[u'data-selectable-allow-new'] = str(self.allow_new).lower()
 
+
+class AutoCompleteSelectWidget(forms.MultiWidget):
+
+    def __init__(self, lookup_class, *args, **kwargs):
+        self.lookup_class = lookup_class
+        self.allow_new = kwargs.pop('allow_new', False)
+        widgets = [
+            AutoCompleteWidget(lookup_class, allow_new=self.allow_new),
+            forms.HiddenInput(attrs={u'data-selectable-is-hidden': 'true'})
+        ]
+        super(AutoCompleteSelectWidget, self).__init__(widgets, *args, **kwargs)
+
+    def decompress(self, value):
+        if value:
+            lookup = self.lookup_class()
+            item = lookup.get_item(value)
+            item_value = lookup.get_item_value(item)
+            return [item_value, value]
+        return [None, None]
+

selectable/static/js/jquery.dj.selectable.js

             }
         });
     });
+    $(":input[data-selectable-is-hidden]").each(function(i, elem) {
+        var hiddenName = $(elem).attr('name');
+        var textName = hiddenName.replace('_1', '_0');
+        $(":input[name=" + textName + "][data-selectable-url]").bind("autocompletechange", function(event, ui) {
+            if (ui.item && ui.item.id) {
+                $(elem).val(ui.item.id);
+            } else {
+                $(elem).val("");
+            }
+        });
+    });
 });
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.