UNPKG

8.1 kBJavaScriptView Raw
1/**
2 * Module : kero dataTable page
3 * Author : liuyk(liuyk@yonyou.com)
4 * Date : 2016-08-01 14:34:01
5 */
6
7// 设置当前页
8const setCurrentPage = function(pageIndex, notCacheCurrentPage) {
9 var nowTotalRow = this.totalRow();
10 if (pageIndex != this.pageIndex() && notCacheCurrentPage != true)
11 this.cacheCurrentPage();
12 this.pageIndex(pageIndex)
13 var cachedPage = this.cachedPages[this.pageIndex()]
14 if (cachedPage) {
15 // 取当前页的选中行重设选中行
16 var selectedIndices = cachedPage.selectedIndices;
17 this.removeAllRows()
18 this.setRows(cachedPage.rows)
19 this.setRowsSelect(selectedIndices)
20 }
21 this.totalRow(nowTotalRow);
22}
23
24
25// 更新分页信息,通过fire调用,不对外提供
26const updatePages = function(pages) {
27 var pageSize = this.pageSize(),
28 pageIndex = 0,
29 page, r, row;
30 var page, index, i, rows, focus, selectIndices, status, j, row, originRow;
31 for (i = 0; i < pages.length; i++) {
32 index = pages[i].index
33 rows = pages[i].rows
34 focus = pages[i].current
35 selectIndices = pages[i].select
36 status = pages[i].status
37 if (status === 'del') {
38 this.cachedPages[index] = null;
39 continue;
40 }
41 if (!this.cachedPages[index]) {
42 page = new Page({
43 parent: this
44 })
45 page.rows = rows;
46 for (var j = 0; j < page.rows.length; j++) {
47 page.rows[j].rowId = page.rows[j].id
48 delete page.rows[j].id
49 }
50 this.cachedPages[index] = page
51 page.selectedIndices = selectIndices;
52 page.focus = focus;
53 } else {
54 page = this.cachedPages[index]
55 page.selectedIndices = selectIndices;
56 page.focus = focus;
57 for (var j = 0; j < rows.length; j++) {
58 r = rows[j];
59 if (!r.id)
60 r.id = Row.getRandomRowId()
61 if (r.status == Row.STATUS.DELETE) {
62
63 var row = page.getRowByRowId(r.id)
64 if (row) {
65 // 针对后台不传回总行数的情况下更新总行数
66 var oldTotalRow = this.totalRow();
67 var newTotalRow = oldTotalRow - 1;
68 this.totalRow(newTotalRow);
69 if (row.status == Row.STATUS.NEW) {
70 this.newCount -= 1;
71 if (this.newCount < 0)
72 this.newCount = 0;
73 }
74 }
75 this.removeRowByRowId(r.id)
76 page.removeRowByRowId(r.id)
77
78 } else {
79 row = page.getRowByRowId(r.id)
80 if (row) {
81 page.updateRow(row, r);
82 // if(row.status == Row.STATUS.NEW){
83 // // 针对后台不传回总行数的情况下更新总行数
84 // var oldTotalRow = this.totalRow();
85 // var newTotalRow = oldTotalRow + 1;
86 // this.totalRow(newTotalRow);
87 // }
88 if (row.status == Row.STATUS.NEW && r.status != Row.STATUS.NEW) {
89 this.newCount -= 1;
90 if (this.newCount < 0)
91 this.newCount = 0;
92 }
93 row.setStatus(Row.STATUS.NORMAL)
94 if (r.status == Row.STATUS.NEW) {
95 row.setStatus(Row.STATUS.NEW)
96 }
97 } else {
98 r.rowId = r.id
99 delete r.id
100 page.rows.push(r);
101 if (r.status != Row.STATUS.NEW) {
102 row.setStatus(Row.STATUS.NORMAL)
103 } else {
104 this.newCount += 1;
105 }
106 // 针对后台不传回总行数的情况下更新总行数
107 var oldTotalRow = this.totalRow();
108 var newTotalRow = oldTotalRow + 1;
109 this.totalRow(newTotalRow);
110 }
111 }
112 }
113 }
114
115 }
116}
117
118// 前端分页方法,不建议使用,建议在后端进行分页
119const setPages = function(allRows) {
120 var pageSize = this.pageSize(),
121 pageIndex = 0,
122 page;
123 this.cachedPages = [];
124 for (var i = 0; i < allRows.length; i++) {
125 pageIndex = Math.floor(i / pageSize);
126 if (!this.cachedPages[pageIndex]) {
127 page = new Page({
128 parent: this
129 })
130 this.cachedPages[pageIndex] = page
131 }
132 page.rows.push(allRows[i])
133 }
134 if (this.pageIndex() > -1)
135 this.setCurrentPage(this.pageIndex());
136 this.totalRow(allRows.length);
137 this.totalPages(pageIndex + 1);
138}
139
140// 判断是否存在索引对应的Page
141const hasPage = function(pageIndex) {
142 return (this.pageCache && this.cachedPages[pageIndex]) ? true : false
143}
144
145// 清空cachedPages
146const clearCache = function() {
147 this.cachedPages = []
148}
149
150// 更新当前分页的page对象
151const cacheCurrentPage = function() {
152 if (this.pageCache && this.pageIndex() > -1) {
153 var page = new Page({
154 parent: this
155 });
156 page.focus = this.getFocusIndex();
157 page.selectedIndices = this.selectedIndices().slice();
158 var rows = this.rows.peek();
159 for (var i = 0; i < rows.length; i++) {
160 var r = rows[i].getData();
161 r.rowId = r.id;
162 delete r.id;
163 page.rows.push(r)
164 }
165 this.cachedPages[this.pageIndex()] = page
166 }
167}
168
169//根据datatable的选中行更新每页的选中行
170const updatePagesSelect = function() {
171 var selectRows = this.getSelectedRows();
172 var pages = this.getPages();
173 for (var i = 0; i < pages.length; i++) {
174 var rows = pages[i].rows;
175 var selectedIndices = [];
176 for (var j = 0; j < selectRows.length; j++) {
177 var nowSelectRow = selectRows[j]
178 for (var k = 0; k < rows.length; k++) {
179 var row = rows[k];
180 if (nowSelectRow == row) {
181 selectedIndices.push(k);
182 break;
183 }
184 }
185 }
186 pages[i].selectedIndices = selectedIndices;
187 }
188
189}
190
191
192//根据datatable的rows更新当前页的rows
193const updatePageRows = function() {
194 if (this.pageCache) {
195 var pageIndex = this.pageIndex();
196 var page = this.getPages()[pageIndex];
197 if (page) {
198 page.rows = this.rows();
199 }
200 }
201}
202
203//根据datatable的选中行更新page的选中行
204const updatePageSelect = function() {
205 if (this.pageCache) {
206 var pageIndex = this.pageIndex();
207 var page = this.getPages()[pageIndex];
208 if (page) {
209 var selectedIndices = this.selectedIndices().slice()
210 page.selectedIndices = selectedIndices;
211 }
212 }
213}
214
215//根据datatable的focus更新page的focus
216const updatePageFocus = function() {
217 if (this.pageCache) {
218 var pageIndex = this.pageIndex();
219 var page = this.getPages()[pageIndex];
220 if (page) {
221 page.focus = this.getFocusIndex();
222 }
223 }
224}
225
226// 根据datatable更新page对象
227const updatePageAll = function() {
228 this.updatePageRows();
229 this.updatePageSelect();
230 this.updatePageFocus();
231}
232
233
234export const pageFunObj = {
235 setCurrentPage: setCurrentPage,
236 updatePages: updatePages,
237 setPages: setPages,
238 hasPage: hasPage,
239 clearCache: clearCache,
240 cacheCurrentPage: cacheCurrentPage,
241 updatePagesSelect: updatePagesSelect,
242 updatePageRows: updatePageRows,
243 updatePageSelect: updatePageSelect,
244 updatePageFocus: updatePageFocus,
245 updatePageAll: updatePageAll
246}