7-crm專案-kingadmin,列表頁---排序
阿新 • • 發佈:2020-08-10
展示客戶列表頁面--------排序
@register.simple_tag def get_sorted_column(column,sorted_column,forloop): '''排序''' if column in sorted_column: #如果這一列被排序了 #要判斷上一次排序是按什麼順序,本次取反 last_sort_index = sorted_column[column] if last_sort_index.startswith('-'): #利用切片,去掉‘-’ this_time_sort_index = last_sort_index.strip('-') else: #加上 '-' this_time_sort_index = '-%s'% last_sort_index return this_time_sort_index else: return forloop
views
def get_orderby_result(request,querysets,admin_class): '''排序''' current_ordered_column = {} #通過前端獲取到要排序的欄位的索引(是個字串) orderby_index = request.GET.get('_o') if orderby_index: #通過索引找到要排序的欄位,因為索引有可能是負數也有可能是負數,要用絕對值,否則負值的時候取到了其它欄位了 orderby_key = admin_class.list_display[abs(int(orderby_index))] #記錄下當前是按什麼排序欄位的 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