Commit 32f4edb3 authored by PJ Janse van Rensburg's avatar PJ Janse van Rensburg

Merge branch 'ajax-min-input-length'

parents 9264a987 ab23b6c5
...@@ -6,6 +6,7 @@ next release ...@@ -6,6 +6,7 @@ next release
* Sort on multiple columns with `column_default_sort` * Sort on multiple columns with `column_default_sort`
* Upgrade Leaflet and Leaflet.draw plugins, used for geoalchemy integration * Upgrade Leaflet and Leaflet.draw plugins, used for geoalchemy integration
* Specify `minimum_input_length` for ajax widget
1.5.2 1.5.2
----- -----
......
...@@ -130,8 +130,11 @@ class PostAdmin(sqla.ModelView): ...@@ -130,8 +130,11 @@ class PostAdmin(sqla.ModelView):
'fields': (User.username, User.email) 'fields': (User.username, User.email)
}, },
'tags': { 'tags': {
'fields': (Tag.name,) 'fields': (Tag.name,),
} 'minimum_input_length': 0,
'placeholder': 'Please select',
'page_size': 5,
},
} }
def __init__(self, session): def __init__(self, session):
......
...@@ -50,17 +50,18 @@ class QueryAjaxModelLoader(AjaxModelLoader): ...@@ -50,17 +50,18 @@ class QueryAjaxModelLoader(AjaxModelLoader):
def get_list(self, term, offset=0, limit=DEFAULT_PAGE_SIZE): def get_list(self, term, offset=0, limit=DEFAULT_PAGE_SIZE):
query = self.model.objects query = self.model.objects
criteria = None if len(term) > 0:
criteria = None
for field in self._cached_fields: for field in self._cached_fields:
flt = {u'%s__icontains' % field.name: term} flt = {u'%s__icontains' % field.name: term}
if not criteria: if not criteria:
criteria = mongoengine.Q(**flt) criteria = mongoengine.Q(**flt)
else: else:
criteria |= mongoengine.Q(**flt) criteria |= mongoengine.Q(**flt)
query = query.filter(criteria) query = query.filter(criteria)
if offset: if offset:
query = query.skip(offset) query = query.skip(offset)
......
...@@ -52,16 +52,17 @@ class QueryAjaxModelLoader(AjaxModelLoader): ...@@ -52,16 +52,17 @@ class QueryAjaxModelLoader(AjaxModelLoader):
def get_list(self, term, offset=0, limit=DEFAULT_PAGE_SIZE): def get_list(self, term, offset=0, limit=DEFAULT_PAGE_SIZE):
query = self.model.select() query = self.model.select()
stmt = None if len(term) > 0:
for field in self._cached_fields: stmt = None
q = field ** (u'%%%s%%' % term) for field in self._cached_fields:
q = field ** (u'%%%s%%' % term)
if stmt is None: if stmt is None:
stmt = q stmt = q
else: else:
stmt |= q stmt |= q
query = query.where(stmt) query = query.where(stmt)
if offset: if offset:
query = query.offset(offset) query = query.offset(offset)
......
...@@ -671,7 +671,9 @@ class BaseModelView(BaseView, ActionsMixin): ...@@ -671,7 +671,9 @@ class BaseModelView(BaseView, ActionsMixin):
form_ajax_refs = { form_ajax_refs = {
'user': { 'user': {
'fields': ('first_name', 'last_name', 'email'), 'fields': ('first_name', 'last_name', 'email'),
'page_size': 10 'placeholder': 'Please select',
'page_size': 10,
'minimum_input_length': 0,
} }
} }
......
...@@ -61,6 +61,9 @@ class AjaxSelect2Widget(object): ...@@ -61,6 +61,9 @@ class AjaxSelect2Widget(object):
placeholder = field.loader.options.get('placeholder', gettext('Please select model')) placeholder = field.loader.options.get('placeholder', gettext('Please select model'))
kwargs.setdefault('data-placeholder', placeholder) kwargs.setdefault('data-placeholder', placeholder)
minimum_input_length = int(field.loader.options.get('minimum_input_length', 1))
kwargs.setdefault('data-minimum-input-length', minimum_input_length)
return HTMLString('<input %s>' % html_params(name=field.name, **kwargs)) return HTMLString('<input %s>' % html_params(name=field.name, **kwargs))
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
var opts = { var opts = {
width: 'resolve', width: 'resolve',
minimumInputLength: 1, minimumInputLength: $el.attr('data-minimum-input-length'),
placeholder: 'data-placeholder', placeholder: 'data-placeholder',
ajax: { ajax: {
url: $el.attr('data-url'), url: $el.attr('data-url'),
...@@ -305,6 +305,8 @@ ...@@ -305,6 +305,8 @@
if ($el.attr('data-allow-blank')) if ($el.attr('data-allow-blank'))
opts['allowClear'] = true; opts['allowClear'] = true;
opts['minimumInputLength'] = $el.attr('data-minimum-input-length');
if ($el.attr('data-tags')) { if ($el.attr('data-tags')) {
$.extend(opts, { $.extend(opts, {
tokenSeparators: [','], tokenSeparators: [','],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment