/** * * 固定普通table的行和列 * * */ if (typeof TableFix == 'undefined') { TableFix = {}; }; /** * 固定表头和列([tablefix='headRow']表示平台中的表头) * * @param tableId * @param fixColNum 需要固定的列数 * @param width * @param height */ TableFix.fix = function(tableId, fixColNum, width, height) { if ($("#" + tableId + "_tableLayout").length != 0) { $("#" + tableId + "_tableLayout").before($("#" + tableId)); $("#" + tableId + "_tableLayout").empty(); } else { $("#" + tableId).after("
"); } $('
' + '
' + '
' + '
') .appendTo("#" + tableId + "_tableLayout"); var oldtable = $("#" + tableId); var tableFixClone = oldtable.clone(true); tableFixClone.attr("id", tableId + "_tableFixClone"); $("#" + tableId + "_tableFix").append(tableFixClone); var tableHeadClone = oldtable.clone(true); tableHeadClone.attr("id", tableId + "_tableHeadClone"); $("#" + tableId + "_tableHead").append(tableHeadClone); var tableColumnClone = oldtable.clone(true); tableColumnClone.attr("id", tableId + "_tableColumnClone"); $("#" + tableId + "_tableColumn").append(tableColumnClone); $("#" + tableId + "_tableData").append(oldtable); $("#" + tableId + "_tableLayout table").each(function() { $(this).css("margin", "0"); }); var headHeight = 0; $("#" + tableId + "_tableHead").find("[tablefix='headRow']").each(function (){ headHeight = headHeight + $(this).height(); }); $("#" + tableId + "_tableHead").css("height", headHeight); $("#" + tableId + "_tableFix").css("height", headHeight); var ColumnsWidth = 0; var ColumnsNumber = 0; $("#" + tableId + "_tableColumn tr:last td:lt(" + fixColNum + ")").each(function() { ColumnsWidth += $(this).outerWidth(true); ColumnsNumber++; }); ColumnsWidth += 2; if ($.browser.msie) { switch ($.browser.version) { case "7.0": if (ColumnsNumber >= 3) ColumnsWidth--; break; case "8.0": if (ColumnsNumber >= 2) ColumnsWidth--; break; } } $("#" + tableId + "_tableColumn").css("width", ColumnsWidth); $("#" + tableId + "_tableFix").css("width", ColumnsWidth); $("#" + tableId + "_tableData").scroll( function() { $("#" + tableId + "_tableHead").scrollLeft( $("#" + tableId + "_tableData").scrollLeft()); $("#" + tableId + "_tableColumn").scrollTop( $("#" + tableId + "_tableData").scrollTop()); }); $("#" + tableId + "_tableFix").css({ "overflow" : "hidden", "position" : "relative", "z-index" : "50", "background-color" : "Silver" }); $("#" + tableId + "_tableHead").css({ "overflow" : "hidden", "width" : width - 17, "position" : "relative", "z-index" : "45", "background-color" : "Silver" }); $("#" + tableId + "_tableColumn").css({ "overflow" : "hidden", "height" : height - 17, "position" : "relative", "z-index" : "40", "background-color" : "Silver" }); $("#" + tableId + "_tableData").css({ "overflow" : "auto", "width" : width, "height" : height, "position" : "relative", "z-index" : "35" }); if ($("#" + tableId + "_tableHead").width() > $( "#" + tableId + "_tableFix table").width()) { $("#" + tableId + "_tableHead").css("width", $("#" + tableId + "_tableFix table").width()); $("#" + tableId + "_tableData").css("width", $("#" + tableId + "_tableFix table").width() + 17); } if ($("#" + tableId + "_tableColumn").height() > $( "#" + tableId + "_tableColumn table").height()) { $("#" + tableId + "_tableColumn").css("height", $("#" + tableId + "_tableColumn table").height()); $("#" + tableId + "_tableData").css("height", $("#" + tableId + "_tableColumn table").height() + 17); } $("#" + tableId + "_tableFix").offset( $("#" + tableId + "_tableLayout").offset()); $("#" + tableId + "_tableHead").offset( $("#" + tableId + "_tableLayout").offset()); $("#" + tableId + "_tableColumn").offset( $("#" + tableId + "_tableLayout").offset()); $("#" + tableId + "_tableData").offset( $("#" + tableId + "_tableLayout").offset()); $("#" + tableId + "_tableFixClone").find("[name]").removeAttr("name"); $("#" + tableId + "_tableHeadClone").find("[name]").removeAttr("name"); $("#" + tableId + "_tableColumnClone").find("[name]").removeAttr("name"); $("#" + tableId + "_tableFixClone").find("[formtype]").removeAttr("formtype"); $("#" + tableId + "_tableHeadClone").find("[formtype]").removeAttr("formtype"); $("#" + tableId + "_tableColumnClone").find("[formtype]").removeAttr("formtype"); } /** * 还原改变后的表 * * @param tableId */ TableFix.restore = function(tableId) { var table = $("#" + tableId); var tableClone = table.clone(true); $("#" + tableId + "_tableLayout").after(tableClone); $("#" + tableId + "_tableLayout").remove(); }