Fixed columns with horizontal scrolling to prevent Datatables columns from overlapping

I'm facing an issue with my data table created using datatables. I need to fix the first two columns, but when I do so, the other two columns overlap them while scrolling horizontally. If I remove the fixed columns, the scrolling works correctly without any overlap. Here is an example where the Capacity column is overlapping the Equipment Name: https://i.sstatic.net/AHlo6.png

Html Code:

<style>
  .bg-dark {
    background-color: #2980b9 !important;
  }

  .thead-dark th {
    background-color: #2980b9 !important;
  }

  body {
    font-family: 'Nunito Sans', sans-serif;
  }

  .table td,
  .table th {
    height: 1em;
    font-size: 12px !important;
    padding: 5px 5px;
  }

  .table-scroll {
    overflow-x: scroll;
  }

</style>

...

<p>Javascript Code:</p>
<pre><code><script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.js"></script>
<!-- <script type="text/javascript" src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script> -->
<script
  type="text/javascript"
  src="{% static 'js/jquery.datatables.min.js' %}"
></script>

<script type="text/javascript" src="https://cdn.datatables.net/fixedcolumns/4.2.2/js/dataTables.fixedColumns.min.js"></script>   
<script>
      $(document).ready(function () {
                var table = $("#equipmentCostData").DataTable({ 
                  fixedColumns: true,
                  fixedColumns: {
                    left: 2,
                    right: 0
                  },
                  
                  responsive: true,
                });
            });
</script>

Is there a way to fix the first two columns without overlap while scrolling horizontally?

Answer №1

My Solution:

To fix the columns, I adjusted the style settings for background color and z-index for the first two columns.

<div class="card-body">
            <div class="table-responsive">
              <table class="table dataTable table-striped table-bordered" id="equipmentCostData">
                <thead class="thead-dark">
                  <tr>
                    <th style="z-index: 2 !important;">OPCO</th>
                    <th style="z-index: 2 !important;">Equipment Name</th>
                    <th>Capacity</th>
                    <th>Annual<br/> Distance</th>
                    <th>Equipment <br/> Price</th>
                    <th>Tyre Type</th>
                    <th>Tyre <br/> Count</th>
                    <th>Finance<br/> Fleet %</th>
                    <th>Target <br/> Profit %</th>
                    <th>Loan <br/> Amount %</th>
                    <th>Interest <br/> Rate %</th>
                    <th>Loan<br/> Years</th>
                    <th>Amortization <br/> Years</th>
                    <th>Residual<br/> Value</th>
                    <th>Maintenance Cost/Unit <br/> Distance</th>
                    <th> Tax </th>
                    <th>Insurance</th>
                    <th>Admin <br/> Cost</th>
                    <th>Other Cost <br/> (Misc.)</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                  {% for e in equipment %}
                  <tr id="{{e.id}}">
                    <td style="background-color: white !important;">{{e.opco_id__opco_name}}</td>
                    <td style="background-color: white !important;">{{e.equipment_name}}</td>
                    <td>{{e.capacity}}</td>
                    <td>{{e.annual_distance}}</td>
                    <td>{{e.cost}}</td>
                    <td>{{e.tyre_type_id__tyre_type}}</td>
                    <td>{{e.tyre_count}}</td>
                    <td{{e.finance_fleet}}</td>
                    <td>{{e.target_profit}}</td> 
                    <td>{{e.loan_amount}}</td> 
                    <td>{{e.interest_rate}}</td>
                    <td>{{e.loan_years}}</td>
                    <td>{{e.amotization_years}}</td>
                    <td>{{e.residual_value}}</td>
                    <td>{{e.maintenance_cost_per_unit_distance}}</td>
                    <td>{{e.tax}}</td>
                    <td>{{e.insurance}}</td>
                    <td>{{e.admin_cost}}</td>
                    <td>{{e.other_cost}}</td>
                    <td> 
                      <i class='fa fa-edit editBtn' style="cursor: pointer;"></i>
                      <i class='fa fa-trash pl-3 deleteBtn' style="cursor: pointer;"></i>
                    </td>
                    
                  </tr>
                  {% endfor %}
                </tbody>
              </table>
            </div>
          </div>

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

Is there a way to link the datepicker of one calendar to automatically sync with another under specific circumstances?

I have implemented two datepicker fields on a single page: Start Date and End Date. $("#orders-start-date").datepicker({ dateFormat: "yy-mm-dd", maxDate: new Date(), onSelect: function(date, el) { $this.trigger("orders:filter", date, $ ...

Can you explain the significance of these specific HTML attributes within the button tag?

While browsing the web, I stumbled upon this HTML snippet: <button class="button--standard" mat-button mat-flat-button [disabled]=true >Disabled State</button> The combination of attributes like mat-button mat-flat-button [disabled]=true is u ...

Storing distinct values in separate variables within a JavaScript for loop

I'm attempting to assign various values generated by an outer loop to different variables and then utilize those variables as an array. var code = []; for (i = 0; i < 5; i++) { code[i] = mp3; } Is there a way to access variables in this manner ...

The fixed-top navigation bar in Bootstrap obscures the links within the #content

As a student studying software development, I am currently working on a web development project for my class. Utilizing Bootstrap, I have implemented a navbar-fixed-top and structured the body as a table-striped table with each row containing a <a href= ...

Having trouble getting the innerHTML update to be triggered by onchange

Hey there, I am looking to tweak my file upload button so that it triggers a change in a specific span, signaling to the user that a file has indeed been selected. <script type="text/javascript"> function get_fileName(fileSelector, fileId) { var ...

Error message: The 'Access-Control-Allow-Origin' policy is preventing access in a react express docker application

I have successfully set up a front-end React application and a Node/Express API using Docker. The React app is currently running on localhost:3000, while the API is running on localhost:9000. Both applications are fully functional. However, I am encounteri ...

Utilize a fresh function in Angular to retrieve and store data from a URL into a variable

Currently, I am attempting to utilize Angular in order to retrieve data from a link upon clicking a button. As a newcomer to Angular with only 2 days experience, my knowledge is quite limited. What I aim to achieve is triggering the loading of JSON data w ...

The iframe content is not updating despite using jQuery

On this site , the following code is present: <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <iframe style="position: absolute; top: 0; left: 0; height: 100%; width: 100%" src="blank.html"></iframe ...

Incorporate pictures from the popup onto the main page

I have developed a basic PHP image editor script where users can select images from galleries and merge them together. However, I am facing an issue: The galleries open in a popup while the editor area is on the parent page. I am looking for a JavaScript ...

DIV size issue resolved

Content within a div is surpassing the set fixed size determined by CSS. <style> #fixeddiv { position: fixed; margin: auto; max-height: 300px; max-width: 700px; } </style> <div id="fixeddiv"> <div id="M77 ...

What causes the Object expected error in jQuery?

Looking for a way to demo horizontal collapse pane with a simple web page that includes html, css, and jquery. <html> <head> <script type="text/javascript" src="//code.jquery.com/jquery-1.10.1.js"></script> <title>Sa ...

Jquery's remove function fails to function correctly when used on a cloned element

I am facing an issue where I have a list of rows that need to be deleted. However, when I attempted to use jQuery's remove function, it only removed the original row and not its clone. My goal is for the parent element of the parent to be removed when ...

Switching from an AJAX GET request to a POST request involves updating the

I have been trying to figure out how to convert my AJAX GET query to POST by reading forums and searching on Google, but I am still confused. If someone could assist me with this, it would be greatly appreciated. Thank you! Here is the code snippet I am w ...

Tool to track character limits in multiple text fields

I'm facing a challenge with a form that includes 3 text areas, a copy button, and a reset button. My goal is to concatenate all the characters in the text areas to get a sum which will be displayed next to the copy/reset button. The character limit is ...

Variability in Focus Behavior while Opening a URL in a New Tab with window.open()

Here is a code snippet I have been using to open a URL in a new tab: window.open(urlToOpen, '_blank', 'noopener noreferrer'); The issue I am experiencing is that when this code is executed for the first time, it opens the URL in a new ...

What is needed to enable the functionality of the next and previous buttons? (Carousel Bootstrap)

I'm working on a text-only carousel using Bootstrap, but I'm facing an issue with the slider not functioning properly when I press the "next" and "prev" buttons. Any assistance would be greatly appreciated! <link rel="stylesheet" href="htt ...

Determining the scroll position of a JQuery Mobile collapsible set upon expansion

I am utilizing jQueryMobile (v1.4.0) collapsible set / accordions to showcase a list of elements along with their content, which can be seen in this jsFiddle. <div id="List" data-role="collapsible-set"> <div data-role="collapsible" data-conte ...

What is the best way to bring in an image when the resolution falls below 700 pixels?

Is there a way to import an image only if the screen resolution is greater than 700px? HTML: <picture> <img id="logo_mobile"> <!-- I have also tried this --> <source srcset="logomobile.jpg ...

Tips on sending data with a unique identifier to the backend through a route parameter

Can anyone help me figure out how to send a message to a specific backend route parameter while passing the current ID? I'm not sure how to inform the system about this ID. Here's the Vuex action: postMessage({commit}, payload, id) { axios.pos ...

What are some ways to customize the text and button location for Bootstrap 5's file input?

Bootstrap 5's input type file seems too simplistic. Check it out here https://i.stack.imgur.com/VZ0h5.png I am curious about three things: Can the "Choose file" button be moved to the right? Is it possible to change the message that says "No files ...