The table columns are expanding in width with each click of the "show column" button

https://i.sstatic.net/aZirB.png https://i.sstatic.net/ZC3IP.png every time I click on the button to show or hide certain columns in the table, the width of the columns keeps increasing gradually. Is there a solution for this issue? I am using DataTables with the column visibility button extension. I have already tried setting autoWidth: true but it didn't solve the problem.

Here is my JavaScript code snippet:

var dataTableCurrent = $('#table-current').DataTable({
 autoWidth: true,
 responsive: true,
 dom: 'Bfrtip',
 columnDefs: [ {
  targets :[ 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 ],
  className: 'adv_col'
  } ],
buttons: [ {
  extend: 'columnToggle',
  columns: '.adv_col',
  text: 'Show advanced columns'
} ]
});    

And this is the CSS styling for the table:

.table-wrapper {
    margin: 2em auto;
    width: 50%;
}
table {
    font-family: Helvetica, sans-serif;
    font-size: 0.8rem;
    border: hidden;
    border-collapse: collapse;
}
thead{background-color: green; color: white; text-align: center;}
tbody{background-color: #c2c2c2;} tbody tr:hover {background-color: #0096FF;}
td, th {
    border: 0.5px solid #ddd;
    padding: 8px;
    vertical-align: middle;
}  
tr:nth-child(even){background-color: #f2f2f2;}
#operation-buttons {text-align: center;}
.fa-user-times {color: #FF0000;} .fa-user-times:hover {color: black;}
.fa-archive {color: #2F4F4F;} .fa-archive:hover {color: orange;}
.fa-box-open {color: #2F4F4F;} .fa-box-open:hover {color: yellow;}
.archive-button, .delete-button, .unarchive-button {
    display: inline-block;
    font-size: 1.5rem;
}
#table-archived {display: none};

Answer №1

Try setting autoWidth to false and observe the results. It is recommended to provide a link to codepen or jsfiddle for further testing.

var dataTableCurrent = $('#table-current').DataTable({
 autoWidth: false,
 responsive: true,
 dom: 'Bfrtip',
 columnDefs: [ {
  targets :[ 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 ],
  className: 'adv_col'
  } ],
buttons: [ {
  extend: 'columnToggle',
  columns: '.adv_col',
  text: 'Show advanced columns'
} ]
});    

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

Navigate through a specific div element

I am facing a dilemma with the following code: #points{ width:25%; background:#2a2a2a; height:20px; color:white; z-index:2; position:absolute; } #areas{ position:absolute; color:white; border-right:2px solid black; height:120%; ...

The dropdown menu gets covered by the jQuery Skitter Slideshow

After adding the jquery skitter slideshow above my dynamic drop-down menu bar, I encountered an issue. When I hover over the menu, additional images from the slideshow overlap with the dropdown menu on my site. Please visit this site for reference: I bel ...

Customize the size of innerWidth and innerHeight in your THREEjs project

Is there a way to customize the size of the window where this function launches instead of it automatically calculating the height and width? I've attempted to modify this section of the code, but haven't had any success so far: renderer.setSiz ...

Emphasizes the P tag by incorporating forward and backward navigation options

My goal is to enable users to navigate up and down a list by pressing the up and down arrow keys. For example, if "roma" is currently highlighted, I want it to change to "milan" when the user presses the down arrow key. Here is the HTML code I am working w ...

Is it possible to update only the inner text of all elements throughout a webpage?

Scenario After coming across a thought-provoking XKCD comic, I decided to craft a script that would bring the comic's concept to life: javascript:var a=document.getElementsByTagName('body')[0].innerHTML;a=a.replace(/Program(\w\w+ ...

Retrieve age from date of birth using jQuery and show hidden cells

I need assistance with a user form where I want jQuery to calculate their age when they enter their Date of Birth, and if they are over 35 years old, display a hidden div. Here is the code I have put together so far: $(document).ready(function() { $ ...

Dealing with Sideways Overflow Using slideDown() and slideUp()

Attempting to use slideUp() and slideDown() for an animated reveal of page elements, I encountered difficulty when dealing with a relatively positioned icon placed outside the element. During these animations, overflow is set to hidden, resulting in my ico ...

What's the best way to make div columns appear closer together when floating?

Check out My CodePen: http://example.com/codepen Hello there, today I am working on a 6-column layout with 3 columns in each row. The 3rd column is quite long and resembles a sidebar. According to the design, columns 4 and 5 should be positioned close to ...

The sticky footer in React shifts upwards when the page has minimal content

Having trouble with my web app's footer not staying at the bottom when there is minimal content on the page. I'm feeling stuck and unsure of how to proceed. Can anyone provide guidance on creating a footer that remains at the bottom of the page, ...

Choosing various data using Ajax

I am currently in the process of extracting various pieces of data from my insert.php page, including the post id, username, and user id. I intend to include other selected data as well. However, when trying to echo out multiple queries, all the informatio ...

Initiate the execution of JavaScript functions in the loaded partial view using Ajax

I am currently working with a javaScript function that loads a partial view within my page's layout using the following logic: $('.follow-tasks').click(function (e) { e.preventDefault(); var $followtaskList = $("#td-" + $(this).attr ...

What is the best way to create a new row at a specific index in an ng-repeat loop?

My Goal: I am aiming to insert a new row of ul after every 2 elements in my ng-repeat loop. For example: <ul class="col-sm-2"> <li><p>Automobile & Motorcycle</p></li> ...

How can I enhance the appearance of my custom field using CSS in Advanced Custom Fields?

Utilizing the Wordpress Advanced custom field plugin, I have successfully added a custom field to my site. Within this custom field, I have inserted images and now I am looking to center them on my page using CSS styles such as margin: 0 auto. If you&apo ...

Implementing an AJAX "load more" feature in PHP to retrieve additional data entries

Is there a way to implement a load more button that changes the limit from 0,5 to 0,10 on the second click, and then to 0,15 on the third click, up to a maximum of 0,30? I have been working with PHP and MySQL to retrieve records from the database using th ...

Ways to extract a specific line of text from HTML code

Can someone help me extract a specific line from an HTML code using Python? For instance, in this website: The snippet of code is as follows: var episodes = [[8,54514],[7,54485],[6,54456],[5,54430],[4,54400],[3,54367],[2,54327],[1,54271]]; I am lookin ...

Alignment with Angular 2 Material and Flex-Layout

I've been experimenting with angular 2 material and flex-layout in an attempt to create a responsive gallery of elements. Despite spending countless hours on it, I'm still unable to center my elements as desired: https://i.sstatic.net/Hsicy.png ...

Placing text over an image appears to be causing the navigation links to vanish - could it be a conflict with floating elements?

As a beginner, I'm facing a rather embarrassing situation here. Picture me banging my head on the table level of embarrassment... but let's give it a shot anyways. I've got a navigation bar and an image underneath (not behind). Both seem to ...

Unable to adjust the size of a DIV with a button click in Vue3

I am having trouble resizing a DIV block in Vue3. Whenever I click the button, I receive an error message saying "Cannot read properties of undefined (reading 'style')". I know there is an issue with the JS part of the code, but I'm not sure ...

What is the best way to use PHP to call an ACF variable and substitute a nested HTML element?

Utilizing the repeater field in Wordpress' advanced custom fields, I have made the content on my site dynamic. View an image of the static markup here The following is how I structured the content using plain HTML: <div class="container" ...

I am facing an issue where my Tailwind classes are not functioning properly when passed through props, but they work seamlessly when I directly link the Tailwind CSS using

//Button.jsx import React from 'react'; const Button = (props) => { let color = props.color || 'blue'; return ( <button className={`px-4 py-2 font-bold text-black bg-${color}-500 rounded-full hover:bg-${color}-700 focus ...