开发者

Hide table column by id with JQuery

I need to hide / show table columns with JQuery. Not so hard to find an answer : after a few researches, i found something that look like that :

$('#btnHide').click(function() {  
  $('td:nth-child(2),th:nth-child(2)').toggle();  
});

Works quite well, but has one constraint : you have to give开发者_开发百科 the column number.

In my case, things get taugher : the displayed columns depend on who you are (i.e. simple user : you juste get to see column 1, 3 and 5. Admid user : you see all columns)

This is set with php rules for more security. So I end up with my column "article" with the number "3" when admin, and "2" when user. The solution mentionned

=> I need a solution to use columns ids instead of column number. I've looked around a bit, but I couldn't find an answer. Does anyone have an idea ?

Thanks


You can use the id to obtain the index of a particular column.

Something like

var $col_header = $("th#article"),
    col_index = $("#someTable th").index($col_header[0]);

Integrate that index with what you've already found.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜