2011-10-12 43 views
2

我目前使用以下方法來告訴我的表不使用jQuery tablesorter插件排序某些列:的jQuery的tablesorter如果有不列進行排序類

$(".uiGridContent table").tablesorter({ 
     sortList: [[1, 0]], 
     headers: { 
      0: { sorter: false }, 
      5: { sorter: false }, 
      6: { sorter: false } 
     } 
    }); 

的問題是,在我的應用程序,用戶可以添加和刪​​除列,所以訂單可以改變,所以我目前的代碼不是一個可行的解決方案。如果我在列上放置課程,我不想排序<col class="nosort" />我怎麼能這樣做,所以它不排序這些列?

因爲我使用<col />我曾嘗試以下:

$filter_ignore = $("col.nosort").closest("th").index(); 

    $(".uiGridContent table").tablesorter({ 
     sortList: [[1, 0]], 
     headers: { 
      $filter_ignore: { 
       sorter: false 
      } 
     } 
    }); 

但不工作:/

我想我需要某種形式的循環,找到所有的日與一列爲他們與clase!這裏是我的表的例子:

<table> 
<colgroup> 
    <col class="nosort" /> 
    <col /> 
</colgroup> 
    <thead> 
    <tr> 
    <th scope="col">a</th> 
    <th scope="col">b</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr> 
    <td scope="col">a</td> 
    <td scope="col">b</td> 
    </tr> 
    </tbody> 
</table> 

感謝

+0

退房http://stackoverflow.com/questions/5672993/how-to-ignore-th-with-specific-class-name-in-tablesorter – realshadow

+0

嗨,試過(見OP),但不起作用:/ – Cameron

回答

0

據我所知<col class="nosort" />不會該類添加到其範圍的cols,我不知道怎麼去下COLGROUP或者它甚至有可能全部colls。但是,如果您想將類添加到th中,那麼很容易對其進行禁用排序。

var $filter_ignore = {}; 

$("th.nosort").each(function(i) { 
    $filter_ignore[i] = {sorter: false}; 
}); 

$("table").tablesorter({ 
    sortList: [[1, 0]], 
    headers: $filter_ignore 
}); 

有了這個標記

<table> 
<colgroup> 
    <col class="nosort" /> 
    <col /> 
</colgroup> 
    <thead> 
    <tr> 
    <th scope="col" class="nosort">a</th> 
    <th scope="col">b</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr> 
    <td scope="col">a</td> 
    <td scope="col">b</td> 
    </tr> 
    </tbody> 
</table> 
+0

這會導致錯誤:未捕獲的TypeError:無法調用未定義的方法'addClass' – Cameron

11
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"

+1

請注意這是Mottie的(非常優越)tablesorter分叉。 –

相關問題