Chapter 6, Sorting and Search Function Development
6.1. Sorting function development
1)kingadmin_tags.py
@register.simple_tag def get_sorted_column(column,sorted_column,forloop): ''' Sort ''' if column in sorted_column: #If this column is sorted #To determine what order the last sorting was in, this time negate last_sort_index = sorted_column[column] if last_sort_index.startswith( ' - ' ): #Use slices, remove '-' this_time_sort_index = last_sort_index.strip( ' - ' ) else : #Add '-' this_time_sort_index = ' -%s'% last_sort_index return this_time_sort_index else: return forloop
2)kingadmin/views.py
def get_orderby_result(request,querysets,admin_class): '''排序''' current_ordered_column = {} #Get the index of the field to be sorted through the front end (it is a string) orderby_index = request.GET.get( ' _o ' ) if orderby_index: # Find the field to be sorted through the index, because the index may be negative It may also be negative, use the absolute value, otherwise other fields will be obtained when the negative value is orderby_key = admin_class.list_display[abs(int(orderby_index))] #Record the current ordered_column[orderby_key ] = orderby_index if orderby_index.startswith( ' - ' ): orderby_key = '-' + orderby_key return querysets.order_by(orderby_key),current_ordered_column else: return querysets,current_ordered_column
(3)table_obj_list.html
<th><a href="?_o={% get_sorted_column column sorted_column forloop.counter0 %}">{{ column }}</a></th>
(4) Add the icon of positive and reverse order
Boorstrap components: https://v3.bootcss.com/components/
Put the fonts static file of bootstrap under kingadmin/staic/fonts
5)kingadmin_tags.py
@register.simple_tag def render_sorted_arrow(column,sorted_column): ''' sorted icon ''' if column in sorted_column: last_sort_index = sorted_column[column] if last_sort_index.startswith('-'): arrow_direction = 'bottom' else: arrow_direction = 'top' ele = '''<span class="glyphicon glyphicon-triangle-%s" aria-hidden="true"></span>'''% arrow_direction return mark_safe(ele) return ''
(6)table_obj_list.html
<th><a href="?_o={% get_sorted_column column sorted_column forloop.counter0 %}"> {{ column }}{% render_sorted_arrow column sorted_column %} </a></th>
Effect:
6.2. Combination of paging, sorting and filtering
(1) Sort and filter combination
table_obj_list.html
2)kingamdin_tags.py
@register.simple_tag def render_filtered_args(admin_class): ''' Splicing filtered fields ''' if admin_class.filter_conditions: ele = '' for k,v in admin_class.filter_conditions.items(): ele += '&%s=%s'%(k,v) return mark_safe(ele) else: return ''
Now the combination of filtering and sorting is ok, but the pagination has not been combined
(3) Combination of filtering and paging
table_obj_list.html
kingadmin_tags.py
def render_paginator first add a parameter admin_class
(4) Paging, sorting, filtering combination
table_obj_list.py
kingadmin_tag.py
@register.simple_tag def get_current_sorted_column_index(sorted_column): # Ternary operation, if it is True, execute the left one, if it is False, execute the right one ('') return list(sorted_column.values())[0] if sorted_column else ''
table_obj_list.py
kingadmin_tag.py
Sorting, filtering and pagination combinations are now no problem
6.3. Search function development
Global search
(1)table_obj_list.html
2)kingadmin/views.py
from django.db.models import Q def get_searched_result(request,querysets,admin_class): '''搜索''' search_key = request.GET.get('_q') if search_key: q = Q() q.connector = 'OR' for search_field in admin_class.search_fields: q.children.append(("%s__contains"%search_field,search_key)) return querysets.filter(q) return querysets
What is now implemented is the global search function (can not be filtered and searched at the same time), add the function of filtering + search below
filter + search
Just add a hidden tag
kingadmin/vies.py
table_obj_list.html
Effect:
Function optimization
(1) The user does not know which fields to search through, so add a placeholder in the search box
<form action=""> <input type="search" placeholder="{% for s in admin_class.search_fields %}{{ s }},{% endfor %}" name="_q" value="{{ admin_class.search_key }}"> <input type="submit" value="Search"> {% for k,v in admin_class.filter_conditions.items %} <input type="hidden" name="{{ k }}" value="{{ v }}"> {% endfor %} </form>
(2) Add Bootstrap style
Filter field hints and beautification
table_obj_list.html
Filter button
kingadmin_tags.py
Filter field hints + beautify style for adding boxes
To be closed with </div> at the end