|
54 | 54 | <polymer-element name="aha-table" attributes=" |
55 | 55 | selectable searchable copyable removable |
56 | 56 | pagesize data-sizelist |
57 | | -selecttitle selectalltitle sorttitle columntitle copytitle removetitle edittitle searchtitle pagesizetitle summarytitle |
| 57 | +selecttitle selectalltitle sorttitle columntitle copytitle removetitle edittitle searchtitle firsttitle nexttitle previoustitle lasttitle |
| 58 | +pagetext pageoftext pagesizetext summarytext itemoftext |
58 | 59 | hintclass copyclass removeclass filtershownclass filterhiddenclass"> |
59 | 60 | <template> |
60 | 61 | <style> |
|
100 | 101 | aha-table .searchable select { |
101 | 102 | width: 80px; |
102 | 103 | } |
103 | | - aha-table .pagination { |
104 | | - margin: 10px; |
105 | | - text-align: center; |
106 | | - line-height: 20px; |
107 | | - } |
108 | | - aha-table .pagination table { |
109 | | - border-spacing: 0; |
110 | | - border: 0; |
111 | | - } |
112 | | - aha-table .pagination td { |
113 | | - padding: 0; |
114 | | - } |
115 | | - aha-table .pagination .active button { |
| 104 | + aha-table .pagination .active { |
116 | 105 | background-color: #ccc; |
117 | 106 | } |
118 | | - aha-table .pagination .available button:hover { |
| 107 | + aha-table .pagination .available:hover { |
119 | 108 | background-color: #eee; |
120 | 109 | } |
121 | | - aha-table .pagination .disabled button { |
| 110 | + aha-table .pagination .disabled { |
122 | 111 | color: #ccc; |
123 | 112 | } |
| 113 | + aha-table .pagination button+button { |
| 114 | + margin-left: -10px; |
| 115 | + } |
124 | 116 | aha-table .pagination button { |
125 | 117 | background-color: #fff; |
126 | 118 | border: 1px solid #eee; |
127 | | - min-width: 40px; |
128 | | - margin: 0; |
129 | | - line-height: 20px; |
130 | | - padding: 4px 12px; |
131 | | - text-decoration: none; |
132 | 119 | cursor: pointer; |
133 | 120 | user-select: none; |
134 | 121 | } |
135 | 122 | aha-table .pagination input { |
136 | 123 | background-color: #fff; |
137 | 124 | border: 1px solid #eee; |
138 | | - width: 40px; |
139 | | - line-height: 20px; |
140 | | - padding: 4px 12px; |
| 125 | + width: 20px; |
141 | 126 | } |
142 | 127 | aha-table table .hide { |
143 | 128 | display: none !important; |
|
286 | 271 | </tbody> |
287 | 272 | <tfoot> |
288 | 273 | <tr> |
289 | | - <td colspan="{{meta.length+1}}" class="pagination"> |
290 | | - <table> |
291 | | - <tr> |
292 | | - <td class='{{currentpage === 0 ? "disabled" : "available"}} '> |
293 | | - <button on-click="{{firstPage}}">{{first}}</button> |
294 | | - </td> |
295 | | - <td class='{{currentpage === 0 ? "disabled" : "available"}}'> |
296 | | - <button on-click="{{prevPage}}">{{previous}}</button> |
297 | | - </td> |
298 | | - <td template |
299 | | - repeat="{{ n in currentRange }}" |
300 | | - class="{{n == currentpage ? 'active' : 'available'}}" > |
301 | | - <button data-item="{{n}}" on-click="{{setPage}}">{{n+1}}</button> |
302 | | - </td> |
303 | | - <td class='{{pageCount <= 0 || currentpage === pageCount? "disabled" : "available"}}'> |
304 | | - <button on-click="{{nextPage}}">{{next}}</button> |
305 | | - </td> |
306 | | - <td class='{{pageCount <= 0 || currentpage === pageCount? "disabled" : "available"}}'> |
307 | | - <button on-click="{{lastPage}}">{{last}}</button> |
308 | | - </td> |
309 | | - <td> |
310 | | - {{ pagesizetitle }} |
311 | | - <select value="{{pagesize}}"> |
312 | | - <option template repeat="{{n in sizelist}}" selected?="{{ n == pagesize }}">{{n}}</option> |
313 | | - </select> |
314 | | - </td> |
315 | | - <td> |
316 | | - {{ summarytitle }} {{ firstItemIndex }} - {{ lastItemIndex }} of {{ itemCount }} |
317 | | - </td> |
318 | | - </tr> |
319 | | - </table> |
| 274 | + <td colspan="{{meta.length+1}}"> |
| 275 | + <div class="pagination"> |
| 276 | + <span class="paging"> |
| 277 | + <button |
| 278 | + title="{{firsttitle}}" |
| 279 | + class='first {{currentpage === 1 ? "disabled" : "available"}} ' |
| 280 | + on-click="{{firstPage}}">{{first}}</button> |
| 281 | + <button |
| 282 | + title="{{previoustitle}}" |
| 283 | + class='previous {{currentpage === 1 ? "disabled" : "available"}}' |
| 284 | + on-click="{{prevPage}}">{{previous}}</button> |
| 285 | + <span class="pager"> |
| 286 | + {{pagetext || ''}} |
| 287 | + <input type="text" value="{{currentpage}}"/> |
| 288 | + {{pageoftext || '/'}} |
| 289 | + {{pageCount}} |
| 290 | + </span> |
| 291 | + <button |
| 292 | + title="{{nexttitle}}" |
| 293 | + class='next {{pageCount <= 0 || currentpage === pageCount? "disabled" : "available"}}' |
| 294 | + on-click="{{nextPage}}">{{next}}</button> |
| 295 | + <button |
| 296 | + title="{{lasttitle}}" |
| 297 | + class='last {{pageCount <= 0 || currentpage === pageCount? "disabled" : "available"}}' |
| 298 | + on-click="{{lastPage}}">{{last}}</button> |
| 299 | + </span> |
| 300 | + <span class="pagesize"> |
| 301 | + {{ pagesizetext || '' }} |
| 302 | + <select value="{{pagesize}}"> |
| 303 | + <option template repeat="{{n in sizelist}}" selected?="{{ n == pagesize }}">{{n}}</option> |
| 304 | + </select> |
| 305 | + </span> |
| 306 | + <span class="summary"> |
| 307 | + {{ summarytext || ''}} {{ firstItemIndex }} - {{ lastItemIndex }} {{ itemoftext || '/'}} {{ itemCount }} |
| 308 | + </span> |
| 309 | + </div> |
320 | 310 | </td> |
321 | 311 | </tr> |
322 | 312 | </tfoot> |
|
376 | 366 | //pagesize: the number of items to show per page |
377 | 367 | pagesize: 10, |
378 | 368 | //currentpage: the current active page in view |
379 | | - currentpage: 0, |
380 | | - //currentRange: the range of pages (e.g 1, 2, 3, 4, 5) to display |
381 | | - currentRange: [], |
| 369 | + currentpage: 1, |
382 | 370 | //pageCount: the number of paginated pages |
383 | 371 | pageCount: 0, |
384 | 372 | //itemCount: the number of visible items |
385 | 373 | itemCount: 0, |
386 | 374 | //firstItemIndex: the index number of first item in the page, start from 1 |
387 | | - firstItemIndex: 0, |
| 375 | + firstItemIndex: 1, |
388 | 376 | //lastItemIndex: the index number of last item in the page, start from 1 |
389 | | - lastItemIndex: 0, |
390 | | - //rangeSize: the total size of the paginated range of items |
391 | | - rangeSize: 5, |
| 377 | + lastItemIndex: 1, |
392 | 378 | //sizelist: range list to adjust page size. |
393 | 379 | sizelist: [5, 10, 20, 50, 100], |
394 | 380 | //previous: label for the Previous button |
|
467 | 453 | this.editingRow._editing = false; |
468 | 454 | } |
469 | 455 | this.editingRow = row; |
470 | | - e.target.focus(); |
471 | 456 | }, |
472 | 457 | save: function(e) { |
473 | 458 | var row = e.target.templateInstance.model.row; |
|
536 | 521 |
|
537 | 522 | }; |
538 | 523 |
|
539 | | - this.currentpage = 0; |
| 524 | + this.currentpage = 1; |
540 | 525 | this.refreshPagination(); |
541 | 526 | } |
542 | 527 | }, |
|
570 | 555 | //================== |
571 | 556 | //pagination |
572 | 557 | firstPage: function() { |
573 | | - this.currentpage = 0; |
| 558 | + this.currentpage = 1; |
574 | 559 | }, |
575 | 560 | prevPage: function() { |
576 | | - if ( this.currentpage > 0 ) { |
| 561 | + if ( this.currentpage > 1 ) { |
577 | 562 | this.currentpage--; |
578 | 563 | } |
579 | 564 | }, |
580 | 565 | nextPage: function() { |
581 | | - if ( this.currentpage < this.getPageCount() ) { |
| 566 | + if ( this.currentpage < this.pageCount ) { |
582 | 567 | this.currentpage++; |
583 | 568 | } |
584 | 569 | }, |
585 | 570 | lastPage: function() { |
586 | | - this.currentpage = this.getPageCount(); |
587 | | - }, |
588 | | - getItemCount: function() { |
589 | | - var count = 0; |
590 | | - for (var i = this.data.length - 1; i >= 0; i--) { |
591 | | - if (!this.data[i]._filtered) { |
592 | | - count++; |
593 | | - } |
594 | | - }; |
595 | | - return count; |
596 | | - }, |
597 | | - getPageCount: function() { |
598 | | - return Math.ceil( this.getItemCount() / this.pagesize ) - 1; |
599 | | - }, |
600 | | - setPage: function(e,d,t) { |
601 | | - this.currentpage = parseInt( t.dataset.item, 10 ); |
| 571 | + this.currentpage = this.pageCount; |
602 | 572 | }, |
603 | 573 | currentpageChanged: function(){ |
| 574 | + this.currentpage = this.currentpage ? parseInt(this.currentpage) : 0; |
| 575 | + this.currentpage = this.currentpage < 1 ? 1 : this.currentpage; |
| 576 | + this.currentpage = this.pageCount > 0 && this.currentpage > this.pageCount ? this.pageCount : this.currentpage; |
604 | 577 | this.filterPage(); |
605 | | - this.currentRange = this.range(); |
606 | | - this.firstItemIndex = this.currentpage * this.pagesize + 1; |
| 578 | + this.firstItemIndex = (this.currentpage-1) * this.pagesize+1; |
607 | 579 | if (this.currentpage === this.pageCount) { |
608 | 580 | this.lastItemIndex = this.itemCount; |
609 | 581 | } else { |
610 | | - this.lastItemIndex = (this.currentpage + 1)* this.pagesize; |
| 582 | + this.lastItemIndex = (this.currentpage)* this.pagesize; |
611 | 583 | } |
612 | 584 | }, |
613 | 585 | pagesizeChanged: function(){ |
614 | 586 | this.pagesize = parseInt(this.pagesize); |
615 | | - this.currentpage = 0; |
| 587 | + this.currentpage = 1; |
616 | 588 | this.refreshPagination(); |
617 | 589 | }, |
618 | | - range: function() { |
619 | | - var paginations = []; |
620 | | - var start = this.currentpage; |
621 | | - |
622 | | - if ( start > this.getPageCount() - this.rangeSize ) { |
623 | | - start = this.getPageCount() - this.rangeSize + 1; |
624 | | - } |
625 | | - |
626 | | - for ( var i = start; i < start + this.rangeSize; i++ ) { |
627 | | - if (i >= 0) { |
628 | | - paginations.push(i); |
629 | | - } |
630 | | - } |
631 | | - return paginations; |
632 | | - }, |
| 590 | + // call this when total count is no changed. |
633 | 591 | filterPage: function() { |
634 | | - var from = this.currentpage * this.pagesize; |
| 592 | + var from = (this.currentpage-1) * this.pagesize; |
635 | 593 | var to = from + this.pagesize; |
636 | 594 | var count = 0; |
637 | 595 | for (var i = 0; i < this.data.length; i++) { |
|
646 | 604 | } |
647 | 605 | }; |
648 | 606 | }, |
| 607 | + // call this when total count is change. |
649 | 608 | refreshPagination: function() { |
650 | | - // Update current range to account for items per page, range. |
651 | | - this.currentRange = this.range(); |
652 | | - |
653 | 609 | // Cache the total page count and item count |
654 | | - this.itemCount = this.getItemCount(); |
655 | | - this.pageCount = this.getPageCount(); |
656 | | - this.firstItemIndex = this.currentpage * this.pagesize + 1; |
657 | | - if (this.currentpage === this.pageCount) { |
658 | | - this.lastItemIndex = this.itemCount; |
659 | | - } else { |
660 | | - this.lastItemIndex = (this.currentpage + 1)* this.pagesize; |
661 | | - } |
| 610 | + var count = 0; |
| 611 | + for (var i = this.data.length - 1; i >= 0; i--) { |
| 612 | + if (!this.data[i]._filtered) { |
| 613 | + count++; |
| 614 | + } |
| 615 | + }; |
| 616 | + this.itemCount = count; |
| 617 | + this.pageCount = Math.ceil( count / this.pagesize ); |
662 | 618 |
|
663 | 619 | // Update model bound to UI with filtered range |
664 | 620 | this.filterPage(); |
|
821 | 777 | searchplaceholder: undefined, |
822 | 778 | ready: function() { |
823 | 779 | if (this.dataset.choices) { |
824 | | - options = []; |
| 780 | + this.options = []; |
825 | 781 | var choices = JSON.parse(this.dataset.choices); |
826 | 782 | for (var option in choices) { |
827 | | - options.push({'value': option, 'label': choices[option]}); |
| 783 | + this.options.push({'value': option, 'label': choices[option]}); |
828 | 784 | } |
829 | 785 | } |
830 | 786 | } |
|
0 commit comments