简体   繁体   English

单击按钮时如何显示隐藏的列

[英]How to show hidden column when button is clicked

I have a table, and I need hide/show the table column when click the header the column the column hide and the button will appear.when click the button i want to show the hidden coloumn How can I do this? 我有一张桌子,单击标题时,我需要隐藏/显示表格列,然后显示隐藏的列和按钮。单击按钮时,我要显示隐藏的列我该怎么办? see my code here http://jsfiddle.net/9QkVd/29/ . 在这里查看我的代码http://jsfiddle.net/9QkVd/29/

Thanks 谢谢

$(function() {
    $('table tbody tr:odd').addClass('alt');

    $('table tbody tr').hover(function() {
        $(this).addClass('hover');
    }, function() {
        $(this).removeClass('hover');
    });
});

$('tr th:gt(0)').click(function() {

    var index = (this.cellIndex + 1);
    var cells = $('table tr > :nth-child(' + index + ')');
    cells.toggleClass('hide');

    if ($(this).hasClass('hide')) {
        $(this).find('span').html('<b>+</b>');
    }
    else {
        $(this).find('span').html('<b>-</b>');
    }

    if ($('table tr > th:not(.hide)').length)
        $('table').removeClass('hide');
    else
        $('table').addClass('hide');
     $('.btnAssociate').show();
});

 $('.btnAssociate').click(function()
    {


         $('.btnAssociate').hide();

    });

Try this, 尝试这个,

$('.btnAssociate').click(function () {
    $('table th,table td').removeClass('hide');
    $('.btnAssociate').hide();
});

Demo 演示版

Here is a nifty way of doing it , you can keep hiding the columns and bring them back in order of the most recently clicked item. 这是一种很不错的方法 ,您可以继续隐藏列,并按最近单击的项目的顺序将其移回

Basically add an array to store the index values of the column you click on: 基本上添加一个数组来存储您单击的列的索引值:

var indexVal = [];

And then on the button click function you write: 然后在按钮单击功能上编写:

var cells = $('table tr > :nth-child(' + indexVal[indexVal.length-1] + ')');
cells.toggleClass('hide');
indexVal.pop();
if (!indexVal) $('.btnAssociate').hide();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM