jQuery排序插件tableSorter

Tablesorter 是一个用来直接在浏览器上对表格数据进行排序的jQuery插件,无需再次刷新页面,支持多种单元格数据类型,例如数值、字符串、日期和自定义排序。

主要的特点包括:

  • 多列排序
  • 支持文本、URL地址、数值、IP地址、日期类型,以及自定义类型排序
  • 支持 TH 元素的 ROWSPAN 和 COLSPAN 属性
  • 支持第二个隐藏域排序
  • 可扩展外观
  • 程序简小,打包后只有 7.4K

效果:

 

 

用法:

1.先引两个js

<script type="text/javascript" src="../js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="../js/jquery.tablesorter.min.js"></script>

注意:表格要用thead/th和tbody。如:
<table id="myTable"
<thead> 
<tr> 
    <th>Last Name</th> 
    <th>First Name</th> 
    <th>Email</th> 
    <th>Due</th> 
    <th>Web Site</th> 
</tr> 
</thead> 
<tbody
<tr> 
    <td>Smith</td> 
    <td>John</td> 
    <td>jsmith@gmail.com</td> 
    <td>$50.00</td> 
    <td>http://www.jsmith.com</td> 
</tr> 
<tr> 
    <td>Bach</td> 
    <td>Frank</td> 
    <td>fbach@yahoo.com</td> 
    <td>$50.00</td> 
    <td>http://www.frank.com</td> 
</tr> 
</tbody>
<table>

3.调用排序js代码

<script type="text/javascript">
$(document).ready(function() {
   $("#mytable").tablesorter();
});
</script>

再回到页面点击第一行就可以进行排序了.

 

ps:

  1.一定要引jQuery包,所有jq插件都是基于jQuery包的

  2.如果想指定哪一栏不排序这样写

   $("#mytable").tablesorter({headers:{5:{sorter:false}}});

    第5列的sorter为false就OK了

 

 

/**
 Start by telling tablesorter to sort your table when the document is loaded:
*/
$(document).ready(function(){
 $("#myTable").tablesorter();
}); 
/**
 排序列表  [0,0][1,0] 按第一列,第二列进行升序排序  [列索引,排序方向] 0 asc 1 desc 
 Click on the headers and you'll see that your table is now sortable! You can also pass in configuration options when you initialize the table. This tells tablesorter to sort on the first and second column in ascending order. 
*/
$(document).ready(function(){
 $("#myTable").tablesorter( {sortList: [[0,0], [1,0]]} );
}); 
/**
 排序选项设置
*/
$(document).ready(function(){
 $("#myTable").tablesorter({widthFixed: true, widgets: ['zebra']});
});
/*
var myTextExtraction = function(node){
 // extract data from markup and return it      
 return node.childNodes[0].childNodes[0].innerHTML; 
} 
$(document).ready(function(){
 $("#myTable").tableSorter({textExtraction: myTextExtraction});
});
*/

/**
 禁止第二列.每三列进行排序
**/
$(document).ready(function(){
 $("myTable").tablesorter({
  // pass the headers argument and assing a object 
  headers: {
   // assign the secound column (we start counting zero)
   1: {
   // disable it by setting the property sorter to false
   sorter: false},
   // assign the third column (we start counting zero)
   2: {
   // disable it by setting the property sorter to false
   sorter: false}
  }
 });
});

/**
 使用TH更像一个按钮
$(document).ready(function(){
 $("#myTable").tableSorter(
  {cancelSelection:true}
 );
});
*/

/**
 强制某列排序后不能动,其它列根据该列进行排序
 Sort multiple columns simultaneously by holding down the shift key and clicking a second, third or even fourth column header!
*/
$(document).ready(function(){
 // call the tablesorter plugin/
 $("myTable").tablesorter({
  // set forced sort on the fourth column and i decending order. 
  sortForce: [[0,0]]}
 );
}); 

/**
 按键的更换
*/
$(document).ready(function(){
 // call the tablesorter plugin
 $("table").tablesorter({
  // change the multi sort key from the default shift to alt button
  sortMultiSortKey: 'altKey'
 });
}); 

/**
$(document).ready(function(){
 $("table").tablesorter();
 $("#ajax-append").click(function() {
  $.get("assets/ajax-content.html",function(html) {
   // append the "ajax'd" data to the table body
   $("table tbody").append(html);
   // let the plugin know that we made a update
   $("table").trigger("update");
   // set sorting column and direction, this will sort on the first and third column
   var sorting = [[2,1],[0,0]];
   // sort on the first column 
   $("table").trigger("sorton",[sorting]);
  });
  return false;
 }); 
});
**/

$(document).ready(function(){
 $("myTable").tablesorter({widthFixed: true, widgets: ['zebra']})
  .tablesorterPager({container: $("#pager")});
});

转载于:https://www.cnblogs.com/ishibin/archive/2011/10/13/2209950.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值