Render paths in parent autocomplete widget
This commit is contained in:
parent
60f60bd1f5
commit
6d365b8716
|
@ -10,6 +10,13 @@ class ItemSelectWidget(ModelSelect2Widget):
|
|||
'description__icontains'
|
||||
]
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
kwargs['data_view'] = 'item-complete'
|
||||
super(ItemSelectWidget, self).__init__(*args, **kwargs)
|
||||
|
||||
def label_from_instance(self, obj):
|
||||
return obj.name
|
||||
|
||||
class ItemForm(forms.ModelForm):
|
||||
name = forms.CharField(widget=forms.TextInput())
|
||||
|
||||
|
|
|
@ -11,3 +11,25 @@
|
|||
<input type="submit" value="Save" class="hidden" name="_addanother" />
|
||||
{{ block.super }}
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}{{ block.super }}
|
||||
<script>
|
||||
$(function() {
|
||||
function fmt (state) {
|
||||
if (!state.id) {
|
||||
return state.text;
|
||||
}
|
||||
var result = $('<div><div><small></small></div><b></b></div>');
|
||||
result.find('small').text(state.path.join(' → ')).css({
|
||||
'opacity': 0.6,
|
||||
'letter-spacing': -0.5
|
||||
})
|
||||
result.find('b').text(state.text)
|
||||
return result;
|
||||
};
|
||||
$('.django-select2[name=parent]').djangoSelect2({
|
||||
templateResult: fmt,
|
||||
});
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
from django.conf.urls import include, url
|
||||
from storage.views import index, search, item_display, label_lookup
|
||||
from storage.views import index, search, item_display, label_lookup, ItemSelectView
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^$', index),
|
||||
url(r'^search$', search),
|
||||
url(r'^item/(?P<pk>.*)$', item_display, name='item-display'),
|
||||
url(r'^autocomplete.json$', ItemSelectView.as_view(), name='item-complete'),
|
||||
url(r'^(?P<pk>[^/]*)$', label_lookup, name='label-lookup'),
|
||||
]
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
from django.shortcuts import render, get_object_or_404, redirect
|
||||
from storage.models import Item, Label
|
||||
from django.contrib.postgres.search import SearchVector
|
||||
from django_select2.views import AutoResponseView
|
||||
from django.http import Http404, JsonResponse
|
||||
import shlex
|
||||
|
||||
def apply_smart_search(query, objects):
|
||||
|
@ -69,3 +71,21 @@ def item_display(request, pk):
|
|||
def label_lookup(request, pk):
|
||||
label = get_object_or_404(Label, pk=pk)
|
||||
return redirect(label.item)
|
||||
|
||||
class ItemSelectView(AutoResponseView):
|
||||
def get(self, request, *args, **kwargs):
|
||||
self.widget = self.get_widget_or_404()
|
||||
self.term = kwargs.get('term', request.GET.get('term', ''))
|
||||
self.object_list = self.get_queryset()
|
||||
context = self.get_context_data()
|
||||
return JsonResponse({
|
||||
'results': [
|
||||
{
|
||||
'text': obj.name,
|
||||
'path': [o.name for o in obj.get_ancestors()],
|
||||
'id': obj.pk,
|
||||
}
|
||||
for obj in context['object_list']
|
||||
],
|
||||
'more': context['page_obj'].has_next()
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue