/* * Lock the header and Column * * Parameter Definition * Table-table element or table ID to be locked * FreezeRowNum-number of rows to be locked. If the row is not locked, set it to 0. * FreezeColumnNum-number of the first few columns to be locked. If the column is not locked, set it to 0. * Width-the scroll area width of the table * Height-the height of the scrolling area of the table */ Function freezeTable (table, freezeRowNum, freezeColumnNum, width, height ){ If (typeof (freezeRowNum) = 'string ') FreezeRowNum = parseInt (freezeRowNum) If (typeof (freezeColumnNum) = 'string ') FreezeColumnNum = parseInt (freezeColumnNum) Var tableId; If (typeof (table) = 'string '){ TableId = table; Table = $ ('#' + tableId ); } Else TableId = table. attr ('id '); Var divTableLayout =$ ("#" + tableId + "_ tableLayout "); If (divTableLayout. length! = 0 ){ DivTableLayout. before (table ); DivTableLayout. empty (); } Else { Table. after ("<div id = '" + tableId + "_ tableLayout 'style = 'overflow: hidden; height:" + height + "px; width: "+ width +" px; '> </div> "); DivTableLayout = $ ("#" + tableId + "_ tableLayout "); } Var html = ''; If (freezeRowNum> 0 & freezeColumnNum> 0) Html + = '<div id = "' + tableId + '_ tableFix" style = "padding: 0px;"> </div> '; If (freezeRowNum> 0) Html + = '<div id = "' + tableId + '_ tableHead" style = "padding: 0px;"> </div> '; If (freezeColumnNum> 0) Html + = '<div id = "' + tableId + '_ tableColumn" style = "padding: 0px;"> </div> '; Html + = '<div id = "' + tableId + '_ tableData" style = "padding: 0px;"> </div> '; $ (Html). appendTo ("#" + tableId + "_ tableLayout "); Var divTableFix = freezeRowNum> 0 & freezeColumnNum> 0? $ ("#" + TableId + "_ tableFix"): null; Var divTableHead = freezeRowNum> 0? $ ("#" + TableId + "_ tableHead"): null; Var divTableColumn = freezeColumnNum> 0? $ ("#" + TableId + "_ tableColumn"): null; Var divTableData =$ ("#" + tableId + "_ tableData "); DivTableData. append (table ); If (divTableFix! = Null ){ Var tableFixClone = table. clone (true ); TableFixClone. attr ("id", tableId + "_ tableFixClone "); DivTableFix. append (tableFixClone ); } If (divTableHead! = Null ){ Var tableHeadClone = table. clone (true ); TableHeadClone. attr ("id", tableId + "_ tableHeadClone "); DivTableHead. append (tableHeadClone ); } If (divTableColumn! = Null ){ Var tableColumnClone = table. clone (true ); TableColumnClone. attr ("id", tableId + "_ tableColumnClone "); DivTableColumn. append (tableColumnClone ); } $ ("#" + TableId + "_ tableLayout table" ..css ("margin", "0 "); If (freezeRowNum> 0 ){ Var HeadHeight = 0; Var ignoreRowNum = 0; $ ("#" + TableId + "_ tableHead tr: lt (" + freezeRowNum + ")"). each (function (){ If (ignoreRowNum> 0) IgnoreRowNum --; Else { Var td = $ (this). find ('td: first, th: first '); HeadHeight + = td. outerHeight (true ); IgnoreRowNum = td. attr ('rowspan '); If (typeof (ignoreRowNum) = 'undefined ') IgnoreRowNum = 0; Else IgnoreRowNum = parseInt (ignoreRowNum)-1; } }); HeadHeight + = 2; DivTableHead.css ("height", HeadHeight ); DivTableFix! = Null & divTableFix.css ("height", HeadHeight ); } If (freezeColumnNum> 0 ){ Var ColumnsWidth = 0; Var ColumnsNumber = 0; $ ("#" + TableId + "_ tableColumn tr: eq (" + freezeRowNum + ")"). find ("td: lt (" + freezeColumnNum + "), th: lt (" + freezeColumnNum + ")"). each (function (){ If (ColumnsNumber> = freezeColumnNum) Return; ColumnsWidth + = $ (this). outerWidth (true ); ColumnsNumber + = $ (this). attr ('colspan ')? ParseInt ($ (this). attr ('colspan '): 1; }); ColumnsWidth + = 2; DivTableColumn.css ("width", ColumnsWidth ); DivTableFix! = Null & divTableFix.css ("width", ColumnsWidth ); } DivTableData. scroll (function (){ DivTableHead! = Null & divTableHead. scrollLeft (divTableData. scrollLeft ()); DivTableColumn! = Null & divTableColumn. scrollTop (divTableData. scrollTop ()); }); DivTableFix! = Null & divTableFix.css ({"overflow": "hidden", "position": "absolute", "z-index": "50 "}); DivTableHead! = Null & divTableHead.css ({"overflow": "hidden", "width": width-17, "position": "absolute", "z-index ": "45 "}); DivTableColumn! = Null & divTableColumn.css ({"overflow": "hidden", "height": height-17, "position": "absolute", "z-index ": "40 "}); DivTableData.css ({"overflow": "scroll", "width": width, "height": height, "position": "absolute "}); DivTableFix! = Null & divTableFix. offset (divTableLayout. offset ()); DivTableHead! = Null & divTableHead. offset (divTableLayout. offset ()); DivTableColumn! = Null & divTableColumn. offset (divTableLayout. offset ()); DivTableData. offset (divTableLayout. offset ()); } /* * Adjust the width and height of the locked table. This function is called in the resize event. * * Parameter Definition * Table-table element or table ID to be locked * Width-the scroll area width of the table * Height-the height of the scrolling area of the table */ Function adjustTableSize (table, width, height ){ Var tableId; If (typeof (table) = 'string ') TableId = table; Else TableId = table. attr ('id '); $ ("#" + TableId + "_ tableLayout"). width (width). height (height ); $ ("#" + TableId + "_ tableHead"). width (width-17 ); $ ("#" + TableId + "_ tableColumn"). height (height-17 ); $ ("#" + TableId + "_ tableData"). width (width). height (height ); } Function pageHeight (){ If ($. browser. msie ){ Return document. compatMode = "CSS1Compat "? Document.doc umentElement. clientHeight: document. body. clientHeight; } Else { Return self. innerHeight; } }; // Return the current page width Function pageWidth (){ If ($. browser. msie ){ Return document. compatMode = "CSS1Compat "? Document.doc umentElement. clientWidth: document. body. clientWidth; } Else { Return self. innerWidth; } }; $ (Document). ready (function (){ Var table = $ ("table "); Var tableId = table. attr ('id '); Var freezeRowNum = table. attr ('freezerownum '); Var freezeColumnNum = table. attr ('freezecolumnnum '); If (typeof (freezeRowNum )! = 'Undefined' | typeof (freezeColumnNum )! = 'Undefined '){ FreezeTable (table, freezeRowNum | 0, freezeColumnNum | 0, pageWidth (), pageHeight ()); Var flag = false; $ (Window). resize (function (){ If (flag) Return; SetTimeout (function (){ AdjustTableSize (tableId, pageWidth (), pageHeight ()); Flag = false; },100 ); Flag = true; }); } }); |