2016-07-27 99 views
1

我在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', 

只是「藍色」,而不是「主題藍色」。 它的作品,但也許我做錯了什麼。

+0

你想訂購的服務器或客戶端列? – Mottie

+0

在客戶端。 – fabio

+1

您需要添加解析器 - [請參閱文檔](http://tablesorter.com/docs/example-parsers.html)。 – Mottie

回答

1

使用叉子建議在我解決了這樣的問題我的評論:

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' } } 
    }); 

});