jquery tablesorter自定义订单
问题描述:
我在python上使用这个插件。jquery tablesorter自定义订单
是否可以通过自定义顺序对列进行排序?
在quality
列中,我只能有:'低','平庸','好','好',我希望以这种方式排列(或颠倒)。
在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',
只是“蓝色”,而不是“主题蓝色”。 它的作品,但也许我做错了什么。
答
使用叉子建议在我解决了这样的问题我的评论:
我script-jquery
:
$.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' } }
});
});
你想订购的服务器或客户端列? – Mottie
在客户端。 – fabio
您需要添加解析器 - [请参阅文档](http://tablesorter.com/docs/example-parsers.html)。 – Mottie