DooIT
博客园
::
首页
::
新随笔
::
联系
::
订阅
::
管理
::
47 随笔 :: 16 文章 :: 1376 评论 :: 27 引用
WEB打印分页类(JS)
<
HTML
>
<
HEAD
>
<
TITLE
>
print
</
TITLE
>
<
meta
http-equiv
="content-type"
content
="text/html;charset=gb2312"
>
<
style
>
/**/
/*
*
* 打印相关
*/
@media print
{
}
{
.notprint
{
display
:
none
;
}
.PageNext
{
}
{
page-break-after
:
always
;
}
}
@media screen
{
}
{
.notprint
{
display
:
inline
;
cursor
:
hand
;
}
}
.text1
{
}
{
width
:
120px
;
overflow
:
hidden
;
text-overflow
:
ellipsis
;
}
.text2
{
}
{
width
:
80px
;
overflow
:
hidden
;
text-overflow
:
ellipsis
;
}
</
style
>
<
script
language
="javascript"
>
<!--
/**/
/*
*
** ==================================================================================================
** 类名:CLASS_PRINT
** 功能:打印分页
** 示例:
---------------------------------------------------------------------------------------------------
var pp = new CLASS_PRINT();
window.onload = function(){
pp.header = document.getElementById("tabHeader");
pp.content= document.getElementById("tabDetail");
pp.footer = document.getElementById("tabFooter");
pp.hideCols("5,7");
pp.hideRows("3,15");
pp.pageSize = 10;
}
<BODY onbeforeprint="pp.beforePrint()" onafterprint="pp.afterPrint()">
---------------------------------------------------------------------------------------------------
** 作者:ttyp
** 邮件:ttyp@21cn.com
** 日期:2006-11-10
** ==================================================================================================
*/
function
CLASS_PRINT()
{
this
.header
=
null
;
this
.content
=
null
;
this
.footer
=
null
;
this
.board
=
null
;
this
.pageSize
=
10
;
var
me
=
this
;
//
哈希表类
function
Hashtable()
{
this
._hash
=
new
Object();
this
.add
=
function
(key,value)
{
if
(
typeof
(key)
!=
"
undefined
"
)
{
if
(
this
.contains(key)
==
false
)
{
this
._hash[key]
=
typeof
(value)
==
"
undefined
"
?
null
:value;
return
true
;
}
else
{
return
false
;
}
}
else
{
return
false
;
}
}
this
.remove
=
function
(key)
{
delete
this
._hash[key];}
this
.count
=
function
()
{
var
i
=
0
;
for
(
var
k
in
this
._hash)
{i
++
;}
return
i;}
this
.items
=
function
(key)
{
return
this
._hash[key];}
this
.contains
=
function
(key)
{
return
typeof
(
this
._hash[key])
!=
"
undefined
"
;}
this
.clear
=
function
()
{
for
(
var
k
in
this
._hash)
{
delete
this
._hash[k];}
}
}
//
字符串转换为哈希表
this
.str2hashtable
=
function
(key,cs)
{
var
_key
=
key.split(
/
,
/
g);
var
_hash
=
new
Hashtable();
var
_cs
=
true
;
if
(
typeof
(cs)
==
"
undefined
"
||
cs
==
null
)
{
_cs
=
true
;
}
else
{
_cs
=
cs;
}
for
(
var
i
in
_key)
{
if
(_cs)
{
_hash.add(_key[i]);
}
else
{
_hash.add((_key[i]
+
""
).toLowerCase());
}
}
return
_hash;
}
this
._hideCols
=
this
.str2hashtable(
""
);
this
._hideRows
=
this
.str2hashtable(
""
);
this
.hideCols
=
function
(cols)
{
me._hideCols
=
me.str2hashtable(cols)
}
this
.isHideCols
=
function
(val)
{
return
me._hideCols.contains(val);
}
this
.hideRows
=
function
(rows)
{
me._hideRows
=
me.str2hashtable(rows)
}
this
.isHideRows
=
function
(val)
{
return
me._hideRows.contains(val);
}
this
.afterPrint
=
function
()
{
var
table
=
me.content;
if
(
typeof
(me.board)
==
"
undefined
"
||
me.board
==
null
)
{
me.board
=
document.getElementById(
"
divPrint
"
);
if
(
typeof
(me.board)
==
"
undefined
"
||
me.board
==
null
)
{
me.board
=
document.createElement(
"
div
"
);
document.body.appendChild(me.board);
}
}
if
(
typeof
(table)
!=
"
undefined
"
)
{
for
(
var
i
=
0
;i
<
table.rows.length;i
++
)
{
var
tr
=
table.rows[i];
for
(
var
j
=
0
;j
<
tr.cells.length;j
++
)
{
if
(me.isHideCols(j))
{
tr.cells[j].style.display
=
""
;
}
}
}
}
me.content.style.display
=
'';
me.header.style.display
=
'';
me.footer.style.display
=
'';
me.board.innerHTML
=
'';
}
this
.beforePrint
=
function
()
{
var
table
=
me.content;
if
(
typeof
(me.board)
==
"
undefined
"
||
me.board
==
null
)
{
me.board
=
document.getElementById(
"
divPrint
"
);
if
(
typeof
(me.board)
==
"
undefined
"
||
me.board
==
null
)
{
me.board
=
document.createElement(
"
div
"
);
document.body.appendChild(me.board);
}
}
if
(
typeof
(table)
!=
"
undefined
"
&&
this
.hideCols.length
>
0
)
{
for
(
var
i
=
0
;i
<
table.rows.length;i
++
)
{
var
tr
=
table.rows[i];
for
(
var
j
=
0
;j
<
tr.cells.length;j
++
)
{
if
(me.isHideCols(j))
{
tr.cells[j].style.display
=
"
none
"
;
}
}
}
}
//
/开始分页
var
pageSize
=
this
.pageSize;
var
head
=
me.header;
var
foot
=
me.footer;
var
page
=
new
Array();
var
rows
=
""
;
var
rowIndex
=
1
;
var
cp
=
0
;
var
tp
=
0
;
for
(i
=