Added autocomplete view and template.
This commit is contained in:
parent
81b181f128
commit
094e77d06a
44
rattail/pyramid/templates/autocomplete.mako
Normal file
44
rattail/pyramid/templates/autocomplete.mako
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
<%def name="autocomplete(field_name, service_url, field_value=None, field_display=None, width='300px', selected=None, cleared=None)">
|
||||||
|
<div id="${field_name}-container" class="autocomplete-container">
|
||||||
|
${h.hidden(field_name, id=field_name, value=field_value)}
|
||||||
|
${h.text(field_name+'-textbox', id=field_name+'-textbox', value=field_display,
|
||||||
|
class_='autocomplete-textbox', style='display: none;' if field_value else '')}
|
||||||
|
<div id="${field_name}-display" class="autocomplete-display"${'' if field_value else ' style="display: none;"'|n}>
|
||||||
|
<span>${field_display or ''}</span>
|
||||||
|
<button type="button" id="${field_name}-change" class="autocomplete-change">Change</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<script type="text/javascript">
|
||||||
|
$(function() {
|
||||||
|
$('#${field_name}-textbox').autocomplete({
|
||||||
|
source: '${service_url}',
|
||||||
|
autoFocus: true,
|
||||||
|
focus: function(event, ui) {
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
select: function(event, ui) {
|
||||||
|
$('#${field_name}').val(ui.item.value);
|
||||||
|
$('#${field_name}-display span:first').text(ui.item.label);
|
||||||
|
$('#${field_name}-textbox').hide();
|
||||||
|
$('#${field_name}-display').show();
|
||||||
|
% if selected:
|
||||||
|
${selected}(ui.item.value, ui.item.label);
|
||||||
|
% endif
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
$('#${field_name}-change').click(function() {
|
||||||
|
$('#${field_name}').val('');
|
||||||
|
$('#${field_name}-display').hide();
|
||||||
|
with ($('#${field_name}-textbox')) {
|
||||||
|
val('');
|
||||||
|
show();
|
||||||
|
focus();
|
||||||
|
}
|
||||||
|
% if cleared:
|
||||||
|
${cleared}();
|
||||||
|
% endif
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
</%def>
|
|
@ -38,15 +38,15 @@ class AutocompleteView(View):
|
||||||
def filter_query(self, q):
|
def filter_query(self, q):
|
||||||
return q
|
return q
|
||||||
|
|
||||||
def make_query(self, query):
|
def make_query(self, term):
|
||||||
q = Session.query(self.mapped_class)
|
q = Session.query(self.mapped_class)
|
||||||
q = self.filter_query(q)
|
q = self.filter_query(q)
|
||||||
q = q.filter(getattr(self.mapped_class, self.fieldname).ilike('%%%s%%' % query))
|
q = q.filter(getattr(self.mapped_class, self.fieldname).ilike('%%%s%%' % term))
|
||||||
q = q.order_by(getattr(self.mapped_class, self.fieldname))
|
q = q.order_by(getattr(self.mapped_class, self.fieldname))
|
||||||
return q
|
return q
|
||||||
|
|
||||||
def query(self, query):
|
def query(self, term):
|
||||||
return self.make_query(query)
|
return self.make_query(term)
|
||||||
|
|
||||||
def display(self, instance):
|
def display(self, instance):
|
||||||
return getattr(instance, self.fieldname)
|
return getattr(instance, self.fieldname)
|
||||||
|
|
|
@ -27,9 +27,9 @@
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
from edbob.pyramid.views import (
|
from edbob.pyramid.views import SearchableAlchemyGridView, CrudView, AlchemyGridView
|
||||||
SearchableAlchemyGridView, CrudView, AlchemyGridView, AutocompleteView)
|
|
||||||
|
|
||||||
|
from rattail.pyramid.views import AutocompleteView
|
||||||
from rattail.db.model import Department, Product, ProductCost, Vendor
|
from rattail.db.model import Department, Product, ProductCost, Vendor
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue