Issue with resetting column widths in Datatables

I am trying to set the maximum width for a datatable with a fixed header using JavaScript.

Below is the JavaScript code snippet I am using:

  var oTable = $('#example').DataTable(
                  {
                    "sScrollY":  200,
                    // "bScrollCollapse": true,
                    // "bAutoWidth": true,
                    // "sScrollX": "100%",
                    // "sScrollXInner": "100%"
   });

However, when I apply the "sScrollY" property, the column widths are not adjusting properly. To fix this issue, I want to reset the column width for each <th> and <tr>.

I have attempted various approaches to solve this problem without success.

Here is the HTML Code:-

<section class="panel">
                          <header class="panel-heading">
                              Scheduled Outages
                          </header>
                          <div class="panel-body">
                                <div class="adv-table">
                                    <table  class="display table table-bordered table-striped" id="example1">
                                      <thead>
                                      <tr>
                                          <th>Id</th>
                                          <th>Planned Start Date</th>
                                          <th>Planned End Date</th>
                                          <th>D...</th>
                                          <th class="hidden-phone">Ho...</th>
                                          <th class="hidden-phone">Syst...</th>
                                          <th class="hidden-phone">Area</th>
                                          <th class="hidden-phone">Loc Name</th>
                                          <th class="hidden-phone">Loc Unit</th>
                                      </tr>
                                      </thead>
                                      <tbody>
                                        <tr class="gradeX">
                                          <td>65</td>
                                          <td>01/17/2018 00:00:00</td>
                                          <td>01/22/2018 00:00:00</td>
                                          <td class="center hidden-phone">5</td>
                                          <td class="center hidden-phone">0</td>
                                          <td class="center hidden-phone">Mainline</td>
                                          <td class="center hidden-phone">Kennedy Ave(WO01)</td>
                                          <td class="center hidden-phone">Kennedy Ave</td>
                                          <td class="center hidden-phone">Meter Loopline</td>
                                      </tr>
                                      </tbody>
                                      <tfoot>
                                      <tr>
                                          <th>Id</th>
                                          <th>Planned Start Date</th>
                                          <th>Planned End Date</th>
                                          <th>D...</th>
                                          <th class="hidden-phone">Ho...</th>
                                          <th class="hidden-phone">Syst...</th>
                                          <th class="hidden-phone">Area</th>
                                          <th class="hidden-phone">Loc Name</th>
                                          <th class="hidden-phone">Loc Unit</th>
                                      </tr>
                                      </tfoot>
                                    </table>
                                </div>
                          </div>
                      </section>

Image Link :-

https://i.sstatic.net/JNP4G.png

Answer №1

Don't forget to check out columns.adjust() :

var oTable = $('#example').DataTable({
  sScrollY: 200px,
  initComplete: function() {
    this.api().columns.adjust().draw()
  }
})

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

The jQuery date picker is showing an error with the function daySettings[2].replace, indicating that

Encountering a problem with the jQuery version I am using: <script src="js/Common/jquery-2.1.1.min.js" type="text/javascript"></script> <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script> The code I have writte ...

The origin of the image is specified within the HTML document

I recently encountered an issue while trying to include images in my HTML file. When I used a local file path like background-image: url('file:///C:/Users/faycel/Desktop/site%20guide/paris.jpg'), the image displayed correctly. However, when I tri ...

Checkbox unchecked, function not detected

I am faced with a scenario where I have a checkbox that triggers another function upon being checked. The code snippet below showcases how this functionality is implemented: function Allchecks1(event) { event.stopPropagation(); var ...

jQuery and Codeigniter collaboration results in unsuccessful deletion of the second image

I encountered a problem while trying to delete data about pictures from the database and folders, and updating the page using jQuery ajax. Initially, I attempted to do this using <a href> but faced an issue where the URL was changed (ending with &apo ...

PHP pattern matching equivalent to selecting elements based on their position in CSS

Struggling with the logic to identify objects in an array by their position. Seeking a PHP equivalent of nth-child(5n + 1) condition, which targets positions like 0, 5, 10, and so on within a loop. One approach could be to divide by 5 and check for whole ...

HTML - one div's child element takes precedence over another div

My HTML page features two consecutive divs. The first div contains a child span that has a relative position, causing it to overlap the second div. I have implemented click events for both divs. However, when I click on the span that overlaps the second d ...

"Enhancing User Interaction: The Dynamic Trio of Ajax, Php, and MySql in Comment

I have successfully developed a basic commenting system, featuring a form with two fields: name and comment. Upon submitting the values, it utilizes Ajax to dynamically add the comment to the existing page without refreshing the entire content. My next ob ...

Television Mount for Precise Video Placement

Seeking assistance for a unique positioning challenge I am facing. I have a Video that needs to be placed on the home page with a TV-like image "frame" around it, and they should scale together. https://i.sstatic.net/2pLhi.png What I've attempted i ...

Code working in Chrome but not in Firefox: JavaScript hyperlink issue

Within my Rails project, an HTML file displays the following: <a href='#' id="show_advanced">Show advanced options</a> Upon clicking on the link, JavaScript/JQuery code is executed: jQuery("#show_advanced").click(function() { // ...

Struggling to extract a specific value from a JSON string?

I am trying to retrieve the first element of a JSON object, but using data.first returns nothing. However, when I use alert(data), it prints the full JSON string. Can someone please help me troubleshoot this? <?php include_once '../con_db.php&apo ...

Tips on utilizing Selenium to input a tab character in a textarea field

Currently, I am utilizing Selenium Web Driver (Java, Chrome Browser) and I need to input a tab character into a text area. The default behavior is that when a tab character is entered, the next input receives focus. However, my intention is for the tab cha ...

Developing a custom CSS for React using clamp and focus attributes

I'm currently working on creating an object to be used in inline style, but I'm having trouble figuring out how to correctly write `clamp` and `after`. const PhoneInputStyle = { fontSize: clamp("13px", "1.111vw", "16px"), /*this particular ...

Is it possible to specifically focus on Google Chrome?

Can anyone help me with positioning the update button on www.euroworker.no/order? The button works fine in Firefox and Internet Explorer, but it's not displaying correctly in Chrome or Safari. I've tried targeting Safari and Chrome specifically, ...

Is the height of each column the same as the deepest column?

If my div layout looks like this: <div id="stretchyheader"></div> <div id="fixedwidthwide"><div> <div id="fixednarrow></div> <div id="footer"></div> Resulting in the following structure: ------------------ ...

How can you determine the index of a table column in JavaScript when you only know its class name?

I am looking for a way to dynamically hide/show table columns based on their classes, without having to add classes to each individual <td> element. This should be accomplished using classes on the columns themselves. Here is a sample of the table ...

What is the best method for using CSS/HTML to showcase text at a height of 2mm consistently across various devices with varying screen resolutions and dimensions?

Designing a basic webpage to showcase the text "Hello, World!": <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <meta charset="utf-8"> </head> <body> <p>Hello, Wor ...

Leveraging the power of jQuery, utilize both mousemove and setTimeout in combination with clearTimeout to seamlessly toggle

Below is a code snippet that shows a typical method for displaying a menu on mousemove. It appears that the timeout duration increases as the mouse continues to move. Why is this happening? Could there be a reason why the cleartimeout function is not wor ...

Enable the x-axis months to be interactive in a chart.js line graph

Currently, I am in the process of developing a line chart using chart.js. The x-axis of the chart is time-based and represents months. I want to make each "month column" clickable/selectable, but I'm facing difficulty in achieving this functionality ...

Utilizing jQuery to target an element by its class variable, even when it has multiple classes

This is a bit of an unusual issue. Summary: I have checkboxes that need to function like radio buttons (don't ask...lol). Meaning when one checkbox is selected, the others in the group need to be unchecked, leaving only the one that was just select ...

Understanding how to access both 'this' variables in TypeScript

When working in TypeScript, it's important to note that defining a function using () => {...} will make the this variable refer to the instance of the surrounding class. Conversely, using function () {...} will result in the this variable maintaini ...