JavaScript分頁功能的實現方法

來源:互聯網
上載者:User

JavaScript分頁功能的實現方法

 JavaScript分頁功能的實現方法

 這篇文章主要介紹了JavaScript分頁功能的實現方法,涉及javascript操作分頁的相關技巧,需要的朋友可以參考下

 

 

本文執行個體講述了JavaScript分頁功能的實現方法。分享給大家供大家參考。具體實現方法如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

<script>

//定義page為全域變數,以便下面使用

var page;

window.onload = function() {

var table = document.getElementById("table");

var btnAdd = document.getElementById("btnAdd");

var btnModify = document.getElementById("btnModify");

var btnDel = document.getElementById("btnDel");

var btnRefresh = document.getElementById("btnRefresh");

var headCheck = document.getElementById("headCheck");

//定義每頁的頁數及初始化table和tbody的id

page = new Page(5, 'table', 'sTBodyId');

//初始載入init方法

page.__init__();

//初始更新表格

page.__updateTableRows__();

}

//下面的所有方法,均寫到window.onload之外,否則運行有問題

function Page(iAbsolute, sTableId, sTBodyId) {

//每頁顯示資料的條數

this.absolute = iAbsolute;

this.tableId = sTableId;

this.tBodyId = sTBodyId;

this.rowCount = 0;//記錄數

this.pageCount = 0;//頁數

this.pageIndex = 0;//頁索引

this.__oTable__ = null;//表格引用

this.__oTBody__ = null;//要分頁內容

this.__dataRows__ = 0;//記錄行引用

this.__oldTBody__ = null;

}

//初始化

Page.prototype.__init__ = function() {

//擷取table引用

this.__oTable__ = document.getElementById(this.tableId);

//擷取tBody引用

this.__oTBody__ = this.__oTable__.tBodies[this.tBodyId];

//擷取tbody的行

this.__dataRows__ = this.__oTBody__.rows;

//擷取tbody的總行數

this.rowCount = this.__dataRows__.length;

try {

//判斷初始化時每頁顯示的資料條數,如果定義的值<0或者定義的值>本來就有的行數,那麼初始化時顯示本來的行數,否則為當前定義的行數

this.absolute = (this.absolute <= 0)

|| (this.absolute > this.rowCount) ? this.rowCount

: this.absolute;

//定義初始化時該顯示幾頁資料,也就是總頁數

this.pageCount = parseInt(this.rowCount % this.absolute == 0 ? this.rowCount

/ this.absolute

: this.rowCount / this.absolute + 1);

} catch (exception) {

}

}

//下一頁

Page.prototype.nextPage = function() {

 

if (this.pageIndex + 1 < this.pageCount) {

this.pageIndex += 1;

this.__updateTableRows__();

}

}

//上一頁

Page.prototype.prePage = function() {

if (this.pageIndex >= 1) {

this.pageIndex -= 1;

this.__updateTableRows__();

}

}

//首頁

Page.prototype.firstPage = function() {

if (this.pageIndex != 0) {

this.pageIndex = 0;

this.__updateTableRows__();

}

}

 

//尾頁

Page.prototype.lastPage = function() {

if (this.pageIndex + 1 != this.pageCount) {

this.pageIndex = this.pageCount - 1;

this.__updateTableRows__();

}

}

//頁定位方法

Page.prototype.aimPage = function(iPageIndex) {

if (iPageIndex > this.pageCount - 1) {

this.pageIndex = this.pageCount - 1;

} else if (iPageIndex < 0) {

this.pageIndex = 0;

} else {

this.pageIndex = iPageIndex;

}

this.__updateTableRows__();

}

//執行分頁時,更新顯示表格內容

Page.prototype.__updateTableRows__ = function() {

//pageIndex初始化時為0

//每頁顯示的資料*當前頁的索引

var iCurrentRowCount = this.absolute * this.pageIndex;

//如果截止到當前頁的所有資料+每頁顯示的資料>總資料條數,則還需要顯示this.absolute+ iCurrentRowCount - this.rowCount這些資料,否則,顯示0條資料

var iMoreRow = this.absolute + iCurrentRowCount > this.rowCount ? this.absolute

+ iCurrentRowCount - this.rowCount

: 0;

var tempRows = this.__cloneRows__();

//alert(tempRows === this.dataRows);

//alert(this.dataRows.length);

//將tbody從table中移除

var removedTBody = this.__oTable__.removeChild(this.__oTBody__);

//重新建立tbody

var newTBody = document.createElement("TBODY");

//給他賦一個id值,為原來的id值

newTBody.setAttribute("id", this.tBodyId);

for (var i = iCurrentRowCount; i < this.absolute + iCurrentRowCount

- iMoreRow; i++) {

//重新給body添加節點

newTBody.appendChild(tempRows[i]);

}

//將新建立的tbody加到table中

this.__oTable__.appendChild(newTBody);

/*

this.dataRows為this.oTBody的一個引用,

移除this.oTBody那麼this.dataRows引用將銷失,

code:this.dataRows = tempRows;恢複原始操作行集合.

*/

this.__dataRows__ = tempRows;

this.__oTBody__ = newTBody;

}

//複製原始操作行集合

Page.prototype.__cloneRows__ = function() {

var tempRows = [];

//將當前body中的所有節點及其子節點都複製一遍

for (var i = 0; i < this.__dataRows__.length; i++) {

tempRows[i] = this.__dataRows__[i].cloneNode(1);

}

return tempRows;

}

</script>

</head>

<body>

<!-- 這裡有一個表格,內容隨意,供分頁使用 -->

<table width="100%" cellpadding="0" cellspacing="0" border="1"

style="padding: 2">

<tr>

<td colspan="3" align="center">總共:<%=connDataList.size()%>條記錄 每頁顯示5條

<a href="javascript:void(0);"

onclick="page.firstPage();return false;">首頁</a> <a

href="javascript:void(0);" onclick="page.prePage();return false;">上一頁</a>

<a href="javascript:void(0);"

onclick="page.nextPage();return false;">下一頁</a> <a

href="javascript:void(0);" onclick="page.lastPage();return false;">尾頁</a>

<span id="pageindex"></span>

</td>

</tr>

</table>

</body>

</html>

希望本文所述對大家的javascript程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.