2016-03-03 160 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等)。