Avatar of jhooie
jhooie
 asked on

Scrollable Table

How do you create scrollable tables in HTML?  I've tried placing the TBODY section in a DIV and setting "overflow-y: scroll."  This works until the user prints the page, when only the visible area of the table is printed.  Is this the only way or the preferred method for creating scrollable tables?  If not, what is the preferred method and if so, how can I print the entire table?  I've tried using "@media print" in the style sheet to override the overflow attribute, but the attribute is not inherited from the CSS.

Also, if at all possible, I would like to keep this in a single table with fixed headers so that I may use "display:table-header-group" to print the header at the top of each subsequent page.
HTML

Avatar of undefined
Last Comment
jhooie

8/22/2022 - Mon
knightEknight

Here is one way:


<HTML>
<BODY>
<CENTER>

<TABLE border='1' cellpadding='1' cellspacing='1'>
 <TR>
  <TH width='160' nowrap>Heading 1</th>
  <TH width='160' nowrap>Heading 2</th>
  <TH width='160' nowrap>Heading 3</th>
  <TH width='160' nowrap>Heading 4</th>
  <TH width='15'  comment='this is the scrollbar spacer'>&nbsp;</th>
 </tr>
 <TR>
  <TD colspan='5'><IFRAME name='myIFrame' id='myIFrame' src='myTable.htm' width='100%' height='123' border='0' frameborder='0' marginwidth='0' marginheight='0' ></iframe></td>
 </tr>
 <TR>
  <TH>Footer 1</th>
  <TH>Footer 2</th>
  <TH>Footer 3</th>
  <TH>Footer 4</th>
  <TH>&nbsp;</th>
 </tr>
</table>

</center>
</body>
</html>
knightEknight

Here's another:

<html>
<head>
<script language="javascript">
window.onload = init;
function init()
{
 var eTable = document.getElementById("headers");
 eTable.style.setExpression("width","document.all.data.offsetWidth");
}
</script>
<style>
table#headers, table#data {table-layout:fixed;background:#dda0dd;}
td, th {vertical-align:top;background:#ffffff;}
</style>
</head>
<body>
<table cellpadding="4" cellspacing="1" id="headers">
<col width="20%">
<col width="20%">
<col width="20%">
<col width="20%">
<tr>
<th>Test</th>
<th>bunk</th>
<th>burp</th>
<th>Hmm</th>
</tr>
</table>
<div style="width:100%;height:300px;overflow-y:auto;">
<table cellpadding="4" cellspacing="1" width="100%" id="data">
<col width="20%">
<col width="20%">
<col width="20%">
<col width="20%">
<tr>
<td>blah</td>
<td>bunk</td>
<td>to be or not to be</td>
<td>burp</td>
</tr>
<tr>
<td>blah</td>
<td>bunk</td>
<td>to be or not to be</td>
<td>burp</td>
</tr>
<tr>
<td>blah</td>
<td>bunk</td>
<td>to be or not to be</td>
<td>burp</td>
</tr>
<tr>
<td>blah</td>
<td>bunk</td>
<td>to be or not to be</td>
<td>burp</td>
</tr>
<tr>
<td>blah</td>
<td>bunk</td>
<td>to be or not to be</td>
<td>burp</td>
</tr>
<tr>
<td>blah</td>
<td>bunk</td>
<td>to be or not to be</td>
<td>burp</td>
</tr>
<tr>
<td>blah</td>
<td>bunk</td>
<td>to be or not to be</td>
<td>burp</td>
</tr>
<tr>
<td>blah</td>
<td>bunk</td>
<td>to be or not to be</td>
<td>burp</td>
</tr>
<tr>
<td>blah</td>
<td>bunk</td>
<td>to be or not to be</td>
<td>burp</td>
</tr>
<tr>
<td>blah</td>
<td>bunk</td>
<td>to be or not to be</td>
<td>burp</td>
</tr>
<tr>
<td>blah</td>
<td>bunk</td>
<td>to be or not to be</td>
<td>burp</td>
</tr>
<tr>
<td>blah</td>
<td>bunk</td>
<td>to be or not to be</td>
<td>burp</td>
</tr>
<tr>
<td>blah</td>
<td>bunk</td>
<td>to be or not to be</td>
<td>burp</td>
</tr>
</table>
</div>
</body>
</html>
ASKER CERTIFIED SOLUTION
szczepan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
apprenti

There is some material related to your question at
http://www.brainjar.com/dhtml/tablesort/
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
jhooie

ASKER
knightEknight,

I can't use IFrames (I don't want to put the table building logic in a separate page).  The second solution works (this is the solution I had before), but doesn't satisfy the printing requirements.  I wrote a function to be called from the onBeforePrint and onAfterPrint events of the body to toggle the overflow between auto and visible.  This will allow the entire table to be printed instead of just the area visible on the screen.  I still would like to be able to have a single table containing the header and the elements so that if the table spans multiple pages when printed, the header will appear at the top of each page.  Is this possible?

IE 5.5 or later, fixed width table and columns.
SOLUTION
SquareHead

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SquareHead

Oh yeah, forgot to mention:

Whatever is generating the table in the main page does not have to generate the table again in the child window -- it's already there in the opener window. So, if this is a big table, or you are using ASP / SQL to generate the table, then an added bonus with the innerHTML method is no waiting for the table to be regenerated!

SqHd
szczepan

did you try my code?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
yoscool

I have an extremely simple way of doing it.
You can see it in action at http://ufo.gamerzcore.com/
<iframe src="html file.htm" scrolling="yes" >
</iframe>
Extremely simple. And i love it.
geomon

Try this one...

<HTML>
 <HEAD>
  <TITLE>Scrolling Table Example</TITLE>
  <STYLE type="text/css">
   TABLE {border-collapse: collapse; table-layout: fixed}
   TH {background-color: silver; border: 1px solid black; border-bottom:
thick solid black}
   TD {border: 1px solid black; border-top: 0px}
  </STYLE>
 </HEAD>
 <BODY>
<SCRIPT language=JavaScript>
<!--
 function s(){
  var w = head.scrollLeft;
  data.scrollLeft = w;
 }
//-->
</SCRIPT>

<DIV id=head style="Z-INDEX: 3; LEFT: 150px; VISIBILITY: visible;
OVERFLOW-X: scroll; WIDTH: 500px; POSITION: absolute; TOP: 60px; HEIGHT:
330px" onscroll=s();>
<TABLE border=1>
  <TBODY>
  <TR align=middle>
    <TD noWrap width=100>Heading 1</TD>
    <TD noWrap width=100>Heading 2</TD>
    <TD noWrap width=100>Heading 3</TD>
    <TD noWrap width=100>Heading 4</TD></TR></TBODY></TABLE></DIV>
<DIV id=data style="OVERFLOW-Y: auto; LEFT: 150px; OVERFLOW-X: auto; WIDTH:
500px; POSITION: absolute; TOP: 90px; HEIGHT: 300px">
<TABLE width=400 border=1>
  <TBODY>
  <TR>
    <TD noWrap width=100>12.3435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR>
    <TD noWrap width=100>12.3435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR>
    <TD noWrap width=100>12.3435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR>
    <TD noWrap width=100>12.3435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR>
    <TD noWrap width=100>12.3435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR>
    <TD noWrap width=100>12.3435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR>
    <TD noWrap width=100>12.3435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR>
    <TD noWrap width=100>12.3435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR>
    <TD noWrap width=100>12.3435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR>
    <TD noWrap width=100>12.3435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR>
    <TD noWrap width=100>12.3435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR>
    <TD noWrap width=100>12.3435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR>
    <TD noWrap width=100>12.3435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR>
    <TD noWrap width=100>12.3435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR>
    <TD noWrap width=100>12.3435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR>
    <TD noWrap width=100>12.3435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR>
    <TD noWrap width=100>12.3435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR>
    <TD noWrap width=100>12.3435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR>
    <TD noWrap width=100>12.3435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR width="400">
    <TD noWrap width=100>12.34345435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR width="400">
    <TD noWrap width=100>12.5435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR width="400">
    <TD noWrap width=100>12.35</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR width="400">
    <TD noWrap width=100>12.34345435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR width="400">
    <TD noWrap width=100>12.34345435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR width="400">
    <TD noWrap width=100>12.34345435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR width="400">
    <TD noWrap width=100>12.34345435</TD>
    <TD width=100>43.345453</TD>
    <TD width=100>342.3545435</TD>
    <TD width=100>342.3545435</TD></TR>
  <TR width="400">
    <TD noWrap width=100>123434.5</TD>
    <TD noWrap width=100>56354.5453</TD>
    <TD noWrap width=100>34767.5435</TD>
    <TD noWrap width=100>342.3545435</TD></TR>
  <TR width="400">
    <TD noWrap width=100>1.24345435</TD>
    <TD noWrap width=100>4.453</TD>
    <TD noWrap width=100>3.423545435</TD>
    <TD noWrap width=100>342.3545435</TD></TR>
</TBODY>
</TABLE>
</DIV>

 </BODY>
</HTML>
COBOLdinosaur

This question has been classified abandoned. I will make a recommendation to the
moderators on its resolution in a week or two. I appreciate any comments
that would help me to make a recommendation.

<note>
Unless it is clear to me that the question has been answered I will recommend delete.  It is possible that a Grade less than A will be given if no expert makes a case for an A grade. It is assumed that any participant not responding to this request is no longer interested in its final disposition.
</note>

If the user does not know how to close the question, the options are here:
https://www.experts-exchange.com/help/closing.jsp


Cd&

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
SquareHead

I'd be (sort of) interested in it's final disposition...
jhooie

ASKER
szczepan gets the points for the fixed headers and SquareHead gets the assist for the trick printing solution.  Thanks to all.