Click here to Skip to main content
Licence CPOL
First Posted 14 Aug 2007
Views 61,849
Bookmarked 27 times

Freeze Panes like Excel

By | 1 Mar 2008 | Article
Freeze panes in an HTML table, cross browser using JavaScript.

Introduction

The script described in this article simply hides/unhides columns or rows using scrolling buttons, creating an Excel-like effect.

Background

There are already a few scripts around that demonstrate how to freeze an HTML table top row and first column.

Most of these scripts are specific to Internet Explorer, while a couple I have tried are cross browser compatible using CSS, but require the columns to be set to a specific width... for example, all column widths must be set to 100px wide.

I found this not to be ideal because the columns hide any content that is wider than 100px and there is no way of adjusting the column widths to view the hidden data via an onscreen control....not ideal for dynamically created data from a database.

Another solution I found was quite good but required a very large and complex amount of JavaScript code.

Using the code

The script simply hides/unhides columns or rows using scrolling buttons:

<button title="Up" onmousedown="upp();" onmouseup="upp(1);">Up</button>
<button title="Left" onmousedown="left();" 
        onmouseup="left(1);">&lt;&lt;</button>
<button title="Right" onmousedown="right();" 
        onmouseup="right(1);">&gt;&gt;</button>
<button title="Down" onmousedown="down();" onmouseup="down(1);">Dn</button>

Naturally, images can easily be used for a more attractive display.

Place the JavaScript function in either the head or body of your page. The function setUp() initializes the table the first time one of the buttons is clicked. Make sure to modify:

if(!myTable){myTable=document.getElementById("t1");

to match the ID of your table if it is not "t1". The script could be better optimised, but for demonstration, the code is easy enough to follow.

  var myRow=1;
  var myCol=1;
  var myTable;
  var noRows;
  var myCells,ID;
  
function setUp(){
    if(!myTable){myTable=document.getElementById("t1");}
     myCells = myTable.rows[0].cells.length;
    noRows=myTable.rows.length;

    for( var x = 0; x < myTable.rows[0].cells.length; x++ ) {
        colWdth=myTable.rows[0].cells[x].offsetWidth;
        myTable.rows[0].cells[x].setAttribute("width",colWdth-4);

    } 
}

function right(up){
    if(up){window.clearTimeout(ID);return;}
    if(!myTable){setUp();}

    if(myCol<(myCells)){
        for( var x = 0; x < noRows; x++ ) {
            myTable.rows[x].cells[myCol].style.display="";
        }
        if(myCol >1){myCol--;}

        ID = window.setTimeout('right()',100);
    }
}
  
function left(up){
    if(up){window.clearTimeout(ID);return;}
    if(!myTable){setUp();}

    if(myCol<(myCells-1)){
        for( var x = 0; x < noRows; x++ ) {
            myTable.rows[x].cells[myCol].style.display="none";
        }
        myCol++
        ID = window.setTimeout('left()',100);
        
    }
}
  
function down(up){
    if(up){window.clearTimeout(ID);return;}
    if(!myTable){setUp();}

    if(myRow<(noRows-1)){
            myTable.rows[myRow].style.display="none";
            myRow++    ;
        
            ID = window.setTimeout('down()',100);
    }    
}
 
function upp(up){
    if(up){window.clearTimeout(ID);return;}
    if(!myTable){setUp();}

    if(myRow<=noRows){
        myTable.rows[myRow].style.display="";
        if(myRow >1){myRow--;}
        ID = window.setTimeout('upp()',100);
    }    
}

See a working Freeze Pains Demo here.

Here is an updated version for better layout within a scrollable div, using custom horizontal and verticle scrollbars: demo.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Will66



Australia Australia

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralTable inside table problem Pinmemberchadok3:43 17 Jun '11  
Questionfreeze pane error PinmemberLim Tjie Siong, Indo0:12 10 Mar '11  
Generalcss help needed Pinmemberwebber1234568:38 3 Jan '11  
GeneralRe: css help needed PinmemberWill6617:59 3 Jan '11  
GeneralRe: css help needed Pinmemberwebber12345610:45 9 Jan '11  
GeneralRe: css help needed PinmemberWill6613:32 9 Jan '11  
GeneralRe: css help needed Pinmemberwebber12345611:11 10 Jan '11  
GeneralMy vote of 3 PinmemberNawaz M11:18 11 Oct '10  
Generalhorizontal scroll working only once pls help Pinmembersnigdha12341:24 12 Aug '10  
GeneralRe: horizontal scroll working only once pls help PinmemberWill663:16 12 Aug '10  
GeneralRe: horizontal scroll working only once pls help Pinmembersnigdha123423:09 12 Aug '10  
GeneralProblem when cells are merged Pinmemberamit121212421:45 2 Aug '10  
GeneralRe: Problem when cells are merged Pinmemberlubos190219:29 21 Sep '11  
GeneralUpdated: now in custom scrollable div PinmemberWill663:57 1 Mar '08  
GeneralRe: Updated: now in custom scrollable div PinmemberMarc Leger9:52 1 Mar '08  
AnswerMultiple Table Solution PinmemberEric Carter11:23 28 Feb '08  
GeneralRe: Multiple Table Solution PinmemberWill663:20 1 Mar '08  
GeneralAnother option (using JQuery) Pinmemberrubensr16:33 22 Aug '07  
GeneralPrint large html table PinmemberElyons0315:11 21 Aug '07  
GeneralTry CSS Pinmembermeteorbites20:22 20 Aug '07  
GeneralRe: Try CSS PinmemberWill6621:35 20 Aug '07  
The point is my offering is cross-browser compatible and minimalist.
The example you offer ONLY works in Internet Explorer because ONLY Internet Explorer allows "expressions" in CSS.
 
meteorbites wrote:
example above doesnt work with firefox but with slight modification it can be done.

 
I'm all for it, but I don't agree it could be done with "slight modification".
 

GeneralRe: Try CSS PinmemberLouisa Chen18:45 21 Aug '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 1 Mar 2008
Article Copyright 2007 by Will66
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid