jQuery 使用 img 作为复选框来显示/隐藏表格列

发布于 2024-10-19 17:04:05 字数 1386 浏览 1 评论 0原文

我需要为用户提供隐藏/显示表列的能力。使用前面的示例和我自己的调整,我使用了复选框来隐藏/显示列,但现在需要删除表单元素。

我对 jquery/javascript 很陌生,所以希望有人可以帮助提供有关如何操作的信息
1.加载时隐藏一些列(基于代码中的触发器)
2. 切换列时交换图像,为用户提供有关显示/隐藏哪些列的视觉反馈。

我有一个小提琴 http://jsfiddle.net/IllusionVK/DqQFP/14/ 具有以下代码其中:

以前使用过以下代码,

$("input:checkbox:not(:checked)").each(function() {
var column = "table ." + $(this).attr("name");
$(column).hide();
});

$("input:checkbox").click(function(){
var column = "table ." + $(this).attr("name");
$(column).toggle();
});
});

但必须删除表单元素(复选框),现在尝试使用 img 标签代替复选框:

$('img').click(function(){
var column = $(this).attr('id');
$('td:nth-child('+ column +')').toggle();
});

我希望最终的代码对于所有无需隐藏/显示列的人来说都是有用的将任何代码添加到实际表中并动态使用 nth-child 。

任何帮助将非常感激!干杯,查理。

到目前为止的帮助来源:
http://www.devcurry。 com/2009/07/hide-table-column-with-single-line-of.html
使用 nth-child,但是是硬编码的,并且仅为一列显示/隐藏而设计

通过使用 jQuery 选中复选框来自动隐藏表格列
但要求表因额外的类而负担过重=对我来说不好,使用复选框=也不好

I need to provide users with the ability to hide/show table columns. Using previous examples with my own adjustments i have used checkboxes to hide/show columns, but now need to remove form elements.

I'm very new to jquery/javascript so hoping someone can help with info on now how to
1. Hide some columns on load (based on trigger in code)
2. Swap images when column is toggled to give users visual feedback as to which columns are shown/hidden.

I have a fiddle at
http://jsfiddle.net/IllusionVK/DqQFP/14/ which has the following code in it:

Previously used following code

$("input:checkbox:not(:checked)").each(function() {
var column = "table ." + $(this).attr("name");
$(column).hide();
});

$("input:checkbox").click(function(){
var column = "table ." + $(this).attr("name");
$(column).toggle();
});
});

but had to remove form elements (checkboxes), and am now trying to use img tag in place of checkbox:

$('img').click(function(){
var column = $(this).attr('id');
$('td:nth-child('+ column +')').toggle();
});

I'm hoping that the final code will be useful for all to hide/show columns without needing to add any code to the actual tables and is using nth-child dynamically.

Any help would be very much appreciated!! Cheers, Charlie.

Sources of help so far:
http://www.devcurry.com/2009/07/hide-table-column-with-single-line-of.html
Uses nth-child, BUT is hardcoded, and designed for only one column show/hide

hide table columns automatically by checking a checkbox with jQuery
But requires that table is overburdened with extra classes=bad for me, use checkboxes=also bad

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

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

发布评论

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

评论(2

五里雾 2024-10-26 17:04:05

如果您使用 index() 如果您制定了约定,则应该能够动态显示/隐藏列您的标签文本与表标题标签相匹配。

$('span').click(function() {
    var t = $.trim($(this).text());
    var i = $(".headercell td:contains(" + t + ")").index() + 1;
    $('td:nth-child(' + i+ ')').toggle();
});

更新了 jsfiddle

附注,img 元素不能包含文本 text,因此我调整了列表以使用 span 标签。

更新
如果您想轻松切换图例项目,您可以执行类似的操作..

<div class="legend">
    <div>Name</div>
    <div>Sex</div>
    <div>Age</div>
</div>

以及以下CSS:

.legend div.unchecked
{
    background-image:url(http://jquery.bassistance.de/validate/demo/images/unchecked.gif);
}
.legend div
{
    background-repeat:no-repeat;
    background-image:url(http://jquery.bassistance.de/validate/demo/images/checked.gif);
    cursor:pointer;
    background-position:left center;
    padding-left:20px;
}

并且您的jquery可以切换类以更改背景图像

$('div.legend div').click(function() {
    $(this).toggleClass("unchecked");
    var t = $.trim($(this).text());
    var i = $(".headercell td:contains(" + t + ")").index() + 1;
    $('td:nth-child(' + i + ')').toggle();
});

如果您想在页面加载时隐藏列,只需添加一个取消选中的类即可您想要取消选中的图例 div。

<div class="legend">
    <div>Name</div>
    <div>Sex</div>
    <div class="uncheckThis">Age</div>
</div>

然后运行此脚本将触发取消选中哪些。

$('div.legend div.uncheckThis').click();

jsfiddle 上的示例。

If you use index() you should be able to dynamically show/hide columns if you make a convention that your text of your tags match the table header tags.

$('span').click(function() {
    var t = $.trim($(this).text());
    var i = $(".headercell td:contains(" + t + ")").index() + 1;
    $('td:nth-child(' + i+ ')').toggle();
});

Updated jsfiddle.

side note, a img element can't have text <img>text</img>, so I adjusted the list to use a span tag instead.

update
If you want to easily toggle the legend items you could do something like this..

<div class="legend">
    <div>Name</div>
    <div>Sex</div>
    <div>Age</div>
</div>

And the following css:

.legend div.unchecked
{
    background-image:url(http://jquery.bassistance.de/validate/demo/images/unchecked.gif);
}
.legend div
{
    background-repeat:no-repeat;
    background-image:url(http://jquery.bassistance.de/validate/demo/images/checked.gif);
    cursor:pointer;
    background-position:left center;
    padding-left:20px;
}

And your jquery could toggle the class to change the background-image

$('div.legend div').click(function() {
    $(this).toggleClass("unchecked");
    var t = $.trim($(this).text());
    var i = $(".headercell td:contains(" + t + ")").index() + 1;
    $('td:nth-child(' + i + ')').toggle();
});

If you want to hide columns on page load simply add a uncheck class to the legend div you want to uncheck.

<div class="legend">
    <div>Name</div>
    <div>Sex</div>
    <div class="uncheckThis">Age</div>
</div>

Then running this script will trigger which ones to uncheck.

$('div.legend div.uncheckThis').click();

Example on jsfiddle.

那请放手 2024-10-26 17:04:05

我猜想控件类应该没问题。所以我把“复选框”包裹在 a 中,而不是你把它放在... IE 不喜欢里面没有 tbody、tr 或 td 的表格。这就是我所做的(演示):

HTML(只是更新的部分)

<div id="tableControl">
    <div class="checked">Name</div>
    <div class="checked">Sex</div>
    <div>Age</div>
</div>

CSS(更新部分)

#tableControl div  {
    cursor: pointer;
    background:url(http://jquery.bassistance.de/validate/demo/images/unchecked.gif) no-repeat;
    padding-left:  20px;
}
#tableControl div.checked {
    background:url(http://jquery.bassistance.de/validate/demo/images/checked.gif) no-repeat;
}

脚本

var hideColumn = function(el) {
    var $el = $(el),
        name = $.trim($el.text()),
        checked = !$el.is('.checked'),
        column = $('.headercell td:contains(' + name + ')').index() + 1;
    $('td:nth-child(' + column + ')').toggle(checked);
    $el.toggleClass('checked', checked);
};

$('#tableControl div')
    .each(function() {
        if (!$(this).is('.checked')) {
            // add class so the script can remove it (on startup)
            $(this).addClass('checked');
            hideColumn(this);
        }
    }).click(function() {
        hideColumn(this);
    });

I was guessing that classes for the controls would be okay. So I wrapped the "checkboxes" in a instead of the you had it in... IE doesn't like tables without a tbody, tr or td inside. So this is what I did (demo):

HTML (just the updated part)

<div id="tableControl">
    <div class="checked">Name</div>
    <div class="checked">Sex</div>
    <div>Age</div>
</div>

CSS (updated portions)

#tableControl div  {
    cursor: pointer;
    background:url(http://jquery.bassistance.de/validate/demo/images/unchecked.gif) no-repeat;
    padding-left:  20px;
}
#tableControl div.checked {
    background:url(http://jquery.bassistance.de/validate/demo/images/checked.gif) no-repeat;
}

Script

var hideColumn = function(el) {
    var $el = $(el),
        name = $.trim($el.text()),
        checked = !$el.is('.checked'),
        column = $('.headercell td:contains(' + name + ')').index() + 1;
    $('td:nth-child(' + column + ')').toggle(checked);
    $el.toggleClass('checked', checked);
};

$('#tableControl div')
    .each(function() {
        if (!$(this).is('.checked')) {
            // add class so the script can remove it (on startup)
            $(this).addClass('checked');
            hideColumn(this);
        }
    }).click(function() {
        hideColumn(this);
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文