2011-04-15 24 views
0

我用的tablesorter排序表,但不是所有列,我想忽略與定義的類名的特定列,如:如何在tablesorter中忽略具有特定類名的th?

<table class="basicList" cellpadding="0" cellspacing="0"> 
        <thead> 
         <tr> 
          <th class="ck_field"><input type="checkbox" id="check_all" name="check_all" /></th> 
          <th class="col_filter"><a href="#" class="btn_filter">&nbsp;</a></th> 
          <th>Name <span class="sort_indicator">&nbsp;</span></th> 
         </tr> 
         <tr class="filter_row"> 
          <td>&nbsp;</td> 
          <td>&nbsp;</td> 
          <td> <input type="text" id="the_name" name="name" class="filter_field"/></td> 
         </tr> 
        </thead> 
       </table> 

在這個例子中,我不想排序前兩列。

而且我想:

$(document).ready(function() { 

    console.log($(".basicList .col_filter").index()); 
    console.log($(".basicList .ck_field").index()); 

    var ck_ignore = $(".basicList .ck_field").index(); 
    var filter_ignore = $(".basicList .col_filter").index(); 

    $(".basicList").tablesorter({ widgets: ['zebra'],headers: { 
      //disable the first checkbox cell    
      $ck_ignore: {     
       sorter: false 
      }, 
      $filter_ignore : {     
       sorter: false 
      } 
     } 
    }); 

不工作,如何解決這個問題呢?

+0

爲什麼你'$'在變量名的前面? – Khez 2011-04-15 06:34:18

+0

@Khez:通常用於表示它們是jQuery對象,儘管在這種情況下,它看起來像是用作字典鍵......這很奇怪。 – mpen 2011-04-15 06:53:35

+0

@Mark,我知道$表示什麼,但他只是創建了2個有問題的變量,他們沒有'$'。 – Khez 2011-04-15 06:55:31

回答

3

使用方法如下:

var myHeaders = {}; 
myHeaders[ck_ignore] = { sorter: false }; 
myHeaders[filter_ignore] = { sorter: false }; 

$(".basicList").tablesorter({ widgets: ['zebra'], headers: myHeaders }); 

注意:如果你有這些類的更多列,只會對第一個出現次數。

+0

選擇此答案JT! – Calum 2011-04-15 06:59:26

+0

是的,那麼它的工作原理!謝謝! – 2011-04-15 07:05:54

0

只需使用頭參數:

$(document).ready(function() { 

    console.log($(".basicList .col_filter").index()); 
    console.log($(".basicList .ck_field").index()); 

    var ck_ignore = $(".basicList .ck_field").index(); 
    var filter_ignore = $(".basicList .col_filter").index(); 

    $(".basicList").tablesorter({ widgets: ['zebra'],headers: {  
      // pass the headers argument and assing a object 
     headers: { 
      // assign the first column (we start counting zero) 
      0: { 
       // disable it by setting the property sorter to false 
       sorter: false 
      } 
     } 
     , 
      $filter_ignore : {     
       sorter: false 
      } 
     } 
    }); 
1

遇到同樣的問題,這是我想出了。假設你th類不排序被稱爲nosort

function setupTablesorter() { 
    $('table.tablesorter').each(function (i, e) { 
     var myHeaders = {} 
     $(this).find('th.nosort').each(function (i, e) { 
      myHeaders[$(this).index()] = { sorter: false }; 
     }); 

     $(this).tablesorter({ widgets: ['zebra'], headers: myHeaders }); 
    });  
} 

這已超過我看到這裏的其他答案的幾個優點。首先,即使您有多個不可排序的列,它也可以工作。其次,如果在同一頁面上有多個表格,每個表格都具有不同的可排序列,它也可以工作。

1

按照該documentation,你現在可以使用class="sorter-false"

相關問題