Top section of a table repaired

It seems that the position fixed attribute is not working as expected in this scenario. I might be missing something.

I'm trying to keep the input boxes in the first row frozen when scrolling, but none of the solutions I've found seem to be effective.

Check out this link for more information

.table{
text-align:center;
overflow: auto;
    table{
        width: 95%;
        margin: auto;
        margin-top: 5px;
         }
    }
.table_scroll
 {
display:block;
height:500px;
overflow-y:scroll;
 }

<div class="table">
<table class="table_scroll">
<tbody>
<tr class="main_tr">
 <th class="input_col"><input title="empno" placeholder="empno" type="text" class="col_data" id="empno" autocomplete="off"></th>
 <th class="input_col"><input title="name" placeholder="name" type="text" class="col_data" id="name" autocomplete="off"></th>
 <th class="input_col"><input title="job" placeholder="job" type="text" class="col_data" id="job" autocomplete="off"></th>
 <th class="input_col"><input title="boss" placeholder="boss" type="text" class="col_data" id="boss" autocomplete="off"></th>
 <th class="input_col"><input title="hiredate" placeholder="hiredate" type="text" class="col_data" id="hiredate" autocomplete="off"></th>
 <th class="input_col"><input title="salary" placeholder="salary" type="text" class="col_data" id="salary" autocomplete="off"></th>
 <th class="input_col"><input title="comm" placeholder="comm" type="text" class="col_data" id="comm" autocomplete="off"></th>
 <th class="input_col"><input title="deptno" placeholder="deptno" type="text" class="col_data" id="deptno" autocomplete="off"></th>
</tr>

<tr id="row1" class="remove table_row">
  <td>7369</td>
  <td>SMITH</td>
  <td>CLERK</td>
  <td>7902</td>
  <td>1980-12-17</td>
  <td>800.00</td>
  <td></td>
  <td>20</td>
</tr>
<tr id="row2" class="remove table_row">
  <td>7370</td>
  <td>ALLEN</td>
  <td>CLERK</td>
  <td>7902</td>
  <td>1980-12-17</td>
  <td>800.00</td>
  <td></td>
  <td>20</td>
</tr>
</tbody>
</table>
</div>

Answer №1

Note: This solution is specifically for vertical scrolling and assumes that the table will fit horizontally. If horizontal scrolling is needed, additional jQuery code will be required to listen to scroll events and update styles accordingly.

Problem 1: Changing Column Widths

Solution 1: Use CSS to set table-layout: fixed and predefine the widths of each column.

Solution 2: Use jQuery to measure the columns dynamically, apply table-layout: fixed, and set the width of each column.

var $table = $('table');
var $tbody = $table.find('tbody');
var $ths = $table.find('th');
var $tds =  $table.find('tbody tr:first-child td'); //only need the first row
var widths = [];


//Measure the widths
$ths.each(function(i, cell) {
    var $cell = $(cell);
    widths.push(
        cell.clientWidth //don't use jquery's width() it's inaccurate in cases with bordercollapse.
        - parseFloat($cell.css('padding-right'))
        - parseFloat($cell.css('padding-left'))
    );
});

//Freeze the cells widths
$ths.each(function(i, cell) {
    $(cell).width(widths[i]);
});
$tds.each(function(i, cell) {
    $(cell).width(widths[i]);
});

Problem 2: Implementing Scrolling

Solution 1: Set the table, tbody, and thead to display: block. Position thead absolutely and set tbody to scroll.

table.freeze  {
    position: relative;
    display: block;
    table-layout: fixed;
}

table.freeze thead {
    position: absolute;
    left: 0;
    top: 0;
    display: block;
    background-color: #fff;
}
table.freeze tbody {
    height: 200px;
    overflow-y: auto;
    overflow-x: hidden;
    display: block;
}

Solution 2: Clone the thead into a separate table, position it absolutely within another wrapper. This approach may simplify other functionalities like making the header also stay visible during page scrolls or adding horizontal scrolling.

See It in Action Check out this working example. Make sure the result pane is wide enough to view the entire table.

http://jsfiddle.net/shindigg/232Vv/1/

Answer №2

Take a look at this code snippet that needs modification:

tr.table_row td {
    display:table-cell;   
}

Answer №3

Consider updating your CSS:

.table_scroll
 {
display:block;
height:500px;
overflow-y:auto;
 }

See it in action here: http://jsfiddle.net/r8WDb/6/

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

An AJAX request was dispatched to the identical PHP script

At the moment, I am working with a single PHP file named createNewForm.php. Within this file, there is a series of checkboxes that, when checked, should trigger an SQL query to repopulate a list within the same PHP file. For instance, if I check 'Ani ...

Turbolinks refusing to disregard page in Ruby on Rails application

Here is the front page view file ('Welcome/index.html.erb') : https://github.com/bfbachmann/Blog/blob/master/app/views/welcome/index.html.erb Current Rails Version: 4.2.6 The main issue at hand: I am currently struggling to display my THREE.js ...

Is there a way to include multiple div elements on a single page using React?

I am currently in the process of developing an e-commerce website and I am looking to display multiple items on the front end using React.js with Bootstrap as the CSS library. Below is the code snippet for my return div. How can I go about showcasing mul ...

Unable to fix the unhandled reference error

https://i.sstatic.net/cJdyl.jpgI'm currently working on a website design and running into an issue with the 'draw_images' function being referenced as undefined, even though I have already defined it. I suspect this error may be due to scopi ...

Smoothly scroll through parallax backgrounds

I've implemented a feature where an element moves in relation to scrolling using jQuery: $('#object').css('transform','translateY('+($(window).scrollTop()*.4)+'px)'); Here's the CSS: #object { width: ...

How to access a specific element within an ng-grid

My grid options include: $scope.ngOptions = { data: 'data', columnDefs: [ {field: 'Status', displayName: "Status", cellTemplate: selectTableTemplate, enableCellEdit: true}, {cellTemplate: &apos ...

Turning a string array into a basic array can be achieved through a few simple steps

While I am aware that this question has been posed multiple times, my scenario is slightly unique. Despite exhausting numerous methods, I have yet to discover a suitable workaround. $array = ["9","8","7","6","5"]; //result of javascript JSON.stringify() ...

Discovering the method to retrieve the information of the selected item in AngularJS

My table is using the ng-repeat in the <tr> element to load content dynamically from a JSON file. Each row has a unique ID in the table. I need a way to access the inner HTML of the respective row's ID column when it is clicked. Is there a solut ...

Troubles encountered with webserver authentication while attempting to view webserver material using UWP WebView

In my IoT Core app development project, I am faced with the challenge of displaying webpages from a remote Apache webserver. The WebView class method .Navigate() has been effective for accessing non-protected pages, but I am struggling to figure out how to ...

There seems to be a problem with the external JavaScript file not functioning

After dragging and dropping the .js file into my ASP.NET project, I am facing an issue where it remains unresponsive, even though the code works fine when used inline. This problem is occurring while using VS 2017. Here is a snippet of my code: <scrip ...

Issue with Displaying Full-Page Div

My HTML page consists of just one div. The body code is as follows: <div> </div> Here is the corresponding CSS: body { margin:0; height:100vh; width:100vh; } div { height:100vh; width:100vh; color:#000000; } I c ...

How can we alert users when data has been updated without the need for a page

The following code was authored by me: $('.actionButton').click(function(){ var buttonValue = $(this).val(); var userId = '<?php echo $_SESSION['id']; ?>'; console.log(userId); ...

Checking for the winner in a JavaScript tic-tac-toe game

Here is the code snippet for a tic-tac-toe game: $(".square").one("click", function() { if( gameOver == false ) { sq1 = $("#sq1").text(); // captures the value of squares after being clicked. sq2 = $("#sq2").text(); //and so on for all 9 squares } / ...

Leverage the power of EJS and Node JS by incorporating node modules into

I am having trouble importing my datetimepicker JavaScript and style sheet into my EJS file. Unfortunately, the CSS and JS files are not rendering properly when I run the code. Below is how my code is structured: In the EJS file: <html> <meta n ...

Steps to ensure your Bootstrap side menu spans the entire height of the page

Welcome to my side menu <template> <div class="p-3 text-bg-dark shadow"> <div class="dropdown"> <a href="#" class=" align-items-center text-white text-d ...

Is the Paypal button causing issues with the navigation bar?

I recently encountered an issue while trying to integrate the Paypal smart button into my first project. Whenever the Debit or Credit card button is clicked, the text above it overlaps with the navbar. I attempted to add extra space to the background image ...

What are the best tools to develop a browser-based 2D top-down soccer simulation?

I'm looking to create a 2D top-down soccer simulation game for web browsers using modern technologies and without the need for additional plugins like Flash or Silverlight, making it compatible with mobile devices as well. The game will be AI-controll ...

There is no need for updates as git is already current for some mysterious reason

As a newcomer to git, I've been trying to wrap my head around it but still struggling. Can someone help clarify this for me? My question pertains to the 'master' branch in git which contains the following code: const list = [ 'h&ap ...

Can the navbar brand be centered in Bootstrap 4 while also aligning some text to the left of the brand?

Just dipping my toes into website coding and Bootstrap is a whole new territory for me. I'm struggling with centering the brand in the navbar while keeping the text aligned to the left of it. So far, every attempt results in both elements being center ...

Issues arise when attempting to animate letters using jQuery and CSS

My goal is to create a dynamic animation effect on a string when a user hovers over it. This involves turning the string into an array and applying different animations to each letter. Although the animations are working perfectly, I'm facing one issu ...