JQuery-based code for fixed table headers (IE6, 7, and 8 passed the test)

Source: Internet
Author: User

A list needs to be displayed during project creation, but the header must be frozen when there is too much data to be rolled, so I wrote the following script (I once found the corresponding script on the Internet, but it is not ideal, so I wrote it myself. But currently, because the project only uses the freeze of the header, you do not need to specify column freezing, so currently you can only calculate an incomplete script, but generally you only need to freeze the header to use it). Now let's take a look:


In this way, the table header is frozen and the content of the following table can be freely rolled.
Check the Code:
// Extension of the CloneTableHeader Method for jquery
Copy codeThe Code is as follows:
JQuery. fn. CloneTableHeader = function (tableId, tableParentDivId ){
// Obtain the DIV of the frozen header. If the DIV already exists, remove it.
Var obj = document. getElementById ("tableHeaderDiv" + tableId );
If (obj ){
JQuery (obj). remove ();
}
Var browserName = navigator. appName; // obtain the browser information, which is used by the following code to differentiate the browser
Var ver = navigator. appVersion;
Var browserVersion = parseFloat (ver. substring (ver. indexOf ("MSIE") + 5, ver. lastIndexOf ("Windows ")));
Var content = document. getElementById (tableParentDivId );
Var scrollWidth = content. offsetWidth-content. clientWidth;
Var tableOrg = jQuery ("#" + tableId); // obtain table content
Var table = tableOrg. clone (); // clone table content
Table. attr ("id", "cloneTable ");
// Note: the header to be frozen should be placed in thead.
Var tableHeader = jQuery (tableOrg). find ("thead ");
Var tableHeaderHeight = tableHeader. height ();
TableHeader. hide ();
Var colsWidths = jQuery (tableOrg). find ("tbody tr: first td"). map (function (){
Return jQuery (this). width ();
}); // Dynamically obtain the width of each column
Var tableCloneCols = jQuery (table). find ("thead tr: first td ")
If (colsWidths. size ()> 0) {// assign a value to the frozen header width based on the browser (mainly to distinguish IE8)
For (I = 0; I <tableCloneCols. size (); I ++ ){
If (I = tableCloneCols. size ()-1 ){
If (browserVersion = 8.0)
TableCloneCols. eq (I). width (colsWidths [I] + scrollWidth );
Else
TableCloneCols. eq (I). width (colsWidths [I]);
} Else {
TableCloneCols. eq (I). width (colsWidths [I]);
}
}
}
// Create a DIV container for freezing the header and set the attributes
Var headerDiv = document. createElement ("div ");
HeaderDiv. appendChild (table [0]);
JQuery(headerDiv).css ("height", tableHeaderHeight );
JQuery(headerDiv).css ("overflow", "hidden ");
JQuery(headerDiv).css ("z-index", "20 ");
JQuery(headerDiv).css ("width", "100% ");
JQuery (headerDiv). attr ("id", "tableHeaderDiv" + tableId );
JQuery (headerDiv). insertBefore (tableOrg. parent ());
}

The above is the complete code. Now let's take a look at how to use it:
Add the following script to the page.
Copy codeThe Code is as follows:
<Script type = "text/javascript">
JQuery (function (){
JQuery. fn. CloneTableHeader ("tab1", "div1 ");
});
</Script>

In this way, the ID of the input table and the DIV where the table is located is OK. Note that the table header to be frozen must be placed in thead; otherwise, the table cannot be frozen.
The preceding Code passes the IE6, 7, and 8 tests. FF and chrome may cause inaccurate header width.
Package and download specific code: Click here to download
PS: If you find it helpful, please recommend it. Thank you!
Author: Yun-yo
Source: http://kyo-yo.cnblogs.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.