Mark Lavin avatar Mark Lavin committed eb7ced7

Started to add widgets with JS bindings.

Comments (0)

Files changed (4)

selectable/base.py

+from django.core.urlresolvers import reverse
 from django.utils.encoding import smart_unicode
 
 
         return name
     name = classmethod(_name)
 
+    def _url(cls):
+        return reverse('selectable-lookup', args=[cls.name()])
+    url = classmethod(_url)
+
     def get_query(self, request):
         return []
 
-    def format_item(self, item):
+    def get_item_name(self, item):
         return smart_unicode(item)
 
+    def get_item_id(self, item):
+        return smart_unicode(item)
+
+    def get_item_value(self, item):
+        return smart_unicode(item)
+
+    def format_item(self, item):
+         return {
+            'id': self.get_item_id(item),
+            'value': self.get_item_value(item),
+            'name': self.get_item_name(item)
+        }
+
+

selectable/forms/__init__.py

-from selectable.fields import *
-from selectable.widgets import *
+from selectable.forms.widgets import *

selectable/forms/widgets.py

+from django import forms
+
+
+__all__ = (
+    'AutoCompleteWidget',
+)
+
+
+class AutoCompleteWidget(forms.TextInput):
+
+    def __init__(self, lookup_class, *args, **kwargs):
+        self.lookup_class = lookup_class
+        super(AutoCompleteWidget, self).__init__(*args, **kwargs)
+        # New attrs
+        url = lookup_class.url()
+        self.attrs['data-selectable-url'] = url
+

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

+$(document).ready(function() {
+    $(":input[data-selectable-url]").each(function(i, elem) {
+        var url = $(elem).attr('data-selectable-url');
+        $(elem).autocomplete({source: url});
+    });
+});
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.