Skip to content

Commit 1a0deb2

Browse files
committed
add change_list.html && update search/filter logic
1 parent bdfd3f3 commit 1a0deb2

File tree

4 files changed

+214
-0
lines changed

4 files changed

+214
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
{% extends "admin/base_site.html" %}
2+
{% load i18n admin_urls static admin_list %}
3+
4+
{% block extrastyle %}
5+
{{ block.super }}
6+
<link rel="stylesheet" type="text/css" href="{% static "admin/css/changelists.css" %}">
7+
{% if cl.formset %}
8+
<link rel="stylesheet" type="text/css" href="{% static "admin/css/forms.css" %}">
9+
{% endif %}
10+
{% if cl.formset or action_form %}
11+
<script type="text/javascript" src="{% url 'admin:jsi18n' %}"></script>
12+
{% endif %}
13+
{{ media.css }}
14+
{% if not actions_on_top and not actions_on_bottom %}
15+
<style>
16+
#changelist table thead th:first-child {width: inherit}
17+
</style>
18+
{% endif %}
19+
{% endblock %}
20+
21+
{% block extrahead %}
22+
{{ block.super }}
23+
{{ media.js }}
24+
{% endblock %}
25+
26+
{% block bodyclass %}{{ block.super }} app-{{ opts.app_label }} model-{{ opts.model_name }} change-list{% endblock %}
27+
28+
{% if not is_popup %}
29+
{% block breadcrumbs %}
30+
<ol class="breadcrumb">
31+
<li><a href="{% url 'admin:index' %}"><i class="fa fa-dashboard"></i> {% trans 'Home' %}</a></li>
32+
<li><a href="javascript:void(0)">{{ cl.opts.app_config.verbose_name }}</a></li>
33+
<li class="active">{{ cl.opts.verbose_name_plural|capfirst }}</li>
34+
</ol>
35+
{% endblock %}
36+
{% endif %}
37+
38+
{% block coltype %}flex{% endblock %}
39+
40+
{% block content %}
41+
<div id="content-main">
42+
{% block object-tools %}
43+
<ul class="object-tools">
44+
{% block object-tools-items %}
45+
{% change_list_object_tools %}
46+
{% endblock %}
47+
</ul>
48+
{% endblock %}
49+
{% if cl.formset and cl.formset.errors %}
50+
<p class="errornote">
51+
{% if cl.formset.total_error_count == 1 %}{% trans "Please correct the error below." %}{% else %}{% trans "Please correct the errors below." %}{% endif %}
52+
</p>
53+
{{ cl.formset.non_form_errors }}
54+
{% endif %}
55+
<div class="module{% if cl.has_filters %} filtered{% endif %}" id="changelist">
56+
<div class="row">
57+
{% block date_hierarchy %}{% if cl.date_hierarchy %}{% date_hierarchy cl %}{% endif %}{% endblock %}
58+
59+
{% block search %}{% search_form cl %}{% endblock %}
60+
61+
</div>
62+
63+
<form id="changelist-form" method="post"{% if cl.formset and cl.formset.is_multipart %} enctype="multipart/form-data"{% endif %} novalidate>{% csrf_token %}
64+
{% if cl.formset %}
65+
<div>{{ cl.formset.management_form }}</div>
66+
{% endif %}
67+
68+
{% block result_list %}
69+
{% if action_form and actions_on_top and cl.show_admin_actions %}{% admin_actions %}{% endif %}
70+
{% result_list cl %}
71+
{% if action_form and actions_on_bottom and cl.show_admin_actions %}{% admin_actions %}{% endif %}
72+
{% endblock %}
73+
{% block pagination %}{% pagination cl %}{% endblock %}
74+
</form>
75+
</div>
76+
</div>
77+
{% endblock %}
78+
79+
{% block custom_js %}
80+
81+
<script>
82+
var selectors = document.querySelector('#changelist-search').querySelectorAll('select');
83+
for (var i in selectors) {
84+
selectors[i].onchange = function() {
85+
var $field = $(this);
86+
var $option = $field.find('option:selected');
87+
var select_name = $option.data('name');
88+
console.log(select_name);
89+
if (select_name) {
90+
$field.attr('name', select_name);
91+
} else {
92+
$field.removeAttr('name');
93+
}
94+
// Handle additional values for date filters
95+
// var additional = $option.data('additional');
96+
// if (additional) {
97+
// var hidden_id = $field.data('name') + '_add';
98+
// var $hidden = $('#' + hidden_id);
99+
// if (!$hidden.length) {
100+
// $hidden = $('<input/>').attr('type', 'hidden').attr('id', hidden_id);
101+
// $field.after($hidden);
102+
// }
103+
// additional = additional.split('=');
104+
// $hidden.attr('name', additional[0]).val(additional[1])
105+
// }
106+
107+
// remove the hidden input of select_name
108+
var input = document.querySelector('#search_group').querySelector('input[name="' + select_name + '"]');
109+
input.parentNode.removeChild(input)
110+
}
111+
}
112+
113+
</script>
114+
115+
{% endblock %}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{% load i18n %}
2+
3+
<div class="col-md-1">
4+
<div class="form-group">
5+
<select class="form-control select2 select2-hidden-accessible" style="width: 100%;" tabindex="-1" aria-hidden="true">
6+
<option value="">{{ title }}</option>
7+
<option value="">------</option>
8+
{{new_choices}}
9+
{% for choice in new_choices %}
10+
<option data-name="{{ choice.name }}" value="{{ choice.value }}" {% if choice.selected %} selected {% endif %}>{{ choice.display }}</option>
11+
{% endfor %}
12+
</select>
13+
</div>
14+
</div>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{% load i18n static admin_list adminlte_list %}
2+
3+
<!--将filter和search合并到一个form中进行处理-->
4+
5+
<div id="toolbar">
6+
<form id="changelist-search" method="get">
7+
{% block filters %}
8+
{% if cl.has_filters %}
9+
{% for spec in cl.filter_specs %}{% adminlte_admin_list_filter cl spec %}{% endfor %}
10+
{% endif %}
11+
{% endblock %}
12+
13+
{% if cl.search_fields %}
14+
15+
<div class="col-md-2"><!-- DIV needed for valid HTML -->
16+
<div class="form-group">
17+
<input class="form-control" type="text" size="40" name="{{ search_var }}" value="{{ cl.query }}" id="searchbar" autofocus>
18+
</div>
19+
</div>
20+
<div class="col-md-2">
21+
<div class="form-group" id="search_group">
22+
<button type="submit" class="btn btn-primary">{% trans 'Search' %}</button>
23+
{% if show_result_count %}
24+
<span class="small quiet">{% blocktrans count counter=cl.result_count %}{{ counter }} result{% plural %}{{ counter }} results{% endblocktrans %} (<a href="?{% if cl.is_popup %}_popup=1{% endif %}">{% if cl.show_full_result_count %}{% blocktrans with full_result_count=cl.full_result_count %}{{ full_result_count }} total{% endblocktrans %}{% else %}{% trans "Show all" %}{% endif %}</a>)</span>
25+
{% endif %}
26+
{% for pair in cl.params.items %}
27+
{% if pair.0 != search_var %}<input type="hidden" name="{{ pair.0 }}" value="{{ pair.1 }}">{% endif %}
28+
{% endfor %}
29+
</div>
30+
</div>
31+
</form>
32+
</div>
33+
{% endif %}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from django.template import Library
2+
from django.template.loader import get_template
3+
import urllib.parse
4+
5+
register = Library()
6+
7+
8+
@register.simple_tag
9+
def adminlte_admin_list_filter(cl, spec):
10+
tpl = get_template(spec.template)
11+
new_choice = []
12+
13+
print(spec.lookup_kwarg)
14+
print(list(spec.choices(cl)))
15+
16+
for choice in list(spec.choices(cl)):
17+
qs = (urllib.parse.parse_qs(choice.get('query_string')))
18+
# print(qs)
19+
for k, v in qs.items():
20+
if k.strip('?') in cl.params.keys() and k.strip(
21+
'?') != spec.lookup_kwarg:
22+
continue
23+
new_choice.append({'name': k.strip('?'), 'value': v[0],
24+
'display': choice.get('display'),
25+
'selected': choice.get('selected')})
26+
27+
print(new_choice)
28+
29+
return tpl.render({
30+
'title': spec.title,
31+
'choices': list(spec.choices(cl)),
32+
'new_choices': new_choice,
33+
'spec': spec,
34+
})
35+
36+
# def adminlte_search_form(cl):
37+
# """
38+
# Display a search form for searching the list.
39+
# """
40+
# print(cl)
41+
# return {
42+
# 'cl': cl,
43+
# 'show_result_count': cl.result_count != cl.full_result_count,
44+
# 'search_var': SEARCH_VAR
45+
# }
46+
#
47+
#
48+
# @register.tag(name='adminlte_search_form')
49+
# def adminlte_search_form_tag(parser, token):
50+
# return InclusionAdminNode(parser, token, func=adminlte_search_form,
51+
# template_name='search_form.html',
52+
# takes_context=False)

0 commit comments

Comments
 (0)