我使用Django创建了一个Employee管理系统。我已经在其中完成了一个过滤方法,它基于从下拉菜单和文本输入中选择的选项。过滤工作正常。在第一页上,它给出了整个员工列表,可以按升序和降序显示。在同一页面上给出了过滤方法。过滤后的数据显示在另一页中。现在我想在过滤后的数据页面上给出一个按钮,单击该按钮会以升序/降序显示数据。我已经为完整的员工列表在视图中编写了一个单独的升序和降序函数。它如何用于此功能。我会在这里粘贴我的代码。请帮我找一个解决方案,因为我是django编程的新手 我已经给出了2个单独的图像用于升序和降序。我想这样:点击1个图像列表按升序排列;并单击其他图像按降序列出。
过滤()
def filter(request):
val3=''
if request.GET.has_key('choices'):
val2=request.GET.get('choices')
if request.GET.has_key('textField'):
val3=request.GET.get('textField')
if request.POST:
val2=request.POST.get('choices')
val3=request.POST.get('textField')
if val2=='Designation':
newData = EmployeeDetails.objects.filter(designation=val3)
flag=True
elif val2=='Name':
newData = EmployeeDetails.objects.filter(userName__icontains=val3)
flag=True
elif val2=='EmployeeID':
newData = EmployeeDetails.objects.filter(employeeID=val3)
flag=True
elif val2=='Project':
newData = EmployeeDetails.objects.filter(project=val3)
flag=True
elif val2=='DateOfJoin':
newData = EmployeeDetails.objects.filter(dateOfJoin=val3)
flag=True
else:
return HttpResponseRedirect('/employeeList/')
#tableList = EmployeeDetails.objects.all()
paginator = Paginator(newData, 10)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
contacts = paginator.page(page)
except (EmptyPage, InvalidPage):
contacts = paginator.page(0)
return render_to_response('filter.html',{'newData':newData,'emp_list': contacts,'val2':val2,'val3':val3,'flag':flag})
filter.html
<div>
Employees List
<a STYLE="text-decoration:none" align=center href="http://10.1.0.90:8080/sortAscend/ "> <img src="/static/sort_asc.gif " border="1" height="12" /> </a>
<h4 align="left">
{%for data in newData%}
<a STYLE="text-decoration:none" href ="http://10.1.0.90:8080/singleEmployee/{{data.id}}?choices={{val2}}&textField={{val3}}&flag=1 ">
{{ data.userName}}<br>
{%endfor%}
</h4>
</div>
升序和降序功能
def sortAscend(request):
tableList = EmployeeDetails.objects.all().order_by('userName')
paginator = Paginator(tableList, 12)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
contacts = paginator.page(page)
except (EmptyPage, InvalidPage):
contacts = paginator.page(0)
return render_to_response('sortAscend.html', {'emp_list': contacts})
#Method for listing the employees in descending order
def sortDescend(request):
tableList = EmployeeDetails.objects.all().order_by('-userName')
paginator = Paginator(tableList, 12)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
contacts = paginator.page(page)
except (EmptyPage, InvalidPage):
contacts = paginator.page(0)
return render_to_response('sortDescend.html', {'emp_list': contacts})
sortAscending.html
{%for emp in emp_list.object_list%}
<tr> <td><a STYLE="text-decoration:none" href ="http://10.1.0.90:8080/singleEmployee/{{emp.id}} "> {{ emp.userName }} </a></td> </tr><td>
{%endfor%}
答案 0 :(得分:1)
我不确定我是否得到了这个问题,但是如果你想将排序应用于过滤的对象,你可能想要添加某种缓存(不同于django的内置缓存),它存储过滤的查询集和排序那。或者您可以使用django的会话管理传递过滤器选项并重做排序查询。这将需要重构过滤器,以便if / elif链独立于该视图并返回过滤的查询集。
例如:
def filterHandler(request):
val3=''
if request.GET.has_key('choices'):
val2=request.GET.get('choices')
if request.GET.has_key('textField'):
val3=request.GET.get('textField')
if request.POST:
val2=request.POST.get('choices')
val3=request.POST.get('textField')
newData , flag = filter(val2, val3)
if newData is None:
return HttpResponseRedirect('/employeeList/')
#tableList = EmployeeDetails.objects.all()
paginator = Paginator(newData, 10)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
try:
contacts = paginator.page(page)
except (EmptyPage, InvalidPage):
contacts = paginator.page(0)
request.session['val2'] = val2
request.session['val3'] = val3
return render_to_response('filter.html',{'newData':newData,'emp_list': contacts,'val2':val2,'val3':val3,'flag':flag})
def filter(val2, val3):
newData = None
flag = False
if val2=='Designation':
newData = EmployeeDetails.objects.filter(designation=val3)
flag=True
elif val2=='Name':
newData = EmployeeDetails.objects.filter(userName__icontains=val3)
flag=True
elif val2=='EmployeeID':
newData = EmployeeDetails.objects.filter(employeeID=val3)
flag=True
elif val2=='Project':
newData = EmployeeDetails.objects.filter(project=val3)
flag=True
elif val2=='DateOfJoin':
newData = EmployeeDetails.objects.filter(dateOfJoin=val3)
flag=True
return newData, flag
现在,您的排序方法可以获取最初传递的过滤器值。您也可以选择将它们作为获取参数添加到视图网址。
答案 1 :(得分:1)
在视图级别处理排序的另一种方法是在模板上执行此操作。因此,您可能需要签出jquery tablesorter(因为您在显示中也使用了表格)。它按升序/降序处理排序。
因此,如果过滤后的结果已准备好显示在页面上,比如filtered_results.html,则可以这样做。
<!-- filtered_results.html -->
<head>
...
<script type="text/javascript" src="/path/to/jquery-latest.js"></script>
<script type="text/javascript" src="/path/to/jquery.tablesorter.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#myTable").tablesorter();
});
</script>
</head>
<body>
<table id="myTable">
<thead><tr><th>Some-Label</th></tr></thead>
<tbody>
{% for emp in emp_list.object_list %}
<tr><td><a href="http://10.1.0.90:8080/singleEmployee/{{emp.id}}">{{emp.userName}}</a></td></tr>
{% endfor %}
</tbody>
</table>
</body>
单击“Some-Label”单元格将按升序/降序切换排序。
此外,它还有一个用于处理分页的插件。查看此link了解演示。