From: Scott Baker Date: Wed, 6 Aug 2014 01:33:31 +0000 (-0700) Subject: filtering of select in sliverinline now working X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=34b502f51d2fc727249a1185f3a586311a8fdea5;p=plstackapi.git filtering of select in sliverinline now working --- diff --git a/planetstack/core/admin.py b/planetstack/core/admin.py index f8e6dac..86b2b3c 100644 --- a/planetstack/core/admin.py +++ b/planetstack/core/admin.py @@ -234,6 +234,13 @@ class SliverInline(PlStackTabularInline): def formfield_for_foreignkey(self, db_field, request=None, **kwargs): if db_field.name == 'deploymentNetwork': kwargs['queryset'] = Deployment.select_by_acl(request.user) + # the inscrutable jquery selector below says: + # find the closest parent "tr" to the current element + # then find the child with class "field-node" + # then find the child with that is a select + # then return its id + kwargs['widget'] = forms.Select(attrs={'onChange': "update_nodes(this, $($($(this).closest('tr')[0]).children('.field-node')[0]).children('select')[0].id);"}) + #kwargs['widget'] = forms.Select(attrs={'onChange': "console.log($($($(this).closest('tr')[0]).children('.field-node')[0]).children('select')[0].id);"}) field = super(SliverInline, self).formfield_for_foreignkey(db_field, request, **kwargs) diff --git a/planetstack/templates/admin/core/slice/change_form.html b/planetstack/templates/admin/core/slice/change_form.html index 035b730..c94b580 100644 --- a/planetstack/templates/admin/core/slice/change_form.html +++ b/planetstack/templates/admin/core/slice/change_form.html @@ -7,6 +7,20 @@ deployment_nodes = [ [{{ dn.0 }}, {{ dn.1 }} , "{{ dn.2 }}"], {% endfor %} ]; + +function update_nodes(deployment_select, node_select_id) { + deployment_id = $(deployment_select).val(); + html = "\n"; + for (i in deployment_nodes) { + dn = deployment_nodes[i]; + if (dn[0] == deployment_id) { + html = html + '\n' + } + } + //console.log(html); + $("#"+node_select_id).empty().append(html); +} + {% endblock %}