【发布时间】:2016-07-29 05:23:37
【问题描述】:
我在 python 上使用这个插件。
是否可以按自定义顺序对列进行排序?
在quality 列中,我只能有:'low'、'mediocre'、'good'、'great',我想以这种方式(或相反)排序。
在Name 列中,我(按视图)有一个自定义顺序,但我也想提供按字母顺序排序的可能性,然后返回原始顺序...
我的views.py:
def aff_list(request):
context_dict = {}
lista_aff_a=[]
lista_aff_s=[]
for aff in Aff.objects.all():
if aff.price=='luxury':
lista_aff_a.append(aff)
elif aff.price=='cheap':
lista_aff_s.append(aff)
lista_aff=lista_aff_a + lista_aff_s #this way is ordered before lista_aff_a, then lista_aff_s
context_dict['lista_aff'] = lista_aff
return render(request, 'aff_list.html', context_dict)
我的aff_list.html:
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="{% static "js/jquery-tablesorter/jquery.tablesorter.js" %}"></script>
<link rel="stylesheet" href="{% static "css/tablesorter.css" %}" type="text/css" />
<script src="{% static "js/script-jquery.js" %}"></script>
...
<div class="panel panel-default">
<table id="lista_aff" class="tablesorter table table-hover table table-bordered table table-condensed">
<thead>
<tr>
<th>Name</th>
<th>Quality</th>
</tr>
</thead>
<tbody>
{% for aff in lista_aff %}
<tr>
<td>
{{ aff.name }}
</td>
<td>
{{ aff.quality }}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
我的script-jquery:
$(document).ready(function() {
$("#lista_aff").tablesorter();
});
编辑:
最后一个问题:
我在static/js下载文件并解压,然后在我的模板头部写:
<link href="{% static "js/tablesorter-master/css/theme-blue.css" %}" />
<link rel="stylesheet" href="{% static "css/dashboard.css" %}">
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="{% static "js/tablesorter-master/js/jquery.tablesorter.js" %}">
为了使它们起作用,我必须将主题的名称从 theme.# 更改为 theme-# 并添加我的 script-jquery.js:
theme : 'blue',
只是“蓝色”,而不是“主题蓝色”。 它有效,但也许我做错了什么。
【问题讨论】:
-
您是在服务器端还是客户端对列进行排序?
-
你需要添加一个解析器 - see documentation.
-
谢谢。对于“名称”列?是否可以“重置”订单并查看初始订单?
-
原始版本的tablesorter没有内置reset方法。您可以查看我的fork of tablesorter,它有一个
sortResetoption,它将在第三次点击时重置排序。 -
好的,谢谢!我解决了这些问题,但我用另一个疑问编辑了我的问题(我保证很少)。如果你愿意,你可以写一个答案,所以我会接受。恭喜你的工作!
标签: jquery python tablesorter