Tips for adjusting column positions in a table while maintaining a fixed header and utilizing both horizontal and vertical scrolling

Is there a way to display a table that scrolls both horizontally and vertically, with the header moving horizontally but not vertically while the body moves in both directions? I have been able to shift the position of the columns, however, I am struggling to keep the header fixed during vertical scroll as it also moves up.

CSS:

<style>
    table, td, th {
       border: 1px solid black;
    }
    table {
       border-collapse: collapse;
    }
   th, td {
       padding: 8px;
       text-align: left;
    }
</style>

<style>
    .alt tr:nth-child(odd) {
       background: #CCECFF;
    }

    .altfp tr:nth-child(odd) {
        background: #CCFF99;
    } 

</style>
<style>
    .scroll-table-outer {height: 300px; overflow-x: scroll;}
    .scroll-table, td{border-collapse:collapse; border:1px solid #777; min-width: 110px;}
</style>

SCRIPTS:

<script src="https://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<script src="./js/jquery-1.12.4.js"></script>
<script src="./js/dragtable.js" type="text/javascript" ></script>

HTML:

<div class="scroll-table-outer">
    <table class="draggable forget-ordering" id="details" width="90%">
        <thead>
            <tr>
                <th class="part col1">Part Number</th>
                <th class="replaces col2">Replaces</th> 
                <th class="replacedby col3">Replaced_By</th>
                <th class="site col4">Site</th>
                <th class="brand col5">Brand</th>
                <th class="com col6">Commodity</th>
                <th class="owner col7">Owner</th>
                <th class="desc col8">Description</th>
                <th class="desc2 col9">Description2</th>
              ##<th class="mdqty">MD Qty Per</th>
                <th class="mdpn col10">MD PN</th>
                <th class="mdtrackerpn coll1">MD Tracker PN</th>
                <th class="price coll2">YE Price Var Savings </th>
                <th class="plan coll3">YE Plan Spend K</th>
                <th class="spend coll4">YE Spend K</th>
                <th class="base coll5">YE Adj 4Q Base Spend</th>
                <th class="price coll6">1Q Price Var Savings K</th>
                <th class="plan coll7">1Q Plan Spend K</th>
                <th class="spend coll8">1Q Spend K</th>
                <th class="base coll9">1Q Adj 4Q Base Spend</th>
                <th class="price coll10">2Q Price Var Savings K</th>
                <th class="plan cola">2Q Plan Spend K</th>
                <th class="spend colb">2Q Spend K</th>
                <th class="base colc">2Q Adj 4Q Base Spend</th>
                <th class="price cold">3Q Price Var Savings K</th>
                <th class="plan cole">3Q Plan Spend K</th>     
                <th class="spend colf">3Q Spend K</th>
                <th class="base colg">3Q Adj 4Q Base Spend</th>
                <th class="price col1a">4Q Price Var Savings K</th>
                <th class="plan col1b">4Q Plan Spend K</th>
                <th class="spend col1c">4Q Spend K</th>
                <th class="base col1d">4Q Adj 4Q Base Spend</th>
           </tr>
      </thead>

      #if ($emptyPartListSearchResult=="true")
        <p><strong>No results found for specified criteria.</strong></p>
      #end

     #if($request.getSession().getAttribute("run") == "tp")
       <tbody class="alt tg draggable"> 
     #elseif($request.getSession().getAttribute("run") == "tfp")
       <tbody class="altfp tg">
     #end

     #set ($cnt = 0 )                   
     #foreach($pl in $rplist)
     <tr> 
         <td class="part col1" >$pl.partNum</a></td>
         <td class="replaces col2" >$pl.replaces</td>
         <td class="replacedby col3" >$pl.replacedBy</td>
         <td class="site col4" >$pl.site</td>
         <td class="brand col5">$pl.brand</td>
         <td class="com col6">$pl.commodity</td>
         <td class="owner col7">$pl.owner</td>  
         <td class="desc col8">$pl.description</td> 
         <td class="desc2 col9">$pl.description2</td>
         <td class="mdpn col10">$pl.mdPN</td>
         <td class="mdtrackerpn coll1">$pl.mdTrackerPn</td>     

         // more code here...
       
    </tr>

    #set ( $cnt = $cnt + 1 )
    #end
    <tr>
      <td class="part">Totals</td>
      // more rows here...
   </tr>
 </tbody>       
</table>    

Answer №1

table{border-collapse: collapse; border-spacing: 0;}
table,td,th{border: 1px solid #ccc;}
thead{background: #fff;}
td,th{padding: 4px;}
<html>
    <head>
    </head>
    <body>
    <table id="customers">
      <thead>  
          <tr>
            <th>Company</th>
            <th>Contact</th>
            <th>Country</th>
          </tr>
      </thead>
      <tbody>
      <tr>
        <td>Alfreds Futterkiste</td>
        <td>Maria Anders</td>
        <td>Germany</td>
      </tr>
      <tr>
        <td>Centro comercial Moctezuma</td>
        <td>Francisco Chang</td>
        <td>Mexico</td>
      </tr>
      <tr>
        <td>Ernst Handel</td>
        <td>Roland Mendel</td>
        <td>Austria</td>
      </tr>
      <tr>
        <td>Island Trading</td>
        <td>Helen Bennett</td>
        <td>UK</td>
      </tr>
      <tr>
        <td>Laughing Bacchus Winecellars</td>
        <td>Yoshi Tannamuri</td>
        <td>Canada</td>
      </tr>
      <tr>
        <td>Magazzini Alimentari Riuniti</td>
        <td>Giovanni Rovelli</td>
        <td>Italy</td>
      </tr>
        // Additional rows here
    </body> 
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/floatthead/2.0.3/jquery.floatThead.js"></script> 
    <script>
    var $table = $('#customers');
    $table.floatThead();
    </script>
 </html>

to enable drag columns, I implemented a solution that involves destroying the header responsible for dragging

$(document).ready(function(){
   var table = $('#customers');
   table.floatThead();
   $('.destroy').click(function(){
       table.floatThead('destroy');
       $('th').prepend('<i class="handle"></i>');
       table.dragtable({
           dragHandle: '.handle',
           maxMovingRows: 1
       });
   });
});

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

Having trouble with the $.get function in AJAX with Rails 4? It seems

After working with Rails for a few years, I decided to dip my toes into the world of AJAX. With a Rails 4 app in hand, I'm currently testing out some functions. My end goal is to reload a partial on the edit page located at app/views/stories/edit.html ...

Ember 2: Display a loading message only if the IDs were part of the initial response

I frequently use the following code snippet in my projects: {{#each model.posts as |post|}} <div>post.title</div> {{else}} <div>Loading the posts...</div> {{/each}} However, I sometimes face uncertainty regarding whether t ...

Smoothly transition between the new and existing child elements in a React component with a

Currently, I am utilizing a React component that renders a child element through props.children. The content within this child element varies dynamically. I am looking for the most effective method to smoothly transition (fade out the old element and fad ...

Jquery script that utilizes the Steam WebAPI to return data

I'm encountering an issue with a script I found on github. I added value.appid myself, thinking it was logical, but I believe that data.response.games contains values for all my games. How can I either print or view what is defined? I would like to ...

Implementing a feature to add selected checkboxes and remove unselected checkboxes with PHP and MySQL

I am currently working with 3 tables in my database: product, category, and product_category. I am in the process of creating a form that allows users to edit (insert/delete) categories for a specific product. My challenge lies in inserting an array of che ...

Run code snippets within summaries

I am seeking assistance with JQuery. Currently, I have checkboxes populated in the following manner: <span class="checked"> <input class="cbid" type="checkbox" name="cb1"> </span> <span > <input class="cbid" type="checkbox" ...

Modifying the default label for each bubble on a bubble chart with chartjs-plugin-datalabels

Is there a way to add labels to each bubble in the bubble chart using chartjs-plugin-datalabels? For every bubble, I'd like to display the label property of each object within the data.dataset array, such as "Grapefruit" or "Lime". Currently, I'm ...

Understanding the method of recovering a nested promise

I am facing an issue with returning the result parameter from the getColumn function. Despite my attempts, it keeps logging as undefined. The connection function establishes a connection to a SQL database and retrieves a data set through a query. Is ther ...

Angular 4: Implementing a Re-login Dialog/Modal Using Interceptors

Issue Description I recently started working with Angular 4 and I am facing a challenge in handling user re-logging when the token expires. Let's Dive Into the Code I have implemented a response intercepter that checks for a 401 error in the respon ...

Combine the animations of multiple elements in a queue using jQuery

I have a dilemma with the animation of several elements. Rapidly navigating back and forth over the submenu seems to cause a queuing issue, potentially leading to malfunction. I've attempted using stop(), delay(), :animated without success. Any sugge ...

Having trouble with video playback in the HTML5 media player on Codeigniter platform

I'm attempting to incorporate a video into my web application using the HTML5 media player within the Codeigniter framework. Below is the code snippet I have been working on: public function view($settings, Screen $screen) { ...

Ways to create diagonal or vertical table breaks using CSS

If you have a <table> that is very tall or wide and you want it to wrap for screen display, how can this be achieved using CSS? For example, a table that is 300% page height and 30% page width would be divided into three parts. # # # # # # beco ...

Formatting Dates in Node.js

Can someone assist me in understanding how to properly format the print date shown below? Thu Sep 06 2018 18:18:26 GMT+0530 I attempted to use console.log(new Date()) However, the output generated was 2018-09-06T12:48:25.776Z I am unsure of how to co ...

Utilizing Node Mailer and Sendgrid to send emails in an Angular MEAN stack project with the angular-fullstack framework generated by Yeoman

Trying to figure out the best location for the code responsible for sending an email through a contact form in my Angular App using angular-fullstack MEAN stack from Yeoman. I managed to implement the email sending logic in the app.js file on the server s ...

Having trouble loading script files with JSDOM in NodeJS?

I'm currently experimenting with loading an HTML page in jsdom to generate graphs, but I'm facing challenges in getting the JavaScript to execute. Here's the HTML page I'm trying to load, which simply renders a basic graph: <html&g ...

Encountering a TypeScript error when using Redux dispatch action, specifically stating `Property does not exist on type`

In my code, there is a redux-thunk action implemented as follows: import { Action } from "redux"; import { ThunkAction as ReduxThunkAction } from "redux-thunk"; import { IState } from "./store"; type TThunkAction = ReduxThunk ...

Utilizing Element IDs for JQuery Tab Implementation

Having two buttons and two divs with paragraphs, I want to create tabs without adding new classes or IDs. Can I achieve tab switching using the existing IDs that end with "_one"? For instance: The first button has the ID "tab_button_one" and the first di ...

Display JSON values in sequence using Material-UI animations

I have received an array of JSON data from the API that looks like this: "fruits": [ { "id": "1", "fruit": "APPLE", }, { "id": "2", "fruit": ...

Navigating through a JSON document using jQuery

I've been attempting to iterate through a JSON string (located in a separate file named language.json) but I can't seem to make it work { "language": [ { "name": "English Languages", "values": ["English 1", "English 2", " ...

Converting PHP variables to JavaScript using AJAX and XML communication

In order to gain a deeper understanding, I am determined to tackle this task without relying on jQuery. This means I am willing to reinvent the wheel in order to fully comprehend how it functions. My research has led me to believe that AJAX is the key to a ...