Conceal a column in jQuery DataTables without completely removing it from the DOM

I am trying to conceal a column in jQuery datatables without removing it from the DOM completely. When I attempt to hide the column using the bVisible property, it disappears entirely.

My goal is to set the display property of the table cells in that column to none so that the values are hidden on the page, but still exist in the DOM. This column serves as a unique identifier for each row, and I need access to the unique ID when a row is selected. How can I accomplish this?

I am populating the table using the aaData property with server-side pagination.

I have reviewed this question, but their solutions seem to remove the column from the DOM entirely. jQuery datatables hide column

Answer №1

To hide a column in your table, you can use the className attribute along with either the columnDefs or columns.

Simply define a hide_column class in your CSS like this:

.hide_column {
    display : none;
}

There are two ways to assign the .hide_column class:

1. Using columnDefs (assign custom class to first column):

$('#example').DataTable( {
  columnDefs: [
    { targets: [ 0 ],
      className: "hide_column"
    }
  ]
} );

2. Using columns:

$('#example').DataTable( {
  "columns": [
    { className: "hide_column" },
    null,
    null,
    null,
    null
  ]
} );

You can find more information and code snippets on styling columns here.


Previous Answer:

If you want to hide a column, try adding:

"sClass": "hide_column"

This should effectively hide the specified column.

Answer №2

Expanding on the explanation provided by Daniel:

CSS:

th.hide_me, td.hide_me {display: none;}

When initializing datatables:

"aoColumnDefs": [ { "sClass": "hide_me", "aTargets": [ 0 ] } ] // Assigning the class "hide_me" to the first column in the visible columns array

Don't forget to apply the hidden class to the header cell as well:

<thead>
    <th class="hide_me">First Column</th>
    <th>Second Column</th>
    <th>Third Column</th>
</thead>

This method is especially useful when working with server-side processing and need to include data from an AJAX source without displaying it in the datatable. You can still access the column's value on the front-end without showing it, which is beneficial for filtering based on hidden data values.

For instance:

// Within the datatables initialization script
<script>
    var filteredValues = [];
    $('td.your_filtering_class').each(function(){
        var someVariable = $(this).find('.hide_me').html();
        filteredValues.push(someVariable);
    }
</script>

Answer №3

To conceal various columns simultaneously:

$('#table').dataTable({
  "columnDefs": [{ 
    "targets": [0,2,4], //Values separated by commas
    "visible": false,
    "searchable": false }
  ]
});

Answer №4

Here is my contribution for you.

I'm not entirely sure if the code is correct, but it seems to be working.

If you have more than one setup column like me,

$('#example').dataTable( {
  "columnDefs": [ {
        "targets"  : 'no-sort',
        "orderable": false,
        "order": [],
    }],
    "columnDefs": [ {
        "targets"  : 'hide_column',
        "orderable": false,
        "order": [],
        "visible": false
    }]
} );

Answer №5

If you want to conceal an element, simply employ the hide method.

$(element).hide();

Conversely, if you wish to reveal the element, utilize the show method:

$(element).show();

In order to select a specific column, consider using the n-th child selector from jquery.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Thumbnail Centering Issue in Swiper Plugin by iDangero.us

Currently, I am utilizing the Swiper plugin created by iDangero.us. Within my layout design, there are 6 slider images accompanied by 6 thumbnails. Surprisingly, when I attempt to center align the thumbnails, it appears to cause issues with the plugin. Th ...

PHP: Utilizing $_SESSION variables for automatic form filling across various PHP pages

I've spent an hour searching for a solution to the problem below, but I haven't been able to find one that fits my specific issue. However, I have some ideas that I'll share after outlining the problem. Problem: In an html form within a ph ...

Is it possible to create two distinct homepages for a single website?

I'm currently working on a project where I am creating a replica of the website Make My Trip This website has different layouts for laptop and mobile devices, appearing completely different. How can we achieve this kind of functionality? Here is the ...

Fade in and out list items while adjusting the height of the container

Check out this code snippet I put together in Codepen: <div class="slider"> <ul> <li id="S1"> <div class="txt"><p>First slogan</p></div> <div class="img"><img src="http://placehold.it/80 ...

Utilize Twitter Bootstrap 4 to organize menu items in neat rows

I would like my menu to be structured in rows rather than wrapping and breaking to the next line when there is no more space. For instance: https://i.sstatic.net/dInTx.png Currently, it looks like this: https://i.sstatic.net/QkVS6.png Even changing to ...

Javascript addClass and removeClass functions are not functioning as expected

Can you help me figure out why the icon still goes into the "startSharing" section even when it is turned off? In my html file, I have the following code for the icon: <div class="startSharing"></div> And in my javascript file, I have the fo ...

Generating a highchart by retrieving JSON data using AJAX

I'm currently working on generating a basic chart on a webpage using data from a MySQL database that is fetched via a MySQL script. My main challenge lies in understanding how to combine the ajax call with the necessary data for the chart. I'm n ...

Tips for accessing the ID in a specific row of a datatable

My module displays a list of customer details in a datatable with a button for action. Why am I unable to retrieve the id of the button added to each row? When I click the button, no alert is displayed. What could be causing this issue? Below is my ajax ...

Revamping FullCalendar: Strategies for refreshing or initializing FullCalendar anew or efficiently adding multiple events at once

I attempted to batch add new events to the calendar but failed to find a convenient method to do so. As a result, I decided to reinitialize the view with a new events array. Here's what I tried: var events = [ { title: 'Conference&ap ...

Is there a Jquery function that retrieves the html content of a specified div element?

After trying multiple approaches without success, I am struggling with extracting the HTML content of a div element in my HTML layout. The div element is declared as ("myDiv"). I am looking to create a function that can accept any div as an argument and th ...

How to Retrieve Element Property Values from the DOM with JavaScript

I am currently attempting to access the property of an element within my webpage. My main objective is to toggle a float property between left and right when a specific onClick event occurs. However, despite my efforts, I am facing challenges in even acces ...

Is there a way to have this <a> link trigger a postback to my ASP.NET method when clicked?

Is there a way for me to trigger a postback to my method when I click on a specific link? <li runat="server" onclick="log_out" > <a onclick="log_out" runat="server" href="LogIn.aspx" ><i class="icon_key_alt"></i> Log Out</a& ...

Looking to utilize jQuery for writing and reading data to and from a JSON file?

My goal is to create a JSON file and write data to it without any limitations on entries. I also need to be able to read this data from the file and display it on an HTML page, where I can collect the necessary information. As a newcomer to jQuery, I woul ...

The issue of submitting a form with Ajax serialization

I have searched through similar questions, but none have provided an accurate answer or fix. Please offer a solution! I am using Ajax to submit a form to a page and expecting a value in return. It's something I've done many times before, but for ...

The Colorful World of CSS Backgrounds

I've been searching for hours trying to track down the source of this strange greenish background color. I've combed through every single file and it's starting to drive me insane. Any ideas where this color could be coming from? I highly d ...

When the button is clicked for the first time, the select option will return null

I have created a button that triggers the opening of a modal window. Inside this modal, there is a select option element. My goal is to retrieve the value of the selected option every time I click the button. However, I am facing an issue where on the fir ...

Using object-fit with the value of "cover" expands the size of the containing

Currently, I am setting up a grid for a blog where I have cropped the images using object-fill: cover; to ensure uniformity in appearance regardless of the image aspect ratio. However, this approach is causing an issue by stretching the parent element (a d ...

Adjust the margin-top based on the height of another element

I'm facing an issue with two fixed navbars positioned one under the other. I used the 'fixed-top' class to fix both at the top, but the margin-top I added to make them align is not responsive. Is there a way to make the margin-top responsiv ...

CSS transition not working properly when toggling

I am working on creating a toggle feature for a div that can expand and collapse upon clicking. While I have successfully implemented the expand transition using max-height to accommodate varying content sizes, I am facing difficulties with transitioning t ...

With JSX and CSS in React, I have the ability to easily generate two columns, but unfortunately cannot achieve the same fluid

I am attempting to create a layout with three columns of text fields and labels. While I can easily achieve this with two columns, the third text field is appearing on a separate row below: <div className={styles.threecol}> <div className= ...