UNPKG

7.82 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/**
26 * 更新分页数据
27 */
28const updatePages = function (pages) {
29 var pageSize = this.pageSize(), pageIndex = 0, 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({parent: this})
43 page.rows = rows;
44 for (var j = 0; j < page.rows.length; j++) {
45 page.rows[j].rowId = page.rows[j].id
46 delete page.rows[j].id
47 }
48 this.cachedPages[index] = page
49 page.selectedIndices = selectIndices;
50 page.focus = focus;
51 } else {
52 page = this.cachedPages[index]
53 page.selectedIndices = selectIndices;
54 page.focus = focus;
55 for (var j = 0; j < rows.length; j++) {
56 r = rows[j];
57 if (!r.id)
58 r.id = Row.getRandomRowId()
59 if (r.status == Row.STATUS.DELETE) {
60
61 var row = page.getRowByRowId(r.id)
62 if(row){
63 // 针对后台不传回总行数的情况下更新总行数
64 var oldTotalRow = this.totalRow();
65 var newTotalRow = oldTotalRow - 1;
66 this.totalRow(newTotalRow);
67 if(row.status == Row.STATUS.NEW){
68 this.newCount -= 1;
69 if(this.newCount < 0)
70 this.newCount = 0;
71 }
72 }
73 this.removeRowByRowId(r.id)
74 page.removeRowByRowId(r.id)
75
76 } else {
77 row = page.getRowByRowId(r.id)
78 if (row) {
79 page.updateRow(row, r);
80 // if(row.status == Row.STATUS.NEW){
81 // // 针对后台不传回总行数的情况下更新总行数
82 // var oldTotalRow = this.totalRow();
83 // var newTotalRow = oldTotalRow + 1;
84 // this.totalRow(newTotalRow);
85 // }
86 if(row.status == Row.STATUS.NEW && r.status != Row.STATUS.NEW){
87 this.newCount -= 1;
88 if(this.newCount < 0)
89 this.newCount = 0;
90 }
91 row.status = Row.STATUS.NORMAL
92 if(r.status == Row.STATUS.NEW){
93 row.status = Row.STATUS.NEW
94 }
95 } else {
96 r.rowId = r.id
97 delete r.id
98 page.rows.push(r);
99 if(r.status != Row.STATUS.NEW){
100 r.status = Row.STATUS.NORMAL;
101 }else{
102 this.newCount += 1;
103 }
104 // 针对后台不传回总行数的情况下更新总行数
105 var oldTotalRow = this.totalRow();
106 var newTotalRow = oldTotalRow + 1;
107 this.totalRow(newTotalRow);
108 }
109 }
110 }
111 }
112
113 }
114}
115
116/**
117 * 前端分页方法,不建议使用,建议在后端进行分页
118 * @param allRows
119 */
120const setPages = function (allRows) {
121 var pageSize = this.pageSize(), pageIndex = 0, page;
122 this.cachedPages = [];
123 for (var i = 0; i < allRows.length; i++) {
124 pageIndex = Math.floor(i / pageSize);
125 if (!this.cachedPages[pageIndex]) {
126 page = new Page({parent: this})
127 this.cachedPages[pageIndex] = page
128 }
129 page.rows.push(allRows[i])
130 }
131 if (this.pageIndex() > -1)
132 this.setCurrentPage(this.pageIndex());
133 this.totalRow(allRows.length);
134 this.totalPages(pageIndex + 1);
135}
136
137const hasPage = function (pageIndex) {
138 return (this.pageCache && this.cachedPages[pageIndex]) ? true : false
139}
140
141const clearCache = function () {
142 this.cachedPages = []
143}
144
145const cacheCurrentPage = function () {
146 if (this.pageCache && this.pageIndex() > -1) {
147 var page = new Page({parent: this});
148 page.focus = this.getFocusIndex();
149 page.selectedIndices = this.selectedIndices().slice();
150 var rows = this.rows.peek();
151 for (var i = 0; i < rows.length; i++) {
152 var r = rows[i].getData();
153 r.rowId = r.id;
154 delete r.id;
155 page.rows.push(r)
156 }
157 this.cachedPages[this.pageIndex()] = page
158 }
159}
160
161/**
162 * [updatePagesSelect 根据datatable的选中行更新每页的选中行]
163 */
164const updatePagesSelect = function(){
165 var selectRows = this.getSelectedRows();
166 var pages = this.getPages();
167 for(var i = 0; i < pages.length; i++){
168 var rows = pages[i].rows;
169 var selectedIndices = [];
170 for(var j = 0; j < selectRows.length; j++){
171 var nowSelectRow = selectRows[j]
172 for(var k = 0; k < rows.length; k++){
173 var row = rows[k];
174 if(nowSelectRow == row){
175 selectedIndices.push(k);
176 break;
177 }
178 }
179 }
180 pages[i].selectedIndices = selectedIndices;
181 }
182
183}
184
185
186/**
187 * [updatePageRows 根据datatable的rows更新当前页的rows]
188 */
189const updatePageRows = function(){
190 if(this.pageCache){
191 var pageIndex = this.pageIndex();
192 var page = this.getPages()[pageIndex];
193 if(page){
194 page.rows = this.rows();
195 }
196 }
197}
198
199/**
200 * [updatePageSelect 根据datatable的选中行更新page的选中行]
201 */
202const updatePageSelect = function(){
203 if(this.pageCache){
204 var pageIndex = this.pageIndex();
205 var page = this.getPages()[pageIndex];
206 if(page){
207 var selectedIndices = this.selectedIndices().slice()
208 page.selectedIndices = selectedIndices;
209 }
210 }
211}
212
213/**
214 * [updatePageFocus 根据datatable的focus更新page的focus]
215 */
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/**
227 * [updatePageAll 根据datatable更新page对象]
228 */
229const updatePageAll = function(){
230 this.updatePageRows();
231 this.updatePageSelect();
232 this.updatePageFocus();
233}
234
235
236export {
237 setCurrentPage,
238 updatePages,
239 setPages,
240 hasPage,
241 clearCache,
242 cacheCurrentPage,
243 updatePagesSelect,
244 updatePageRows,
245 updatePageSelect,
246 updatePageFocus,
247 updatePageAll
248}
\No newline at end of file