2016-03-03 112 views
1

我在jquery中用3列生成了一個表:數字,名稱,營業額。像這樣:如何在jquery中對錶中的單個列進行排序

$(".tab").append("<tr><td>+ index + "</td><td>+ name + "</td><td>+ turnover + "</td></tr>); 

我用「tablesorter」排序第3列。

$(".tab").tablesorter({ 
      // sort on the seconde column , desc asc 
       sortList: [[2,-1]] 
     }); 

的問題是,我想我的第一列「指數」並沒有改變,因爲它表明只是從1數到7,但是當我做的第3列我的第一列排序也將changes.I像一些想法來解決這個問題,謝謝

+0

http://stackoverflow.com/questions/7849558/sort-only-one-column-with-jquery-and-tablesorter –

+0

http://stackoverflow.com/questions/8942974/how-to-disable-sorting-on-column-in-jquery-tablesorter –

+0

http://stackoverflow.com/questions/4015320/jquery-tablesorter-plugin-disabling-sorting-on-some-columns –

回答

0

我掃描了文檔,它看起來並不像你本來可以做的那樣。但是,您似乎可以通過使用the triggers provided by the library來解決一個解決方案。我會做這樣的事情(在鏈路借用的例子一些代碼):

$(function(){ 

    // call the tablesorter plugin, the magic happens in the markup  
    $("table") 
    .tablesorter({ 
     theme: 'blue', 
     showProcessing : true 
    })  
    .bind("sortEnd",function(e, t){ 
     $("td.index-column").each(function(index){ 
      $(this).text(index + 1); 
     }); 
    });  
}); 

基本上,你要添加的事件處理程序時表完成的排序將觸發。此時,它遍歷每個具有「index-column」類的td元素(您在生成表時必須指定該類 - 這指的是索引列中的值)。然後它將該元素的文本設置爲循環的索引+1(因此在第一次迭代中,0,將第一個匹配元素的文本設置爲1等)。

相關問題