2011-10-12 73 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分叉。 –