salix/client/core/src/paging/paging.js

63 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-02-21 10:36:43 +00:00
import {module} from '../module';
import './style.scss';
export default class Paging {
get numPages() {
return Math.ceil(this.numItems / this.numPerPage);
}
2017-02-21 10:36:43 +00:00
constructor($http, $scope) {
this.$http = $http;
this.$scope = $scope;
this.where = this.filter;
this.numPerPage = null;
this.numItems = 0;
$scope.$watch('$ctrl.index.model.length', () => this.onModelUpdated());
}
2017-05-17 12:58:23 +00:00
$onChanges(changes) {
2017-05-16 10:37:48 +00:00
if (!this.index) return;
2017-02-21 10:36:43 +00:00
this.numPerPage = this.index.filter.size;
this.currentPage = this.index.filter.page;
if (changes.total)
2017-05-17 12:58:23 +00:00
this.numItems = changes.total.currentValue;
2017-02-21 10:36:43 +00:00
}
2017-02-21 10:36:43 +00:00
onModelUpdated() {
let index = this.index;
let filter = index.filter;
if (filter.page >= this.numPages && index.model.length >= this.numPerPage)
2017-02-21 10:36:43 +00:00
this.numItems = filter.page * filter.size + 1;
}
2017-02-21 10:36:43 +00:00
onPageChange(page) {
this.index.filter.page = page;
if (typeof this.pageChange === 'undefined') {
this.index.accept();
} else {
this.pageChange();
}
2017-02-21 10:36:43 +00:00
}
2017-11-15 07:41:54 +00:00
$doCheck() {
if (this.index && this.index.filter && this.index.filter.page && this.index.filter.page != this.currentPage) {
this.currentPage = this.index.filter.page;
}
}
2017-02-21 10:36:43 +00:00
}
2017-02-21 10:36:43 +00:00
Paging.$inject = ['$http', '$scope'];
export const NAME = 'vnPaging';
export const COMPONENT = {
2017-05-31 08:28:39 +00:00
template: require('./paging.html'),
2017-02-21 10:36:43 +00:00
bindings: {
2017-05-17 12:58:23 +00:00
index: '<',
pageChange: '&?',
2017-05-17 12:58:23 +00:00
total: '<'
2017-02-21 10:36:43 +00:00
},
controller: Paging
};
2017-02-21 10:36:43 +00:00
module.component(NAME, COMPONENT);