[jquery] Hide/Show Column in an HTML Table

Here's a little more fully featured answer that provides some user interaction on a per column basis. If this is going to be a dynamic experience, there needs to be a clickable toggle on each column that indicates the ability to hide the column, and then a way to restore previously hidden columns.

That would look something like this in JavaScript:

$('.hide-column').click(function(e){
  var $btn = $(this);
  var $cell = $btn.closest('th,td')
  var $table = $btn.closest('table')

  // get cell location - https://stackoverflow.com/a/4999018/1366033
  var cellIndex = $cell[0].cellIndex + 1;

  $table.find(".show-column-footer").show()
  $table.find("tbody tr, thead tr")
        .children(":nth-child("+cellIndex+")")
        .hide()
})

$(".show-column-footer").click(function(e) {
    var $table = $(this).closest('table')
    $table.find(".show-column-footer").hide()
    $table.find("th, td").show()

})

To support this, we'll add some markup to the table. In each column header, we can add something like this to provide a visual indicator of something clickable

<button class="pull-right btn btn-default btn-condensed hide-column" 
            data-toggle="tooltip" data-placement="bottom" title="Hide Column">
    <i class="fa fa-eye-slash"></i>  
</button>

We'll allow the user to restore columns via a link in the table footer. If it's not persistent by default, then toggling it on dynamically in the header could jostle around the table, but you can really put it anywhere you'd like:

<tfoot class="show-column-footer">
   <tr>
    <th colspan="4"><a class="show-column" href="#">Some columns hidden - click to show all</a></th>
  </tr>
</tfoot>

That's the basic functionality. Here's a demo below with a couple more things fleshed out. You can also add a tooltip to the button to help clarify its purpose, style the button a little more organically to a table header, and collapse the column width in order to add some (somewhat wonky) css animations to make the transition a little less jumpy.

Demo Screengrab

Working Demo in jsFiddle & Stack Snippets:

_x000D_
_x000D_
$(function() {_x000D_
  // on init_x000D_
  $(".table-hideable .hide-col").each(HideColumnIndex);_x000D_
_x000D_
  // on click_x000D_
  $('.hide-column').click(HideColumnIndex)_x000D_
_x000D_
  function HideColumnIndex() {_x000D_
    var $el = $(this);_x000D_
    var $cell = $el.closest('th,td')_x000D_
    var $table = $cell.closest('table')_x000D_
_x000D_
    // get cell location - https://stackoverflow.com/a/4999018/1366033_x000D_
    var colIndex = $cell[0].cellIndex + 1;_x000D_
_x000D_
    // find and hide col index_x000D_
    $table.find("tbody tr, thead tr")_x000D_
      .children(":nth-child(" + colIndex + ")")_x000D_
      .addClass('hide-col');_x000D_
      _x000D_
    // show restore footer_x000D_
    $table.find(".footer-restore-columns").show()_x000D_
  }_x000D_
_x000D_
  // restore columns footer_x000D_
  $(".restore-columns").click(function(e) {_x000D_
    var $table = $(this).closest('table')_x000D_
    $table.find(".footer-restore-columns").hide()_x000D_
    $table.find("th, td")_x000D_
      .removeClass('hide-col');_x000D_
_x000D_
  })_x000D_
_x000D_
  $('[data-toggle="tooltip"]').tooltip({_x000D_
    trigger: 'hover'_x000D_
  })_x000D_
_x000D_
})
_x000D_
body {_x000D_
  padding: 15px;_x000D_
}_x000D_
_x000D_
.table-hideable td,_x000D_
.table-hideable th {_x000D_
  width: auto;_x000D_
  transition: width .5s, margin .5s;_x000D_
}_x000D_
_x000D_
.btn-condensed.btn-condensed {_x000D_
  padding: 0 5px;_x000D_
  box-shadow: none;_x000D_
}_x000D_
_x000D_
_x000D_
/* use class to have a little animation */_x000D_
.hide-col {_x000D_
  width: 0px !important;_x000D_
  height: 0px !important;_x000D_
  display: block !important;_x000D_
  overflow: hidden !important;_x000D_
  margin: 0 !important;_x000D_
  padding: 0 !important;_x000D_
  border: none !important;_x000D_
}
_x000D_
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css">_x000D_
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.3.7/paper/bootstrap.min.css">_x000D_
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">_x000D_
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>_x000D_
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>_x000D_
_x000D_
_x000D_
_x000D_
_x000D_
<table class="table table-condensed table-hover table-bordered table-striped table-hideable">_x000D_
_x000D_
  <thead>_x000D_
    <tr>_x000D_
      <th>_x000D_
        Controller_x000D_
        <button class="pull-right btn btn-default btn-condensed hide-column" data-toggle="tooltip" data-placement="bottom" title="Hide Column">_x000D_
          <i class="fa fa-eye-slash"></i>  _x000D_
        </button>_x000D_
      </th>_x000D_
      <th class="hide-col">_x000D_
        Action_x000D_
        <button class="pull-right btn btn-default btn-condensed hide-column" data-toggle="tooltip" data-placement="bottom" title="Hide Column">_x000D_
          <i class="fa fa-eye-slash"></i>  _x000D_
        </button>_x000D_
      </th>_x000D_
      <th>_x000D_
        Type_x000D_
        <button class="pull-right btn btn-default btn-condensed hide-column" data-toggle="tooltip" data-placement="bottom" title="Hide Column">_x000D_
          <i class="fa fa-eye-slash"></i>  _x000D_
        </button>_x000D_
      </th>_x000D_
      <th>_x000D_
        Attributes_x000D_
        <button class="pull-right btn btn-default btn-condensed hide-column" data-toggle="tooltip" data-placement="bottom" title="Hide Column">_x000D_
          <i class="fa fa-eye-slash"></i>  _x000D_
        </button>_x000D_
      </th>_x000D_
  </thead>_x000D_
  <tbody>_x000D_
_x000D_
    <tr>_x000D_
      <td>Home</td>_x000D_
      <td>Index</td>_x000D_
      <td>ActionResult</td>_x000D_
      <td>Authorize</td>_x000D_
    </tr>_x000D_
_x000D_
    <tr>_x000D_
      <td>Client</td>_x000D_
      <td>Index</td>_x000D_
      <td>ActionResult</td>_x000D_
      <td>Authorize</td>_x000D_
    </tr>_x000D_
_x000D_
    <tr>_x000D_
      <td>Client</td>_x000D_
      <td>Edit</td>_x000D_
      <td>ActionResult</td>_x000D_
      <td>Authorize</td>_x000D_
    </tr>_x000D_
_x000D_
  </tbody>_x000D_
  <tfoot class="footer-restore-columns">_x000D_
    <tr>_x000D_
      <th colspan="4"><a class="restore-columns" href="#">Some columns hidden - click to show all</a></th>_x000D_
    </tr>_x000D_
  </tfoot>_x000D_
</table>
_x000D_
_x000D_
_x000D_

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to hide

bootstrap 4 responsive utilities visible / hidden xs sm lg not working jQuery get the name of a select option jQuery select change show/hide div event Hide Show content-list with only CSS, no javascript used Permanently hide Navigation Bar in an activity How can I hide/show a div when a button is clicked? jQuery hide and show toggle div with plus and minus icon Hide Text with CSS, Best Practice? Show div #id on click with jQuery JavaScript - Hide a Div at startup (load)

Examples related to html-table

Table column sizing DataTables: Cannot read property 'length' of undefined TypeError: a bytes-like object is required, not 'str' in python and CSV How to get the <td> in HTML tables to fit content, and let a specific <td> fill in the rest How to stick table header(thead) on top while scrolling down the table rows with fixed header(navbar) in bootstrap 3? Sorting table rows according to table header column using javascript or jquery How to make background of table cell transparent How to auto adjust table td width from the content bootstrap responsive table content wrapping How to print table using Javascript?

Examples related to show

How to show all of columns name on pandas dataframe? jQuery select change show/hide div event Hide Show content-list with only CSS, no javascript used How can I hide/show a div when a button is clicked? jQuery hide and show toggle div with plus and minus icon Show div #id on click with jQuery jQuery if statement to check visibility How to print a double with two decimals in Android? Programmatically Hide/Show Android Soft Keyboard Show Hide div if, if statement is true