Incorporate a vertical scrollbar in the tbody while keeping the thead fixed for smooth vertical scrolling

I'm seeking a solution to implement horizontal and vertical scroll for my table.

Currently, the horizontal scroll is working fine, but when trying to add vertical scroll, the table header also moves along with it. Is there a way to keep the table header fixed only when scrolling vertically?

.table-responsive {
    max-height:200px;
    overflow-x: auto !important;
    overflow-y: auto !important;
}
<div class="table-responsive table-striped">
    <div id="tblLocationInfo_wrapper" class="dataTables_wrapper form-inline dt-bootstrap">       
        <table id="tblLocationInfo" class="table table-hover dataTable" role="grid" style="width: 2400px;">
            <thead>
                <tr role="row"></tr>
                <tr role="row"></tr>
                <tr role="row"></tr>
                <tr role="row"></tr>
                <tr role="row"></tr>
                <tr role="row"></tr>
                <tr role="row"></tr>
                <tr role="row">
                    <th class="sorting" tabindex="0" aria-controls="tblLocationInfo" rowspan="1" colspan="1" aria-label="TaskType: activate to sort column ascending" style="width: 762px;">TaskType</th>
                    <th class="sorting" tabindex="0" aria-controls="tblLocationInfo" rowspan="1" colspan="1" aria-label="FromDate: activate to sort column ascending" style="width: 762px;">FromDate</th>
                    <th class="sorting" tabindex="0" aria-controls="tblLocationInfo" rowspan="1" colspan="1" aria-label="ToDate: activate to sort column ascending" style="width: 762px;">ToDate</th>
                </tr>
            </thead>
            <tfoot>
                <tr></tr>
                <tr></tr>
                <tr></tr>
                <tr></tr>
                <tr></tr>
                <tr></tr>
                <tr></tr>
                <tr>
                    <th rowspan="1" colspan="1"><input name="Inventory" data-index="0" class="form-control js-footer-search" type="text" placeholder="Search"></th>
                    <th rowspan="1" colspan="1"><input name="FromDate" data-index="1" class="form-control js-footer-search" type="text" placeholder="Search"></th>
                    <th rowspan="1" colspan="1"><input name="ToDate" data-index="2" class="form-control js-footer-search" type="text" placeholder="Search"></th>
                </tr>
            </tfoot>
            <tbody>
                <tr role="row" class="odd">
                    <td>Main Location</td>
                    <td>02 Jul 2018 11:11:50:023</td>
                    <td>02 Jul 2018 11:54:13:760</td>
                </tr>
                <tr role="row" class="even">
                    <td>Main Location</td>
                    <td>02 Jul 2018 11:12:20:710</td>
                    <td>02 Jul 2018 12:13:13:867</td>
                </tr>
                <tr role="row" class="odd">
                    <td>Main Location</td>
                    <td>02 Jul 2018 11:13:22:083</td>
                    <td>02 Jul 2018 12:20:28:553</td>
                </tr>
                <tr role="row" class="even">
                    <td>InventoryLine</td>
                    <td>02 Jul 2018 11:50:56:527</td>
                    <td>02 Jul 2018 11:57:10:043</td>
                </tr>
                <tr role="row" class="odd">
                    <td>Main Location</td>
                    <td>02 Jul 2018 11:27:16:760</td>
                    <td>02 Jul 2018 12:27:40:277</td>
                </tr>
                <tr role="row" class="even">
                    <td>Main Location</td>
                    <td>02 Jul 2018 11:55:31:263</td>
                    <td>02 Jul 2018 12:33:09:467</td>
                </tr>
                <tr role="row" class="odd">
                    <td>InventoryLine</td>
                    <td>02 Jul 2018 12:14:14:043</td>
                    <td>02 Jul 2018 12:37:39:203</td>
                </tr>
                <tr role="row" class="even">
                    <td>Main Location</td>
                    <td>02 Jul 2018 12:23:47:223</td>
                    <td>02 Jul 2018 12:56:50:997</td>
                </tr>
                <tr role="row" class="odd">
                    <td>Main Location</td>
                    <td>02 Jul 2018 12:24:17:920</td>
                    <td>02 Jul 2018 13:06:17:873</td>
                </tr>
                <tr role="row" class="even">
                    <td>InventoryLine</td>
                    <td>02 Jul 2018 12:36:34:623</td>
                    <td>02 Jul 2018 13:23:59:467</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

For further reference, here is the link to the solution on https://jsfiddle.net/ksyfwn5m/2/

Answer №1

Consider applying the fixed property to your th element for optimal results

HTML:

<thead id="header-fixed"></thead>

CSS:

#header-fixed {
    position: fixed;

}

Answer №2

When utilizing the dataTable, it comes with a built-in feature for fixing headers that can solve your issue.

$('#example').DataTable( { fixedHeader: true } );

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

Filtering dynamically generated table rows using Jquery

I'm currently working on a project that involves filtering a dynamic table based on user input in a search bar. The table contains information such as name, surname, phone, and address of users. Using jQuery, I have created a form that dynamically ad ...

rectangle/positionOffset/position().top/any type of element returning a position value of 0 within the container

While the height/position of the container is accurately displayed, attempting to retrieve the top position (or any position) of containing elements yields a return value of 0. Additionally, using .getBoundingClientRect() results in all values (top, left, ...

Mastering the use of SVG icons in Angular 4+: A comprehensive guide

I have an icon.svg file with a collection of icons that I would like to use in my app, similar to how we display material icons. Does anyone have any ideas on how to include the file in the app and use these icons? I've tried looking for solutions a ...

The searchbar is failing to load the requested information through AJAX

I'm encountering an issue that I just can't seem to pinpoint. The goal is for the page to initially load all data, and upon hitting search, only display the requested data without refreshing the page. (Ideally, I would like to be able to modify ...

I am experiencing issues with the functionality of my ajax form request

I am trying to display an alert message on my page but it doesn't seem to be working. Below is the code from my view: $.ajax({ url :"<?php echo base_url();? >booking/dispatch_challan/DispatchChallanController/createDispatchChallan", ...

Securing Communication with HTTPS in Express JS

After purchasing an AlphaSSL from a hosting provider, I received the following files: domain.csr.crt domain.interCert.crt domain.PKCS7Cert.crt domain.rootCert.crt domain.X509Cert.crt However, in order to enable HTTPS on Node.JS using Express, I am aware ...

Active Admin: Maintaining the top menu navigation bar while adding a fresh custom page

I am currently managing an active admin panel which involves several models, including a custom one. I am looking to maintain the top navbar when redirecting to a new page within the admin panel. Main Page: https://i.stack.imgur.com/USov1.png Custom Page ...

Node.js: Capturing requests and responses for console logging

I'm currently working on a Hapi server using Good to log all requests and responses to the console. I've been able to successfully log responses but I'm facing issues with logging the body of the response, and for requests, I'm not gett ...

Alter the value of a key within a JSON object at a specific nested level using Node.js or JavaScript

I am attempting to swap out a specific value in the JSON file. Let's say the JSON data provided below: sample.json let sample={ "yuiwedw":{ "id":"yuiwedw", "loc": "ar", "body":{ "data":"we got this", "loc":"ar", "system":{ ...

Node.js Express refuses to serve .js files with absolute URLs

I have encountered a perplexing issue with two files located in /public/widget, namely help.html and help.js http://localhost:8084/widget/help.html When entered into the address bar, it functions normally However, when attempting to access http://local ...

Having trouble retrieving the toDataURL data from a dynamically loaded image source on the canvas

Currently, I am working on a project that involves a ul containing li elements with images sourced locally from the "/images" folder in the main directory. <section class="main"> <ul id="st-stack" class="st-stack-raw"> ...

Application utilizing electron technology featuring various tabbed browsing windows connecting to secure websites

Currently, I am in the process of developing my own unique version of a platform similar to using Electron technology. The objective for this app is to provide users with the ability to view multiple URLs simultaneously, such as and , through a tabbed i ...

Guide to implementing an AJAX loader during page load in PHP

Can anyone assist me in creating a page loader for when the page is loading? I am working on implementing a PayPal payment system. I have noticed that some pages display a loader (ajax loader) while directing to the PayPal gateway. I also want to add an ...

Enclose elements in a div using a jQuery each function

I have successfully parsed data from an XML feed to JSON, but now I want to wrap each part of the data within a div. Here is the code snippet I am working with: var newsDiv = $('<div class="news-feed">').appendTo($("body")); $(release ...

Issue with Angular Factory not being invoked

I am currently using a tutorial to create a MEAN app with Google Maps. However, I have encountered an issue where the map is not appearing on the page. Surprisingly, there are no errors in the browser console and even when debugging with node-inspector, I ...

Preventing pageup/pagedown in Vuetify slider: Tips and tricks

I am currently using Vuetify 2.6 and have integrated a v-slider into my project. Whenever the user interacts with this slider, it gains focus. However, I have assigned PageUp and PageDown keys to other functions on the page and would like them to continue ...

Issue with Ajax submit button failing to pass ID

I am encountering an issue while trying to update data using bootstrap modal. When the CGridView selectionChanged event is triggered, it should call a function to display a modal dialog form and populate the form with the selected data. Below is my CGrid ...

Utilize the Vue-Chartjs plugin registration for a specific chart component only

I am currently working with a chart component in Vue 3 using the composition API. My goal is to incorporate the datalabels plugin into this specific chart only. Upon attempting to register the plugin outside of the chart's data or options, I noticed ...

Selenium, Geckodriver, and Firefox are having issues loading jQuery

Currently, I am in the process of writing unit tests for a Django project. However, I have encountered an issue where jQuery is not loading when Selenium loads up the page. This error message shows up in the geckodriver.log file: JavaScript error: http:/ ...

Divide the number by the decimal point and then send it back as two distinct parts

Let's tackle a challenging task. I have a price list that looks like this: <span class="price">10.99€/span> <span class="price">1.99€/span> My goal is to transform it into the following format: <span class="price">10< ...