function
abstractTable(){
this.id = null;
this.tableobj = null;
this.rowNum = 0;
this.colNum = 0;
this.header = [];
this.content = [];
this.currentClickRowID = 0;
this.getColNum =
function
(){
this.colNum = this.header.length;
return
this.colNum;
}
this.clearTable =
function
(){};
this.showHeader =
function
(){};
this.showContent =
function
(begin,
end
){};
this.showFoot =
function
(){};
this.allDataNum = 0;
this.displayNum = 10;
this.maxPageNum = 0;
this.currentPageNum =1;
this.groupDataNum = 10;
this.groupNum = 1;
this.paginationFromBeginToEnd =
function
(begin,
end
){}
this.first =
function
(){}
this.last =
function
(){}
this.prev =
function
(){}
this.next =
function
(){}
this.
goto
=
function
(){}
this.init =
function
(begin,
end
){}
}
function
tableTemplet(table_id){
abstractTable.call(this);
this.id = table_id;
}
function
table(options){
if
(!options){
return
;}
if
(!$.isPlainObject(options)){
return
;}
tableTemplet.call(this,options.tableId);
this.tableobj = $(
"#"
+this.id);
this.clearTable =
function
(){
this.tableobj.html(
" "
);
}
this.paginationFromBeginToEnd=
function
(x,y){
this.maxPageNum = Math.
ceil
(this.allDataNum/this.displayNum);
var
arrPage = [];
for
(
var
i= x;i<y;i++){
arrPage.push(this.content[i]);
}
return
arrPage;
}
this.showHeader =
function
(){
if
(this.header != null){
var
$thead
= $(
"<thead>"
),
$tr
= $(
"<tr>"
),
$th
;
for
(
var
i=0;i<this.colNum;i++){
$th
= $(
"<th>"
).html(this.header[i]);
$th
.appendTo(
$tr
);
}
$tr
.appendTo(
$thead
);
$thead
.appendTo(this.tableobj)
}
}
this.showContent =
function
(begin,
end
){
if
(this.content != null){
var
$tbody
= $(
"<tbody>"
),
$tr
,
$td
;
var
tempDaTa = this.paginationFromBeginToEnd(begin,
end
),
len = tempDaTa.length;
for
(
var
i=0;i<len;i++){
$tr
= $(
"<tr>"
).appendTo(
$tbody
);
if
(i%2==1){
$tr
.addClass(
"evenrow"
);
}
for
(
var
key in tempDaTa[i]){
$td
= $(
"<td>"
).html(tempDaTa[i][key]).appendTo(
$tr
);
}
}
this.tableobj.append(
$tbody
);
}
}
this.showFoot =
function
(){
var
$tfoot
= $(
"<tfoot>"
),
$tr
= $(
"<tr>"
),
$td
= $(
"<td>"
).attr(
"colspan"
,this.colNum).addClass(
"paging"
);
$tr
.append(
$td
);
$tfoot
.append(
$tr
);
this.tableobj.append(
$tfoot
);
this.pagination(
$td
);
}
this.pagination =
function
(tdCell){
var
$td
= typeof(tdCell) ==
"object"
? tdCell : $(
"#"
+ tdCell);
var
oA = $(
"<a/>"
);
oA.attr(
"href"
,
"#1"
);
oA.html(
"首页"
);
$td
.append(oA);
if
(this.currentPageNum>=2){
var
oA = $(
"<a/>"
);
oA.attr(
"href"
,
"#"
+(this.currentPageNum - 1));
oA.html(
"上一页"
);
$td
.append(oA);
}
if
(this.maxPageNum <= this.groupDataNum){
for
(
var
i = 1;i <= this.maxPageNum ;i++){
var
oA = $(
"<a/>"
);
oA.attr(
"href"
,
"#"
+i);
if
(this.currentPageNum == i){
oA.attr(
"class"
,
"current"
);
}
oA.html(i);
$td
.append(oA);
}
}
else
{
if
(this.groupNum<=1){
for
(
var
j = 1;j <= this.groupDataNum ;j++){
var
oA = $(
"<a/>"
);
oA.attr(
"href"
,
"#"
+j);
if
(this.currentPageNum == j){
oA.attr(
"class"
,
"current"
);
}
oA.html(j);
$td
.append(oA);
}
}
else
{
var
begin = (this.groupDataNum*(this.groupNum-1))+ 1,
end
,
maxGroupNum = Math.
ceil
(this.maxPageNum/this.groupDataNum);
if
(this.maxPageNum%this.groupDataNum!=0&&this.groupNum==maxGroupNum){
end
= this.groupDataNum*(this.groupNum-1)+this.maxPageNum%this.groupDataNum
}
else
{
end
= this.groupDataNum*(this.groupNum);
}
for
(
var
j = begin;j <=
end
;j++){
var
oA = $(
"<a/>"
);
oA.attr(
"href"
,
"#"
+j);
if
(this.currentPageNum == j){
oA.attr(
"class"
,
"current"
);
}
oA.html(j);
$td
.append(oA);
}
}
}
if
( (this.maxPageNum - this.currentPageNum) >= 1 ){
var
oA = $(
"<a/>"
);
oA.attr(
"href"
,
"#"
+ (this.currentPageNum + 1));
oA.html(
"下一页"
);
$td
.append(oA);
}
var
oA = $(
"<a/>"
);
oA.attr(
"href"
,
"#"
+ this.maxPageNum);
oA.html(
"尾页"
);
$td
.append(oA);
var
page_a =
$td
.find(
'a'
);
var
tempThis = this;
page_a.unbind(
"click"
).bind(
"click"
,
function
(){
var
nowNum = parseInt($(this).attr(
'href'
).substring(1));
if
(nowNum>tempThis.currentPageNum){
if
(tempThis.currentPageNum%tempThis.groupDataNum==0){
tempThis.groupNum += 1;
var
maxGroupNum = Math.
ceil
(tempThis.maxPageNum/tempThis.groupDataNum);
if
(tempThis.groupNum>=maxGroupNum){
tempThis.groupNum = maxGroupNum;
}
}
}
if
(nowNum<tempThis.currentPageNum){
if
((tempThis.currentPageNum-1)%tempThis.groupDataNum==0){
tempThis.groupNum -= 1;
if
(tempThis.groupNum<=1){
tempThis.groupNum = 1;
}
}
}
if
(nowNum==tempThis.maxPageNum){
var
maxGroupNum = Math.
ceil
(tempThis.maxPageNum/tempThis.groupDataNum);
tempThis.groupNum = maxGroupNum;
}
if
(nowNum==1){
var
maxGroupNum = Math.
ceil
(tempThis.maxPageNum/tempThis.groupDataNum);
tempThis.groupNum = 1;
}
tempThis.currentPageNum = nowNum;
tempThis.init((tempThis.currentPageNum-1)*tempThis.displayNum,
tempThis.currentPageNum*tempThis.displayNum);
return
false;
});
}
this.init =
function
(begin,
end
){
this.header = options.headers;
this.colNum = this.header.length;
this.content = options.data;
this.allDataNum = this.content.length;
if
(options.displayNum){
this.displayNum = options.displayNum;
}
if
(options.groupDataNum){
this.groupDataNum = options.groupDataNum;
}
this.clearTable();
this.showHeader();
this.showContent(begin,
end
);
this.showFoot();
}
this.init(0,options.displayNum);
}