jquery表排序器自定义顺序
我在python上使用这个插件 是否可以按自定义顺序对列进行排序 在jquery表排序器自定义顺序,jquery,python,tablesorter,Jquery,Python,Tablesorter,我在python上使用这个插件 是否可以按自定义顺序对列进行排序 在quality列中,我只能有:“low”、“mediocre”、“good”、“great”,我希望以这种方式排序(或反向排序) 在Name列中,我(根据视图)有一个客户订单,但我想提供按字母顺序排序的可能性,然后返回原始订单 我的视图.py: def aff_list(request): context_dict = {} lista_aff_a=[] lista_aff_s=[] for af
quality
列中,我只能有:“low”、“mediocre”、“good”、“great”,我希望以这种方式排序(或反向排序)
在Name
列中,我(根据视图)有一个客户订单,但我想提供按字母顺序排序的可能性,然后返回原始订单
我的视图.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>
编辑:
最后一个问题:
我下载文件并在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" %}">
只是“蓝色”,而不是“主题蓝色”。
这是可行的,但也许我做错了什么。使用评论中建议的叉子,我用以下方式解决了我的问题: 我的
脚本jquery
:
$(document).ready(function() {
$("#lista_aff").tablesorter();
});
$.tablesorter.addParser({
id: 'quality',
is: function(s) {
// return false so this parser is not auto detected
return false;
},
format: function(s) {
// format your data for normalization
return s.toLowerCase().replace(/great/,0).replace(/good/,1
).replace(/mediocre/,2).replace(/low/,3);
},
// set type, either numeric or text
type: 'numeric'
});
$(document).ready(function() {
$("#lista_Aff").tablesorter({
theme : 'blue',
sortReset : true,
headers: { 1: { sorter: 'quality' } }
});
});
您是想在服务器端还是客户端对列进行排序?您需要添加一个解析器-。谢谢。至于“姓名”一栏呢?是否可以“重置”顺序并查看初始顺序?原始版本的tablesorter没有内置重置方法。您可以查看我的,其中有一个将在第三次单击时重置排序。很好,谢谢!我解决了问题,但我用另一个疑问编辑了我的问题(我保证很少)。如果你愿意,你可以写一个答案,我会接受的。感谢你的工作!
$.tablesorter.addParser({
id: 'quality',
is: function(s) {
// return false so this parser is not auto detected
return false;
},
format: function(s) {
// format your data for normalization
return s.toLowerCase().replace(/great/,0).replace(/good/,1
).replace(/mediocre/,2).replace(/low/,3);
},
// set type, either numeric or text
type: 'numeric'
});
$(document).ready(function() {
$("#lista_Aff").tablesorter({
theme : 'blue',
sortReset : true,
headers: { 1: { sorter: 'quality' } }
});
});