Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show/ Hide Table Column with colspan using jQuery

Tags:

html

jquery

css

I have a HTML table as shown in http://jsfiddle.net/Lijo/auny3/ . The table has 10 columns – divided into three col groups. I need to hide/show the colgroup named “Associate Info” (including its rows data) using buttons “Show Associate” and “Hide Associate”.

What is the best way (in terms of performance) for doing this using jQuery?

Please answer if you have a better answer than http://jsfiddle.net/Lijo/auny3/8/

Note: I am generating the table using ASP.Net GridView as given in http://www.sacdeveloper.com/Community/Articles/tabid/86/ctl/ArticleView/mid/458/articleId/1/Group_columns_in_an_ASPNet_Gridview.aspx

Reference:

  1. http://jsfiddle.net/Lijo/auny3/8/

  2. http://jsfiddle.net/Lijo/auny3/12/

  3. http://jsfiddle.net/Lijo/auny3/11/

  4. http://jsfiddle.net/Lijo/auny3/13/

Selected Answer

  1. http://jsfiddle.net/Lijo/UqdQp/2/

enter image description here

like image 756
LCJ Avatar asked Sep 17 '12 08:09

LCJ


2 Answers

Hi now used to this

Jquery

$(document).ready(function(){

$("#show").click(function(){
    $("#showhide").show();
    });



    $("#hide").click(function(){
    $("#showhide").hide();
    });
})  

and some change to your html

Live demo

like image 145
Rohit Azad Malik Avatar answered Sep 22 '22 14:09

Rohit Azad Malik


Or you can use nth-child selector:

$('input').click(function(){
    if($(this).val() == "Hide Associate"){
    $('th:nth-child(2), td:nth-child(2), th:nth-child(3):not(:first), td:nth-child(3), th:nth-child(4), td:nth-child(4), th:nth-child(5), td:nth-child(5)').hide();
    }else{
        $('th:nth-child(2), td:nth-child(2), th:nth-child(3):not(:first), td:nth-child(3), th:nth-child(4), td:nth-child(4), th:nth-child(5), td:nth-child(5)').show();
    }
});
like image 29
Alex Ball Avatar answered Sep 26 '22 14:09

Alex Ball