使用 JQuery 按 id 隐藏表列

发布于 2024-10-08 23:20:59 字数 413 浏览 0 评论 0原文

我需要使用 JQuery 隐藏/显示表列。找到答案并不难:经过一些研究,我发现了一些看起来像这样的东西:

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

效果很好,但有一个限制:你必须给出列号。
就我而言,事情变得更棘手:显示的列取决于您是谁(即简单用户:您只需看到第 1、3 和 5 列。Admid 用户:您可以看到所有列)
这是使用 php 规则设置的,以提高安全性。因此,我最终得到的“文章”列在管理员时为数字“3”,在用户时为数字“2”。提到的解决方案

=>我需要一个解决方案来使用列 ID 而不是列号。我环顾四周,但找不到答案。有人有主意吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对你的占有欲 2024-10-15 23:20:59

您可以使用 id 来获取特定列的索引。

像这样的东西

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

将该索引与您已经找到的内容整合起来。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文