一个表中多行的jQuery表数据排序器插件

时间:2012-11-09 06:32:50

标签: jquery tablesorter

我有一张桌子有两个桌子标题< th>。我需要使用jquery表排序对两个不同的集进行排序。

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="listtable">
 <thead>
  <tr>
   <th>Heading Set1 - A</th>
   <th>Heading Set1 - B</th>
   <th>Heading Set1 - C</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>&nbsp;</td>
   <td>&nbsp;</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
  <thead>
  <tr>
   <th>Heading Set2 - A</th>
   <th>Heading Set2 - B</th>
   <th>Heading Set2 - C</th>
  </tr>
  </thead>
 <tbody>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  </tbody>
  </table>

用于对单个TH进行排序的脚本如下:

$(function() 
{ 

  $(".listtable").tablesorter({ 

  sortList : [[1,0]], 

  widgets: ["zebra", "columns"], 

  widgetOptions: { 
    columns: [ "primary", "secondary", "tertiary" ], 
    columns_thead : true, 
    columns_tfoot : true 
 } 

 });

我能够对一组TH进行排序,但我需要在现有表中添加另一组TH和TBODY。我的脚本不允许在一个表中对两个集合进行排序。

2 个答案:

答案 0 :(得分:0)

试试这个pplugin,

http://datatables.net/release-datatables/examples/basic_init/multi_col_sort.html

DataTables允许您同时按多列排序。 DataTables还提供了一种方法来添加自己的排序函数,以扩展内置于DataTables中的函数。如果您希望对数据格式(如货币和非Javascript标准日期格式)进行排序(这种自然排序算法是一种流行的用法),这将非常有用。

答案 1 :(得分:0)

我找到了this reference,但我找不到W3C recommendations中的确切措辞。它表示每个表只允许一个thead和一个tfoot。多个tbody都可以。

因此,不要使用两个thead,而只需将其替换为tbody并添加类tablesorter-infoOnly,以便插件忽略内容。像这样:

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="listtable">
    <thead>
        <tr>
            <th>Heading Set1 - A</th>
            <th>Heading Set1 - B</th>
            <th>Heading Set1 - C</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
    </tbody>
    <tbody class="tablesorter-infoOnly">
        <tr>
            <th>Heading Set2 - A</th>
            <th>Heading Set2 - B</th>
            <th>Heading Set2 - C</th>
        </tr>
    </tbody>
    <tbody>
        <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
    </tbody>
</table>
相关问题